visualization_views.py 5.21 KB
Newer Older
HippoHoppy's avatar
HippoHoppy committed
1 2
from django.shortcuts import render

3 4

def measure_distribution(request, input_dict, output_dict, widget):
romanorac's avatar
romanorac committed
5 6 7 8 9 10 11 12 13 14
    from disco.core import result_iterator
    from discomll.utils import distribution

    results = distribution.measure(input_dict["dataset"])

    dist = ""
    for k, v in result_iterator(results):
        dist += str(k.split("/")[-1][:-2]) + " " + str(v) + "\n"

    input_dict["string"] = dist
15 16 17
    return render(request, 'visualizations/display_string.html',
                  {'widget': widget, 'input_dict': input_dict, 'output_dict': output_dict})

romanorac's avatar
romanorac committed
18

19
def bigdata_ca(request, input_dict, output_dict, widget):
romanorac's avatar
romanorac committed
20 21 22 23 24 25 26
    from discomll.utils import accuracy
    import os.path
    from mothra.settings import MEDIA_ROOT
    from workflows.helpers import ensure_dir

    folder = 'discomll_measures'
    tag = input_dict["predictions"]
27
    destination = MEDIA_ROOT + '/' + folder + "/" + tag[0][6:] + '.txt'
romanorac's avatar
romanorac committed
28 29 30 31
    ensure_dir(destination)

    if input_dict["dataset"].params["id_index"] == -1:
        input_dict["string"] = "ID index should be defined."
32 33 34 35 36 37 38
    elif not os.path.isfile(destination):  # file doesnt exists
        measure, acc = accuracy.measure(test_data=input_dict["dataset"],
                                   predictions=input_dict["predictions"],
                                   measure="ca")
        string = "Classification Accuracy \n"
        score = str(measure) + " " + str(acc) + "\n"
        string += score
romanorac's avatar
romanorac committed
39 40
        input_dict["string"] = string

41 42
        f = open(destination, 'w')
        f.write(score)
romanorac's avatar
romanorac committed
43
        f.close()
44

HippoHoppy's avatar
HippoHoppy committed
45
    else:
46 47 48
        #ca results are cached
        string = "Classification Accuracy \n"
        f = open(destination, 'r')
romanorac's avatar
romanorac committed
49 50 51
        input_dict["string"] = string + str(f.readlines()[0])
        f.close()

52 53 54 55 56
    return render(request, 'visualizations/display_string.html',
                  {'widget': widget, 'input_dict': input_dict, 'output_dict': output_dict})


def bigdata_mse(request, input_dict, output_dict, widget):
romanorac's avatar
romanorac committed
57 58 59 60 61 62 63
    from discomll.utils import accuracy
    import os.path
    from mothra.settings import MEDIA_ROOT
    from workflows.helpers import ensure_dir

    folder = 'discomll_measures'
    tag = input_dict["predictions"]
64
    destination = MEDIA_ROOT + '/' + folder + "/" + tag[0][6:] + '.txt'
romanorac's avatar
romanorac committed
65 66 67 68
    ensure_dir(destination)

    if input_dict["dataset"].params["id_index"] == -1:
        input_dict["string"] = "ID index should be defined."
69 70 71 72
    elif not os.path.isfile(destination):  # file doesnt exists
        measure, acc = accuracy.measure(test_data=input_dict["dataset"],
                                   predictions=input_dict["predictions"],
                                   measure="mse")
romanorac's avatar
romanorac committed
73
        string = "Mean squared error\n"
74 75
        score = str(measure) + " " + str(acc) + "\n"
        string += score
romanorac's avatar
romanorac committed
76 77
        input_dict["string"] = string

78 79
        f = open(destination, 'w')
        f.write(score)
romanorac's avatar
romanorac committed
80
        f.close()
81

romanorac's avatar
romanorac committed
82 83
    else:
        string = "Mean squared error\n"
84
        f = open(destination, 'r')
romanorac's avatar
romanorac committed
85 86 87
        input_dict["string"] = string + str(f.readlines()[0])
        f.close()

88 89
    return render(request, 'visualizations/display_string.html',
                  {'widget': widget, 'input_dict': input_dict, 'output_dict': output_dict})
romanorac's avatar
romanorac committed
90

HippoHoppy's avatar
HippoHoppy committed
91

92
def results_to_file(request, input_dict, output_dict, widget):
romanorac's avatar
romanorac committed
93
    from disco.core import result_iterator
HippoHoppy's avatar
HippoHoppy committed
94 95 96 97 98
    import os.path
    from mothra.settings import MEDIA_ROOT
    from workflows.helpers import ensure_dir

    tag = input_dict["string"]
romanorac's avatar
romanorac committed
99
    folder = 'discomll_results'
romanorac's avatar
romanorac committed
100
    add = "add" if input_dict["add_params"] == "true" else ""
101 102 103

    destination = MEDIA_ROOT + '/' + folder + "/" + tag[0][6:] + add + '.txt'

HippoHoppy's avatar
HippoHoppy committed
104
    ensure_dir(destination)
105 106 107 108

    if not os.path.isfile(destination):  # file doesnt exists

        f = open(destination, 'w')
romanorac's avatar
romanorac committed
109 110 111 112 113 114 115
        if input_dict["add_params"] == "true":
            for k, v in result_iterator(tag):
                f.writelines(str(k) + " " + str(v) + "\n")
        else:
            for k, v in result_iterator(tag):
                f.writelines(str(k) + " " + str(v[0]) + "\n")
        f.close()
116
    filename = folder + "/" + tag[0][6:] + add + '.txt'
romanorac's avatar
romanorac committed
117 118

    output_dict['filename'] = filename
119 120 121
    return render(request, 'visualizations/string_to_file.html',
                  {'widget': widget, 'input_dict': input_dict, 'output_dict': output_dict})

romanorac's avatar
romanorac committed
122

123
def model_view(request, input_dict, output_dict, widget):
romanorac's avatar
romanorac committed
124 125 126 127 128 129 130 131
    from discomll.utils import model_view
    import os.path
    from mothra.settings import MEDIA_ROOT
    from workflows.helpers import ensure_dir

    folder = 'discomll_models'
    tag_name = input_dict["fitmodel_url"]
    tag = input_dict["fitmodel_url"].values()[0]
132 133

    destination = MEDIA_ROOT + '/' + folder + "/" + tag[0][6:] + '.txt'
romanorac's avatar
romanorac committed
134
    ensure_dir(destination)
135 136

    if not os.path.isfile(destination):  # file doesnt exists
romanorac's avatar
romanorac committed
137
        model = model_view.output_model(tag_name)
138
        f = open(destination, 'w')
romanorac's avatar
romanorac committed
139 140 141
        f.write(model)
        f.close()

142
    filename = folder + "/" + tag[0][6:] + '.txt'
romanorac's avatar
romanorac committed
143

144 145 146
    output_dict['filename'] = filename
    return render(request, 'visualizations/string_to_file.html',
                  {'widget': widget, 'input_dict': input_dict, 'output_dict': output_dict})