Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
severinsimmler committed Apr 16, 2018
1 parent 3a56376 commit ec070f5
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 34 deletions.
2 changes: 0 additions & 2 deletions application/gui.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#!/usr/bin/env python3

import sys
import pathlib
import PyQt5.QtGui
Expand Down
28 changes: 11 additions & 17 deletions application/modeling.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,19 @@
import time
import application.utils
import flask
import pandas as pd
import application.modeling
import dariah_topics
import numpy as np
import logging
import pathlib
import logging
import lda
import bokeh.plotting
import bokeh.embed
import werkzeug.utils
import pathlib
import time
import sys
import shutil
import logging
import tempfile
import dariah_topics
import flask
import pandas as pd
import shutil
import numpy as np
import pandas as pd
import bokeh.plotting
import bokeh.embed
import werkzeug.utils


# These messages are displayed during modeling:
INFO_2A = "FYI: This might take a while..."
INFO_3A = "In the meanwhile, have a look at"
Expand All @@ -48,9 +38,13 @@ def lda_modeling(document_term_arr, n_topics, n_iter, tempdir):
return model


def create_model(tempdir, bokeh_resources):
start = time.time()
def workflow(tempdir, bokeh_resources):
"""
Collects the user input, preprocesses the corpus, trains the LDA model,
creates visualizations, and dumps generated data.
"""
try:
start = time.time()
user_input = {'files': flask.request.files.getlist('files'),
'num_topics': int(flask.request.form['num_topics']),
'num_iterations': int(flask.request.form['num_iterations'])}
Expand Down
32 changes: 18 additions & 14 deletions application/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,24 @@ def decompress(filepath):
return pickle.load(file)


def load_data(tempdir):
"""
Loads the generated data.
"""
data_path = str(pathlib.Path(tempdir, 'data.pickle'))
parameter_path = str(pathlib.Path(tempdir, 'parameter.csv'))
topics_path = str(pathlib.Path(tempdir, 'topics.csv'))

data = application.utils.decompress(data_path)
parameter = pd.read_csv(parameter_path, index_col=0, encoding='utf-8')
parameter.columns = [''] # remove column names
topics = pd.read_csv(topics_path, index_col=0, encoding='utf-8')

data['parameter'] = [parameter.to_html(classes='parameter', border=0)]
data['topics'] = [topics.to_html(classes='topics')]
return data


def remove_markup(content):
"""
Removes markup from text.
Expand Down Expand Up @@ -220,17 +238,3 @@ def is_connected(host='8.8.8.8', port=53, timeout=3):
return True
except:
return False

def load_data(TEMPDIR):
data_path = str(pathlib.Path(TEMPDIR, 'data.pickle'))
parameter_path = str(pathlib.Path(TEMPDIR, 'parameter.csv'))
topics_path = str(pathlib.Path(TEMPDIR, 'topics.csv'))

data = application.utils.decompress(data_path)
parameter = pd.read_csv(parameter_path, index_col=0, encoding='utf-8')
parameter.columns = [''] # remove column names
topics = pd.read_csv(topics_path, index_col=0, encoding='utf-8')

data['parameter'] = [parameter.to_html(classes='parameter', border=0)]
data['topics'] = [topics.to_html(classes='topics')]
return data
2 changes: 1 addition & 1 deletion application/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def stream_template(template_name, **context):
app.update_template_context(context)
t = app.jinja_env.get_template(template_name)
return t.stream(context)
stream = flask.stream_with_context(application.modeling.create_model(TEMPDIR, bokeh_resources))
stream = flask.stream_with_context(application.modeling.workflow(TEMPDIR, bokeh_resources))
return flask.Response(stream_template('modeling.html', info=stream))


Expand Down

0 comments on commit ec070f5

Please sign in to comment.