解决opentuner无法可视化数据库问题

背景:

opentunertuning的过程会写数据库,这个数据库文件路径取决于你运行opentuner时的路径,文件名为opentuner.db/{Hostname}.db。然后opentuner还提供一个django应用来可视化数据库,但运行这个django应用一直失败。解决方法如下。

问题1:ImportError: cannot import name patterns

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/autoreload.py", line 228, in wrapper
    fn(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 125, in inner_run
    self.check(display_num_errors=True)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 359, in check
    include_deployment_checks=include_deployment_checks,
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 346, in _run_checks
    return checks.run_checks(**kwargs)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/checks/registry.py", line 81, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/checks/urls.py", line 16, in check_url_config
    return check_resolver(resolver)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/checks/urls.py", line 26, in check_resolver
    return check_method()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/urls/resolvers.py", line 254, in check
    for pattern in self.url_patterns:
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/urls/resolvers.py", line 405, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/urls/resolvers.py", line 398, in urlconf_module
    return import_module(self.urlconf_name)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/private/tmp/opentuner-master/stats_app/stats_app/urls.py", line 1, in 
    from django.conf.urls import patterns, include, url
ImportError: cannot import name patterns

解决:这是因为这个django app构建时用的是比较老的版本django,而我们用了比较新的版本。新的版本在指定urlpattern时可以直接使用元组,取消了pattern函数的使用。
在首行的import里面移除掉patterns,同时第8行进行如下修改:

# 改前:
urlpatterns = patterns('',
# 改后:
urlpatterns = (

问题2:TemplateDoesNotExist: charts.html

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
    response = self._get_response(request)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/root/opentuner/stats_app/stats_app/views/charts.py", line 51, in display_full_page
    html = render(request, 'charts.html')
  File "/usr/local/lib/python2.7/dist-packages/django/shortcuts.py", line 30, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "/usr/local/lib/python2.7/dist-packages/django/template/loader.py", line 67, in render_to_string
    template = get_template(template_name, using=using)
  File "/usr/local/lib/python2.7/dist-packages/django/template/loader.py", line 25, in get_template
    raise TemplateDoesNotExist(template_name, chain=chain)
TemplateDoesNotExist: charts.html

解决:这是因为这个django app构建时用的是比较老的版本django,而我们用了比较新的。新老对于template的使用指定路径语法是不大一样的。可以通过用新的django创建一个test app,然后查看TEMPLATES处的使用方法,然后复制过去,再把templates目录添加进来:

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    DIRECTORY_NAME + '/templates',
)

替换为:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [DIRECTORY_NAME + '/templates',],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

问题3:TclError: no display name and no $DISPLAY environment variable

Internal Server Error: /graph.png
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
    response = self._get_response(request)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/ubuntu/opentuner/stats_app/stats_app/views/charts.py", line 40, in display_graph
    fig = stats.matplotlibplot_file(labels, xlim=xlim, ylim=ylim, disp_types=disp_types)
  File "/usr/local/lib/python2.7/dist-packages/opentuner/utils/stats_matplotlib.py", line 87, in matplotlibplot_file
    figure = plt.figure()
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 539, in figure
    **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/backend_bases.py", line 171, in new_figure_manager
    return cls.new_figure_manager_given_figure(num, fig)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_tkagg.py", line 1049, in new_figure_manager_given_figure
    window = Tk.Tk(className="matplotlib")
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1818, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
TclError: no display name and no $DISPLAY environment variable

解决:在引入pyplot、pylab之前,要先更改matplotlib的后端模式为”Agg”。

matplotlib.use('Agg')

views/charts.py下最先引用到了matplotlib,所以要在这个文件下加入4、5行这两句:

  1 import datetime
  2 import django
  3 from django.shortcuts import render
  4 import matplotlib
  5 matplotlib.use('Agg')
  6 from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas

参考:port

问题4: Error encountered while connecting to db

(pysqlite2.dbapi2.DatabaseError) file is encrypted or is not a database [SQL: 'PRAGMA table_info("program")']
Error encountered while connecting to db
(pysqlite2.dbapi2.OperationalError) unable to open database file
Error encountered while connecting to db

解决:可以看源代码:

def get_dbs(path, db_type='sqlite:///'):
  """
  Arguments,
    path: Path of directory containing .db files
  Returns,
    A list of (engine, session) pairs to the dbs pointed to by
    the db files
  """
  dbs = list()
  for f in os.listdir(path):
    if 'journal' in f:
      continue
    try:
      db_path = os.path.join(path, f)
      e, sm = resultsdb.connect(db_type + db_path)
      dbs.append(sm())
    except Exception as e:
      print e
      print "Error encountered while connecting to db"
  return dbs

看到path为app的第一级目录,即是:opentuner/stats_app 而不是opentuner/stats_app/stats_app
它会遍历这个目录下的所有文件,并当其为数据库文件尝试初始化,异常便跳过继续尝试下一个。
于是,我们只需要把db文件放到这个目录下即可。

成功后的样子

python manage.py runserver
默认使用8000端口,即:localhost:8000
浏览器打开如下:

解决opentuner无法可视化数据库问题_第1张图片

OpenTuner-Visualizer

问题1:no such table: visualizer_project

Error during template rendering
In template /root/opentuner-visualizer/visualizer/templates/base.html, error at line 0

no such table: visualizer_project

解决:manage.py migrate --run-syncdb

问题2:global name 'output_server' is not defined

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
    response = self._get_response(request)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/root/opentuner-visualizer/visualizer/views/plot.py", line 189, in index
    initialize_plot()
  File "/root/opentuner-visualizer/visualizer/views/plot.py", line 122, in initialize_plot
    output_server("opentuner2")
NameError: global name 'output_server' is not defined

解决:需要安装pip install bokeh==0.9.2

问题3:NameError: global name 'cur_session' is not defined

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
    response = self._get_response(request)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/root/opentuner-visualizer/visualizer/views/plot.py", line 191, in index
    update_plot()
  File "/root/opentuner-visualizer/visualizer/views/plot.py", line 174, in update_plot
    cur_session.store_objects(source)
NameError: global name 'cur_session' is not defined

解决:global cur_session

问题4:AttributeError: unexpected attribute 'conf_id' to Circle

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
    response = self._get_response(request)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/root/opentuner-visualizer/visualizer/views/plot.py", line 189, in index
    initialize_plot()
  File "/root/opentuner-visualizer/visualizer/views/plot.py", line 136, in initialize_plot
    p.circle('x', 'y', conf_id='conf_id', fill_color='fill_color', line_color=None, source=source, size=5)
  File "/usr/local/lib/python2.7/dist-packages/bokeh/plotting/helpers.py", line 466, in func
    glyph = _make_glyph(glyphclass, kwargs, glyph_ca)
  File "/usr/local/lib/python2.7/dist-packages/bokeh/plotting/helpers.py", line 131, in _make_glyph
    return glyphclass(**kws)
  File "/usr/local/lib/python2.7/dist-packages/bokeh/model.py", line 77, in __init__
    super(Model, self).__init__(**kwargs)
  File "/usr/local/lib/python2.7/dist-packages/bokeh/core/properties.py", line 701, in __init__
    setattr(self, name, value)
  File "/usr/local/lib/python2.7/dist-packages/bokeh/core/properties.py", line 722, in __setattr__
    (name, self.__class__.__name__, text, nice_join(matches)))
AttributeError: unexpected attribute 'conf_id' to Circle, possible attributes are angle, angle_units, fill_alpha, fill_color, line_alpha, line_cap, line_color, line_dash, line_dash_offset, line_join, line_width, name, radius, radius_dimension, radius_units, size, tags, visible, x or y

那就把plot.py的136行初始化的conf_id参数去掉

问题5:AttributeError: unexpected attribute 'size' to Line

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
    response = self._get_response(request)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/root/opentuner-visualizer/visualizer/views/plot.py", line 191, in index
    initialize_plot()
  File "/root/opentuner-visualizer/visualizer/views/plot.py", line 139, in initialize_plot
    p.line('x', 'y', line_color="red", source=source_best, size=5)
  File "/usr/local/lib/python2.7/dist-packages/bokeh/plotting/helpers.py", line 466, in func
    glyph = _make_glyph(glyphclass, kwargs, glyph_ca)
  File "/usr/local/lib/python2.7/dist-packages/bokeh/plotting/helpers.py", line 131, in _make_glyph
    return glyphclass(**kws)
  File "/usr/local/lib/python2.7/dist-packages/bokeh/model.py", line 77, in __init__
    super(Model, self).__init__(**kwargs)
  File "/usr/local/lib/python2.7/dist-packages/bokeh/core/properties.py", line 701, in __init__
    setattr(self, name, value)
  File "/usr/local/lib/python2.7/dist-packages/bokeh/core/properties.py", line 722, in __setattr__
    (name, self.__class__.__name__, text, nice_join(matches)))
AttributeError: unexpected attribute 'size' to Line, possible attributes are line_alpha, line_cap, line_color, line_dash, line_dash_offset, line_join, line_width, name, tags, visible, x or y

你可能感兴趣的:(解决opentuner无法可视化数据库问题)