解决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 <module>
    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-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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,126评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,254评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,445评论 0 341
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,185评论 1 278
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,178评论 5 371
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,970评论 1 284
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,276评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,927评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,400评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,883评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,997评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,646评论 4 322
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,213评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,204评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,423评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,423评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,722评论 2 345

推荐阅读更多精彩内容