예제 #1
0
    def _doc_modifier(doc: Document) -> None:
        plot, source = _create_plot(stats, aspects)
        events_table = _create_events_table()
        events_type = RadioButtonGroup(labels=['All Events', 'In-Domain Events'], active=0)

        # pylint: disable=unused-argument
        def _events_handler(attr, old, new):
            _update_events(stats, aspects, events_table, source, events_type.active)

        events_type.on_change('active', _events_handler)
        source.selected.on_change('indices', _events_handler)  # pylint: disable=no-member
        doc.add_root(column(_create_header(), plot, events_type, events_table,
                            sizing_mode="scale_width"))
예제 #2
0
    def setupControls(self):
        repeatResolutionSlider = Slider(
            start=1, end=10, value=1, step=1, title="Repeat resolution"
        )  #,width = self.figureWidth,height = self.buttonHeight)
        repeatResolutionSlider.on_change("value",
                                         self.repeatResolutionCallback)

        chunkModeButtons = RadioButtonGroup(
            labels=["Time", "Sample"], active=0, button_type="warning"
        )  #,width=self.buttonWidth,height = self.buttonHeight)
        chunkModeButtons.on_change("active", self.chunkModeCallback)

        yAxisModeButtons = RadioButtonGroup(labels=["Linear", "Log"],
                                            active=0,
                                            button_type="warning")
        yAxisModeButtons.on_change("active", self.yAxisModeCallback)

        self.controls = row(repeatResolutionSlider, chunkModeButtons,
                            yAxisModeButtons)
예제 #3
0
    def use_server_with_radio_buttons(self):
        # crate columndatasource
        source = ColumnDataSource(
            dict(average_grades=["B+", "A", "D-"],
                 exam_grades=["A+", "C", "D"],
                 student_names=["Stephan", "Helder", "Riazudidn"]))

        # create the figure
        f = figure(x_range=[
            "F", "D-", "D", "D+", "C-", "C", "C+", "B-", "B", "B+", "A-", "A",
            "A+"
        ],
                   y_range=[
                       "F", "D-", "D", "D+", "C-", "C", "C+", "B-", "B", "B+",
                       "A-", "A", "A+"
                   ])

        # add labels for glyphs
        labels = LabelSet(x="average_grades",
                          y="exam_grades",
                          text="student_names",
                          x_offset=5,
                          y_offset=5,
                          source=source)
        f.add_layout(labels)

        # create glyphs
        f.circle(x="average_grades", y="exam_grades", source=source, size=8)

        # create function
        def update_labels(attr, old, new):
            labels.text = options[radio_button_group.active]

        # create select widget
        options = ["average_grades", "exam_grades", "student_names"]
        radio_button_group = RadioButtonGroup(labels=options)
        radio_button_group.on_change("active", update_labels)

        # create layout and add to curdoc
        lay_out = layout([[radio_button_group]])
        curdoc().add_root(f)
        curdoc().add_root(lay_out)
예제 #4
0
파일: main.py 프로젝트: q-rai/WasteKnox
def source_filters(sources):
    def _handle_state_filter():
        for fullname in sources['dataframes']:
            df = sources['dataframes'][fullname]
            df = df[(df.index >= _state['date_range'][0]) & (df.index <= _state['date_range'][1])]
            units = UNIT_CONVERSION_MAP[_state['units']]
            df = df * UNIT_CONVERSION[units]
            sources['sources'][fullname].data = ColumnDataSource(df).data

        for callback in ELEMENT_CALLBACKS:
            callback(sources=sources, **_state)

    def _date_slider_handler(attr, old, new):
        start_time, end_time = new
        start_date = dt.datetime.fromtimestamp(start_time / 1000)
        end_date = dt.datetime.fromtimestamp(end_time / 1000)
        _state['date_range'] = (start_date, end_date)
        _handle_state_filter()

    start_date = dt.datetime(year=2018, month=12, day=1)
    end_date = dt.datetime(year=2018, month=12, day=1)
    date_slider = DateRangeSlider(start=_state['date_range'][0], end=_state['date_range'][1], value=_state['date_range'], step=1, title="Date Range", height=100)
    date_slider.on_change('value', _date_slider_handler)

    def _radio_button_group_handler(attr, old, new):
        _state['units'] = int(new)
        _handle_state_filter()

    radio_button_group = RadioButtonGroup(labels=UNIT_CONVERSION_MAP, active=0, width=700, height=100)

    radio_button_group.on_change('active', _radio_button_group_handler)

    div_button_group_selection = Div(text=f'<img src="/visualization/static/images/{UNIT_CONVERSION_MAP[_state["units"]]}.svg" height="100px"/>', height=100)

    def _update_div_button_group(**kwargs):
        units = UNIT_CONVERSION_MAP[kwargs['units']]
        div_button_group_selection.text = f'<img src="/visualization/static/images/{units}.svg" height="100px"/>'
    ELEMENT_CALLBACKS.append(_update_div_button_group)

    return [date_slider, radio_button_group, div_button_group_selection]
예제 #5
0
파일: ui.py 프로젝트: yuansky/nlp-architect
    def show_analysis() -> None:
        global stats, aspects, plot, source, tabs
        plot, source = _create_plot()
        events_table = _create_events_table()

        # pylint: disable=unused-argument
        def _events_handler(attr, old, new):
            _update_events(events_table, events_type.active)

        # Toggle display of in-domain / All aspect mentions
        events_type = RadioButtonGroup(labels=["All Events", "In-Domain Events"], active=0)

        analysis_layout = layout([[plot], [events_table]])

        # events_type display toggle disabled
        # analysis_layout = layout([[plot],[events_type],[events_table]])

        analysis_tab = Panel(child=analysis_layout, title="Analysis")
        tabs.tabs.insert(2, analysis_tab)
        tabs.active = 2
        events_type.on_change("active", _events_handler)
        source.selected.on_change("indices", _events_handler)  # pylint: disable=no-member
    def setupControls(self):
        boardModeButtons = RadioButtonGroup(labels=self.boardModes,
                                            active=0,
                                            width=400,
                                            button_type="primary")
        boardModeButtons.on_change("active", self.boardModeCallback)

        self.brightnessSlider = Slider(title="Brightness",
                                       show_value=False,
                                       height=self.figureSize,
                                       value=.5,
                                       start=0,
                                       end=1,
                                       step=.1,
                                       orientation="vertical")
        self.brightnessSlider.on_change("value", self.brightnessCallback)

        #create an empty row to store the active controls in
        activeControlsBox = column(name="activeControls")

        #setup the image handler controls to control the board
        cIS.iconFileSource.on_change('data', self.iconFileCallback)

        self.controls = column(boardModeButtons, activeControlsBox)
예제 #7
0
    labels.text = ["chosen file is : " + tkFileDialog.askopenfilename()]
    print labels.text


def open_local_file_long(attr, old, new):
    import wx
    import os

    app = wx.PySimpleApp()
    wildcard = "Python source (*.py)|*.py|" \
               "Compiled Python (*.pyc)|*.pyc|" \
               "All files (*.*)|*.*"
    dialog = wx.FileDialog(None, "Choose a file", os.getcwd(), "", wildcard,
                           wx.OPEN)
    if dialog.ShowModal() == wx.ID_OK:
        print dialog.GetPath()

    dialog.Destroy()


#create select widget
options = ["average_grades", "exam_grades", "student_names"]
radio_button_group = RadioButtonGroup(labels=options)
# radio_button_group.on_change("active",update_labels)
radio_button_group.on_change("active", open_local_file)

#create layout and add to curdoc
lay_out = layout([[radio_button_group]])
curdoc().add_root(f)
curdoc().add_root(lay_out)
예제 #8
0
                     plot_height=450,
                     plot_width=600,
                     tools="save,reset",
                     toolbar_location="below")

plot_figure.scatter('x', 'y', color='label', source=source, size=10)

radio_button_group = RadioButtonGroup(labels=["Red", "Orange"])


def radiogroup_click(attr, old, new):
    active_radio = radio_button_group.active  ##Getting radio button value

    # filter the dataframe with value in radio-button
    if active_radio == 0:
        selected_df = df[df['label'] == 'Red']
    elif active_radio == 1:
        selected_df = df[df['label'] == "Orange"]

    source.data = dict(x=selected_df.x,
                       y=selected_df.y,
                       label=selected_df.label)


radio_button_group.on_change('active', radiogroup_click)

layout = row(radio_button_group, plot_figure)

curdoc().add_root(layout)
curdoc().title = "Radio Button Group Bokeh Server"
예제 #9
0

######################
# Callback behaviour #
######################
sample_fun_input_f.on_change('value', sample_fun_input_modified)
Wavelet_fun_input.on_change('value', Wavelet_fun_modified)
Wavelet_fun_input.on_change('label', update)
T0_input.on_change('value', update)
T1_input.on_change('value', update)
Amp_input.on_change('value', update)
User_Func.on_change('value', update)
Resolution.on_change('value', update)
a_param.on_change('value', param_change)
b_param.on_change('value', param_change)
Trigonometric_radio.on_change('active', Trig_fun_modified)
Frequency_Slider.on_change('value', Trig_fun_modified)
Calc_button.on_change('clicks', update)

############################
# Description and warnings #
############################
description_filename = join(dirname(__file__), "description.html")
loading_spinner_filename = join(dirname(__file__), "loading_spinner.html")
choose_WaveLet_error_filename = join(dirname(__file__),
                                     "choose_WaveLet_error.html")
choose_SampleFunc_error_filename = join(dirname(__file__),
                                        "choose_SampleFunc_error.html")
user_function_error_filename = join(dirname(__file__),
                                    "user_function_error.html")
예제 #10
0
class ProfilePlot:
    """
    A profile plot displays the height distribution of an atmospheric quantity
    with respect to either altitude or pressure.
    """
    def __init__(self, z, p, title, name, width=500):
        def update_y_variable(attrname, old, new):

            if self.y_axis_quantity.active == 0:
                self.y = self.z
                self.y_range = Range1d(start=self.z[0], end=self.z[-1])

            if self.y_axis_quantity.active == 1:
                self.y = self.p
                self.y_range = Range1d(start=self.p[-1], end=self.p[0])

            for s in self.sources:
                s.data["y"] = self.y

            fig = curdoc().get_model_by_name(self.name).children[0]
            fig.y_range = self.y_range

        def update_x_scale(attrname, old, new):
            if self.x_axis_scale.active == 1:
                for s in self.sources:
                    s.data["x"] = np.log10(s.data["x_"])
            else:
                for s in self.sources:
                    s.data["x"] = s.data["x_"]

        def update_y_scale(attrname, old, new):
            if self.y_axis_scale.active == 1:
                for s in self.sources:
                    s.data["y"] = np.log10(self.y)
            else:
                for s in self.sources:
                    s.data["y"] = self.y

        self.z = np.copy(z)
        self.p = np.copy(p)
        self.y = z
        self.quantities = []

        self.name = name

        self.x_axis_scale = RadioButtonGroup(labels=["linear", "log"],
                                             active=0)
        self.x_axis_scale.on_change("active", update_x_scale)

        self.y_axis_scale = RadioButtonGroup(labels=["linear", "log"],
                                             active=0)
        self.y_axis_scale.on_change("active", update_y_scale)

        self.y_axis_quantity = RadioButtonGroup(
            labels=["altitude", "pressure"], active=0)
        self.y_axis_quantity.on_change("active", update_y_variable)

        self.sources = []

        self.fig = figure(title=title, width=width, height=500)

    def add_quantity(self, x, line_kwargs={}):
        self.sources += [ColumnDataSource(data=dict(x=x, x_=x, y=self.y))]
        self.fig.line("x", "y", source=self.sources[-1], **line_kwargs)

    @property
    def doc(self):
        d1 = Div(text="y-axis variable:", width=200)
        d2 = Div(text="x-axis scale:", width=200)
        d3 = Div(text="y-axis scale:", width=200)
        return column(self.fig,
                      row(d1, self.y_axis_quantity),
                      row(d2, self.x_axis_scale),
                      row(d3, self.y_axis_scale),
                      name=self.name)
예제 #11
0
class Interface:
    def __init__(self):

        self.grepolis = Grepolis()

        imgRessource = []
        for v in ["Bois", "Pierre", "Argent"]:
            r = "static/" + v + ".png"
            d = dict(url=[r])
            imgRessource.append(Image(d))
        colRess = column(*[img.figure for img in imgRessource])
        self.inputRess = [
            TextInput(value="", title=el + " :", width=150)
            for el in ["Bois", "Pierre", "Argent"]
        ]
        colinputRess = column(*self.inputRess)

        imgDieu = []
        for v in ["Athena", "Artemis", "Hades", "Zeus", "Poseidon", "Hera"]:
            r = "static/" + v + ".png"
            d = dict(url=[r])
            imgDieu.append(Image(d, multiplier=3))
        rowDieu = [HFill(5)]
        for img in imgDieu:
            rowDieu.append(HFill(5))
            rowDieu.append(img.figure)
        rowDieu = row(*rowDieu)

        imgAtt = []
        for v in ["Att_hack", "Att_sharp", "Att_distance"]:
            r = "static/" + v + ".png"
            d = dict(url=[r])
            imgAtt.append(Image(d))
        colAtt = column(*[img.figure for img in imgAtt])
        self.inputAtt = [
            TextInput(value="", title=el + " :", width=150)
            for el in ["Contondantes", "Blanches", "De Jet"]
        ]
        colinputAtt = column(*self.inputAtt)

        imgDef = []
        for v in ["Def_hack", "Def_sharp", "Def_distance"]:
            r = "static/" + v + ".png"
            d = dict(url=[r])
            imgDef.append(Image(d))
        colDef = column(*[img.figure for img in imgDef])

        self.inputDef = [
            TextInput(value="", title=el + " :", width=150)
            for el in ["Contondantes", "Blanches", "De Jet"]
        ]
        rowinputDef = column(*self.inputDef)

        imgOther = []
        for v in ["Vitesse", "Butin", "Faveur"]:
            r = "static/" + v + ".png"
            d = dict(url=[r])
            imgOther.append(Image(d))

        colOther = column(*[img.figure for img in imgOther])

        self.inputFavBut = [
            TextInput(value="", title=el + " :", width=150)
            for el in ["Vitesse", "Butin", "Faveur"]
        ]
        self.inputOther = column(*self.inputFavBut)

        self.imgUnit = []
        for v in [
                "Combattant", "Frondeur", "Archer", "Hoplite", "Cavalier",
                "Char", "Envoye", "Centaure", "Pegase"
        ]:
            r = "static/" + v + ".jpg"
            d = dict(url=[r])
            self.imgUnit.append(Image(d, multiplier=2))
        rowUnit = row(HFill(10), *[img.figure for img in self.imgUnit])

        imgDefAtt = []
        for v in ["Pop", "Attaque", "Defense"]:
            r = "static/" + v + ".png"
            d = dict(url=[r])
            imgDefAtt.append(Image(d))

        rowInputUnit = [HFill(10)]
        self.unitInput = [
            TextInput(value="", title=el + " :", width=80) for el in [
                "Combattant", "Frondeur", "Archer", "Hoplite", "Cavalier",
                "Char", "Envoye", "Centaure", "Pegase"
            ]
        ]
        for inp in self.unitInput:
            rowInputUnit.append(inp)
            rowInputUnit.append(HFill(30))
        rowInputUnit = row(HFill(10), *rowInputUnit)

        self.selectUnit = CheckboxButtonGroup(labels=[
            "Combattant", "Frondeur", "Archer", "Hoplite", "Cavalier", "Char",
            "Envoye", "Centaure", "Pegase"
        ],
                                              active=[i for i in range(9)])
        self.selectUnit.on_change("active", self.updateSelectUnit)
        self.Dieu = RadioButtonGroup(
            labels=["Athena", "Artemis", "Hades", "Zeus", "Poseidon", "Hera"],
            active=0,
            width=1110)
        self.Dieu.on_change('active', self.updateUnit)

        self.attdef = RadioButtonGroup(labels=["Attaque", "Defense"],
                                       active=0,
                                       width=200)
        self.attdef.on_change('active', self.switchAttDef)

        self.typeAtt = RadioGroup(
            labels=["Armes Contondantes", "Armes Blanches", "Armes de Jet"],
            active=0,
            width=150)
        self.typeAtt.on_change('active', self.process2)
        self.imgFaveur = Image(dict(url=["static/" + "Faveur" + ".png"]))

        self.launch = Button(label="Lancer")
        self.launch.on_click(self.process)

        self.inputPop = TextInput(value="1500",
                                  title="Population : ",
                                  width=120)
        self.inputPop.on_change("value", self.process2)

        self.inputFav = TextInput(value="1500",
                                  title="Faveur Max : ",
                                  width=120)
        self.inputFav.on_change("value", self.process2)

        rowPop = row(HFill(10), self.typeAtt, imgDefAtt[1].figure, self.attdef,
                     HFill(30), imgDefAtt[2].figure, HFill(50),
                     imgDefAtt[0].figure, self.inputPop, HFill(50),
                     self.imgFaveur.figure, self.inputFav, HFill(50))
        self.doc = column(
            rowDieu, self.Dieu, VFill(20), rowPop, VFill(20), self.selectUnit,
            rowUnit, rowInputUnit, VFill(20),
            row(HFill(50), colRess, colinputRess,
                HFill(40), colAtt, colinputAtt, HFill(40), colDef, rowinputDef,
                HFill(40), colOther, self.inputOther))
        #curdoc().add_root(column(rowDieu,self.Dieu,VFill(20),rowPop,VFill(20),self.selectUnit,rowUnit,rowInputUnit,VFill(20),row(HFill(50),colRess,colinputRess,HFill(40),colAtt,colinputAtt,HFill(40),colDef,rowinputDef,HFill(40),colOther,self.inputOther)))
        self.process(None)
        #curdoc().title = "Grepolis"

    def updateUnit(self, attrname, old, new):
        L = ["Athena", "Artemis", "Hades", "Zeus", "Poseidon", "Hera"]
        print(self.selectUnit.active)
        if L[new] == "Poseidon":
            self.imgUnit[-1].figure.visible = False
            self.unitInput[-1].visible = False
            self.selectUnit.active = [1, 2, 3, 4, 5, 6, 7]
        else:
            self.imgUnit[-1].figure.visible = True
            self.unitInput[-1].visible = True
            self.selectUnit.active = [1, 2, 3, 4, 5, 6, 7, 8]
        self.grepolis.setDieu(L[new])
        unit = Add(L[new])
        self.selectUnit.labels = [
            "Combattant", "Frondeur", "Archer", "Hoplite", "Cavalier", "Char",
            "Envoye"
        ] + unit
        for i, v in enumerate(unit):
            r = "static/" + v + ".jpg"
            d = dict(url=[r])
            self.imgUnit[-2 + i].update(d)
            self.unitInput[-2 + i].title = v + " : "
        self.process(None)

    def process2(self, attrname, old, new):
        self.process(None)

    def switchAttDef(self, attrname, old, new):
        if self.attdef.active == 0:
            self.typeAtt.disabled = False
        else:
            self.typeAtt.disabled = True
        self.process(None)

    def updateSelectUnit(self, attrname, old, new):
        N = len(self.selectUnit.labels)
        active = self.selectUnit.active
        zeros = [i for i in range(N) if i not in active]
        for inp in [self.unitInput[i] for i in zeros]:
            inp.value = str(0)
        self.process(None)

    def process(self, attrname):
        try:
            pop = int(self.inputPop.value)
            favmax = int(self.inputFav.value)
            active = self.selectUnit.active
            type = self.typeAtt.active
            if self.attdef.active == 0:
                X, Att, Def, Prix, Speed, Pops, butin = self.grepolis.optimAttaque(
                    pop=pop, favmax=favmax, active=active, type=type)

            else:
                X, Att, Def, Prix, Speed, Pops, butin = self.grepolis.optimDefense(
                    pop=pop, favmax=favmax, active=active)
            for v, inp in zip(X, [self.unitInput[i] for i in active]):
                inp.value = str(v)
            for v, inp in zip(Att, self.inputAtt):
                inp.value = str(v) + " /u - " + str(int(v * pop))
            for v, inp in zip(Def, self.inputDef):
                inp.value = str(v) + " /u - " + str(int(v * pop))
            for v, inp in zip(Prix, self.inputRess):
                inp.value = str(v) + " /u - " + str(int(v * pop))
            for i, (v, inp) in enumerate(
                    zip([Speed, butin, Prix[3]], self.inputFavBut)):
                #add = ""
                #if i > 0:
                #    add = + " /u - " +str(int(v*pop))
                #print(v,add)
                inp.value = str(v) + " /u - " + str(int(
                    v * pop)) if i != 0 else str(v)
        except:
            pass
df=pd.DataFrame({'x':x,'y':y,'label':label})

source = ColumnDataSource(data=dict(x=df.x, y=df.y,label=df.label))

plot_figure = figure(title='Radio Button Group',plot_height=450, plot_width=600,
              tools="save,reset", toolbar_location="below")

plot_figure.scatter('x', 'y',color='label', source=source, size=10)

radio_button_group = RadioButtonGroup(labels=["Red", "Orange"])

def radiogroup_click(attr,old,new):
    active_radio=radio_button_group.active ##Getting radio button value

    # filter the dataframe with value in radio-button
    if active_radio==0:
        selected_df = df[df['label'] == 'Red']
    elif active_radio==1:
        selected_df = df[df['label'] == "Orange"]

    source.data=dict(x=selected_df.x, y=selected_df.y,label=selected_df.label)


radio_button_group.on_change('active',radiogroup_click)

layout=row(radio_button_group, plot_figure)

curdoc().add_root(layout)
curdoc().title = "Radio Button Group Bokeh Server"
def main():
    df = pd.read_csv("nlp_entities.csv")
    s_per_df = pd.read_csv("data/entity_sentiment_avgValues/PersonSentiment.csv")
    s_pla_df = pd.read_csv("data/entity_sentiment_avgValues/PlaceSentiment.csv")
    s_org_df = pd.read_csv("data/entity_sentiment_avgValues/OrganizationSentiment.csv")
    r = 300
    margin = 80
    sentiment = ["all", "Negative", "Positive", "Neutral"]
    with open("Radviz_files.json") as json_file:
        sources = json.load(json_file)
    json_file.close()
    for s in sentiment:
        dstr = sources[s]["views"]["dstr"]
        dates = []
        for d in dstr:
            dates.append(datetime.strptime(d, "%m/%d/%Y"))
        sources[s]["views"]["s_x"] = dates

    ############# Radviz for headlines #############
    print("ploting Radviz ...")
    a_source = ColumnDataSource(data=sources["all"]["anchors"])
    v_source = ColumnDataSource(data=sources["all"]["views"])
    radviz = figure(plot_width=400, plot_height=450, title="Headlines (anchors: words), Centroid Measure",
               x_axis_type=None, y_axis_type=None,
               output_backend="webgl",tools="pan,box_zoom,box_select,reset,save")
    radviz.circle(r, r, radius=r, line_color="black", fill_color=None)
    c1 = radviz.circle('a_x', 'a_y', size=7, color="#af8dc3", source=a_source)
    c2 = radviz.circle('v_x', 'v_y', size=7, color='c', alpha=0.8, source=v_source,legend= 'senti')
    radviz.x_range = Range1d(-margin, 2 * r + margin)
    radviz.y_range = Range1d(-margin, 2 * r + margin)
    c2.selection_glyph = Circle(fill_color="c", line_color=None)
    c2.nonselection_glyph = Circle(fill_color=None, line_color="c")

    hover1 = HoverTool(tooltips=[("words: ", "@anchors")], renderers=[c1])
    hover2 = HoverTool(tooltips=[("file: ", "@views"),
                                 ("headlines: ", "@h"),
                                 ("sentiment: ", "@senti"),
                                 ("sentiment score: ", "@score")
                                 ], renderers=[c2])

    radviz.add_tools(hover1)
    radviz.add_tools(hover2)

    radviz.legend.location = (0,380)
    radviz.legend.orientation = "horizontal"
    radviz.legend.label_text_font_size = "8pt"

    ############# Timeline for each file #############
    print("ploting scatter ...")
    scatter = figure(plot_width=750, plot_height=450, title="Timeline for Files",
                x_axis_type='datetime', x_axis_label='Time',y_axis_label='Sentiment Score',
               output_backend="webgl",tools="pan,box_zoom,box_select,reset,save")
    c3 = scatter.circle('s_x', 'score', source=v_source, size=7, color='c', alpha=0.8, legend= 'senti')
    hover3 = HoverTool(
        tooltips=[
            ("file: ", "@views"),
            ("date: ", "@dstr"),
            ("headline: ", "@e_h")
        ]
    )
    scatter.add_tools(hover3)
    scatter.x_range = Range1d(datetime.strptime('01/01/2002', "%m/%d/%Y"), datetime.strptime('12/31/2004', "%m/%d/%Y"))
    scatter.legend.location = "top_left"
    c3.selection_glyph = Circle(fill_color="c", line_color="#d73027")
    c3.nonselection_glyph = Circle(fill_color=None, line_color="c")


    #create a button group to select different sentiment headlines
    button_group = RadioButtonGroup(labels=sentiment, active=0)

    def button_group_update(attr, old, new):
        b = button_group.active
        s = sentiment[b]
        a_source.data = sources[s]["anchors"]
        v_source.data = sources[s]["views"]
        return

    button_group.on_change("active", button_group_update)

    # create interactive network graph
    print("ploting network 1 ...")
    s1 = ColumnDataSource(data=dict(xs=[], ys=[]))
    s2 = ColumnDataSource(data=dict(vx=[], vy=[], labels=[], color=[],h=[], senti=[]))
    n1 = figure(plot_width=600, plot_height=600, x_axis_type=None, y_axis_type=None,
                outline_line_color=None, tools="pan,box_select,box_zoom,reset,save",
                title="Neighbors of Selected Files",output_backend="webgl")
    n1.multi_line('xs', 'ys', line_color="black", source=s1, alpha=0.3)
    c4 = n1.circle('vx', 'vy', size=20, line_color="black", fill_color='color', source=s2, alpha=0.5,legend='senti')
    n1.text('vx', 'vy', text='labels', text_color="black", text_font_size="10px", text_align="center",
            text_baseline="middle", source=s2)

    hover4 = HoverTool(
        tooltips=[
            ("file: ", "@labels"),
            ("headline: ", "@h")
        ], renderers= [c4]
    )
    n1.add_tools(hover4)
    n1.legend.location = "top_left"

    def update_network1(attr,old,new):
        inds = new['1d']['indices']
        nodes = []
        edges = []
        selected = []
        if inds == []:
            s1.data = dict(xs=[], ys=[])
            s2.data = dict(vx=[], vy=[], labels=[], color=[],h=[], senti=[])
            n1.title.text = "Neighbors of Selected Files"
            return
        for i in inds:
            f = v_source.data["views"][i]
            selected.append(f)
            v = df.loc[df["filename"] == f]["file_neighbors"].values
            n = ast.literal_eval(v[0])
            if f not in n:
                n.append(f)
            nodes = list(set(nodes).union(n))
            e_v = df.loc[df["filename"] == f]["file_neighbors_edge"].values
            e = ast.literal_eval(e_v[0])
            edges = list(set(edges).union(e))
        new_dict = create_file_graph(nodes,edges,df)
        s1.data = new_dict["s1"]
        s2.data = new_dict["s2"]
        n1.title.text = "Neighbors of Selected Files - " +", ".join(selected)
        return

    c2.data_source.on_change("selected",update_network1)

    print("ploting network 2 ...")
    s3 = ColumnDataSource(data=dict(xs=[], ys=[]))
    s4 = ColumnDataSource(data=dict(vx=[], vy=[], labels=[], color=[],s=[]))
    n2 = figure(plot_width=600, plot_height=600, x_axis_type=None, y_axis_type=None,
                outline_line_color=None, tools="pan,box_zoom,reset,save", title="Entities Involved in Selected Files",
                output_backend="webgl")
    n2.multi_line('xs', 'ys', line_color="black", source=s3, alpha=0.3)
    n2.circle('vx', 'vy', size=20, line_color="black", fill_color='color', source=s4, alpha=0.5,legend='s')
    n2.text('vx', 'vy', text='labels', text_color="black", text_font_size="10px", text_align="center",
            text_baseline="middle", source=s4)
    n2.legend.location = (0,580)
    n2.legend.orientation = "horizontal"
    n2.legend.label_text_font_size = "8pt"

    def update_network2(attr, old, new):
        inds = new['1d']['indices']

        s3.data = dict(xs=[], ys=[])
        s4.data = dict(vx=[], vy=[], labels=[], color=[], s=[])
        n2.title.text = "Entities Involved in Selected Files"

        if inds == []:
            return

        selected = []
        nodes = []
        edges = []
        r_per = []
        r_pla = []
        r_org = []
        t_t_edges = []
        senti = {}
        s_color = {}
        for i in inds:
            f = s2.data["labels"][i]
            selected.append(f)
            v = df.loc[df["filename"] == f]["all_nodes"].values
            n = ast.literal_eval(v[0])
            nodes = list(set(nodes).union(n))
            e = ast.literal_eval(df.loc[df["filename"] == f]["all_edges"].values[0])
            edges = list(set(edges).union(e))
            t_t_e = ast.literal_eval(df.loc[df["filename"] == f]["file_neighbors_edge"].values[0])
            t_t_edges = list(set(t_t_edges).union(t_t_e))
            r_person = df.loc[df["filename"] == f]["Person"].values[0]
            if r_person == "None":
                r_person = []
            else:
                r_person = ast.literal_eval(r_person)
            r_place = df.loc[df["filename"] == f]["Place"].values[0]
            if r_place == "None":
                r_place = []
            else:
                r_place = ast.literal_eval(r_place)
            r_organization = df.loc[df["filename"] == f]["Org"].values[0]
            if r_organization == "None":
                r_organization = []
            else:
                r_organization = ast.literal_eval(r_organization)
            r_per = list(set(r_per).union(r_person))
            r_pla = list(set(r_pla).union(r_place))
            r_org = list(set(r_org).union(r_organization))

        t_t = list(itertools.combinations(selected, 2))
        for t in t_t:
            if (t in t_t_edges) or ((t[1], t[0]) in t_t_edges):
                edges.append(t)

        for node in nodes:
            if node in r_per:
                s_color[node] = s_per_df.loc[s_per_df["Entity"] == node]["Sentiment Color"].values[0]
                senti[node] = s_per_df.loc[s_per_df["Entity"] == node]["Sentiment"].values[0]
            elif node in r_pla:
                s_color[node] = s_pla_df.loc[s_pla_df["Entity"] == node]["Sentiment Color"].values[0]
                senti[node] = s_pla_df.loc[s_pla_df["Entity"] == node]["Sentiment"].values[0]
            elif node in r_org:
                s_color[node] = s_org_df.loc[s_org_df["Entity"] == node]["Sentiment Color"].values[0]
                senti[node] = s_org_df.loc[s_org_df["Entity"] == node]["Sentiment"].values[0]
            elif node in selected:
                s_color[node] = df.loc[df["filename"] == node]["sentiment_color"].values[0]
                senti[node] = df.loc[df["filename"] == node]["sentiment"].values[0]
            elif "-Person" in node:
                s_color[node] = "#f441e8"
                senti[node] = "Person Parent Node"
            elif "-Place" in node:
                s_color[node] = "#e2f441"
                senti[node] = "Place Parent Node"
            elif "-Org" in node:
                s_color[node] = "#f49141"
                senti[node] = "Org Parent Node"

        new_dict = create_graph(nodes, edges, s_color, senti)
        s3.data = new_dict["s1"]
        s4.data = new_dict["s2"]
        n2.title.text = "Entities Involved in Selected Files - " + ", ".join(selected)
        return

    c4.data_source.on_change("selected", update_network2)

    def update_network3(attr, old, new):
        inds = new['1d']['indices']
        s3.data = dict(xs=[], ys=[])
        s4.data = dict(vx=[], vy=[], labels=[], color=[], s=[])
        n2.title.text = "Entities Involved in Selected Files"

        if inds == []:
            return

        selected = []
        nodes = []
        edges = []
        r_per = []
        r_pla = []
        r_org = []
        t_t_edges = []
        senti = {}
        s_color = {}
        for i in inds:
            f = v_source.data["views"][i]
            selected.append(f)
            v = df.loc[df["filename"] == f]["all_nodes"].values
            n = ast.literal_eval(v[0])
            nodes = list(set(nodes).union(n))
            e = ast.literal_eval(df.loc[df["filename"] == f]["all_edges"].values[0])
            edges = list(set(edges).union(e))
            t_t_e = ast.literal_eval(df.loc[df["filename"] == f]["file_neighbors_edge"].values[0])
            t_t_edges = list(set(t_t_edges).union(t_t_e))
            r_person = df.loc[df["filename"] == f]["Person"].values[0]
            if r_person == "None":
                r_person = []
            else:
                r_person = ast.literal_eval(r_person)
            r_place = df.loc[df["filename"] == f]["Place"].values[0]
            if r_place == "None":
                r_place = []
            else:
                r_place = ast.literal_eval(r_place)
            r_organization = df.loc[df["filename"] == f]["Org"].values[0]
            if r_organization == "None":
                r_organization = []
            else:
                r_organization = ast.literal_eval(r_organization)
            r_per = list(set(r_per).union(r_person))
            r_pla = list(set(r_pla).union(r_place))
            r_org = list(set(r_org).union(r_organization))

        t_t = list(itertools.combinations(selected, 2))
        for t in t_t:
            if (t in t_t_edges) or ((t[1], t[0]) in t_t_edges):
                edges.append(t)

        for node in nodes:
            if node in r_per:
                s_color[node] = s_per_df.loc[s_per_df["Entity"] == node]["Sentiment Color"].values[0]
                senti[node] = s_per_df.loc[s_per_df["Entity"] == node]["Sentiment"].values[0]
            elif node in r_pla:
                s_color[node] = s_pla_df.loc[s_pla_df["Entity"] == node]["Sentiment Color"].values[0]
                senti[node] = s_pla_df.loc[s_pla_df["Entity"] == node]["Sentiment"].values[0]
            elif node in r_org:
                s_color[node] = s_org_df.loc[s_org_df["Entity"] == node]["Sentiment Color"].values[0]
                senti[node] = s_org_df.loc[s_org_df["Entity"] == node]["Sentiment"].values[0]
            elif node in selected:
                s_color[node] = df.loc[df["filename"] == node]["sentiment_color"].values[0]
                senti[node] = df.loc[df["filename"] == node]["sentiment"].values[0]
            elif "-Person" in node:
                s_color[node] = "#f441e8"
                senti[node] = "Person Parent Node"
            elif "-Place" in node:
                s_color[node] = "#e2f441"
                senti[node] = "Place Parent Node"
            elif "-Org" in node:
                s_color[node] = "#f49141"
                senti[node] = "Org Parent Node"

        new_dict = create_graph(nodes, edges, s_color, senti)
        s3.data = new_dict["s1"]
        s4.data = new_dict["s2"]
        n2.title.text = "Entities Involved in Selected Files - " + ", ".join(selected)
        return

    c2.data_source.on_change("selected", update_network3)

    print("Done")

    l = column(button_group,row(radviz,scatter),row(n1,n2))

    show(l)

    curdoc().add_root(l)
예제 #14
0
                         ticker=BasicTicker(desired_num_ticks=len(colors)),
                         formatter=PrintfTickFormatter(format="%d%%"),
                         label_standoff=6,
                         border_line_color=None,
                         location=(0, 0))
    p.add_layout(color_bar, 'right')

    # page layout
    desc = Div(text=open(join(dirname(__file__), "description.html")).read(),
               width=1000)
    footer = Div(text=open(join(dirname(__file__), "footer.html"),
                           encoding="utf8").read(),
                 width=1200)

    sizing_mode = 'fixed'

    l = layout([[desc], [row(property, radio_button_group)],
                [row(p, data_table)], [footer]],
               sizing_mode=sizing_mode)

    curdoc().add_root(l)


# callbacks
property.on_change('value', select_property)
radio_button_group.on_change('active', select_property)

curdoc().title = 'Commercial Fragment Libraries'

update()
예제 #15
0
		s3ysd = s3yqresult.iloc[:, (rbg2 + 3)]
		
	s3ysdplus = [bar + sd / (2 * sd_factor) for bar, sd in zip(s3y, s3ysd)]
	s3ysdminus = [bar - sd / (2 * sd_factor) for bar, sd in zip(s3y, s3ysd)]	
	
	s1.data = dict(x = x, y = s1y, sdplus = s1sdplus, sdminus = s1sdminus, 
	               color = color)
	p1.title.text = rbg1labels[rbg1]
		
	s2.data = dict(x = x, y = s2y, sdplus = s2sdplus, sdminus = s2sdminus, 
	               color = color)
	p2.title.text = qName + ' ' + rbg2labels[rbg2]
	
	s3.data = dict(x = s3x, xsdplus = s3xsdplus, xsdminus = s3xsdminus,
                   y = s3y, ysdplus = s3ysdplus, ysdminus = s3ysdminus, 
				   color = color)
	p3.title.text = 'Plot of ' + y3name + ' vs. ' + x3name + ' ' + rbg2labels[rbg2]
	p3.xaxis.axis_label = x3name
	p3.yaxis.axis_label = y3name
	
kneighb.on_change('value', update_data)
radio_button_group1.on_change('active', update_data)
radio_button_group2.on_change('active', update_data)
dropdown.on_change('value', update_data)
x3drop.on_change('value', update_data)
y3drop.on_change('value', update_data)

#add to document
curdoc().add_root(layout([[w, p1], [p3, p2]], sizing_mode = 'scale_width'))
curdoc().title = "NN Explorer"
예제 #16
0
# here one can choose arbitrary input function
default_function_input = TextInput(value=fs.function_input_init)
default_function_period_start = TextInput(title='period start', value=fs.timeinterval_start_init)
default_function_period_end = TextInput(title='period end', value=fs.timeinterval_end_init)

# slider controlling degree of the fourier series
degree = Slider(title="degree", name='degree', value=fs.degree_init, start=fs.degree_min,
                end=fs.degree_max, step=fs.degree_step)

# initialize callback behaviour
degree.on_change('value', degree_change)
default_function_input.on_change('value',
                                 type_input_change)  # todo write default functions for any callback, like above
default_function_period_start.on_change('value', type_input_change)
default_function_period_end.on_change('value', type_input_change)
sample_function_type.on_change('active', type_input_change)

# initialize plot
toolset = "crosshair,pan,reset,resize,save,wheel_zoom"
# Generate a figure container
plot = Figure(plot_height=fs.resolution,
              plot_width=fs.resolution,
              tools=toolset,
              title="Fourier Series Approximation",
              x_range=[fs.x_min, fs.x_max],
              y_range=[fs.y_min, fs.y_max]
              )
# Plot the line by the x,y values in the source property
plot.line('t', 'x_orig', source=source_orig,
          line_width=3,
          line_alpha=0.6,
예제 #17
0
                    item.value = 'best_mt_score'
            except AttributeError:
                continue

    if presets.labels[presets.active] == "Tumor Clonality and Expression":
        for item in widgets:
            try:
                if item.title == 'X-Axis Value':
                    item.value = 'tumor_dna_vaf'
                elif item.title == 'Y-Axis Value':
                    item.value = 'tumor_rna_vaf'
            except AttributeError:
                continue

#Add callbacks to the 3 widgets manually created back at the start
x_field.on_change('value', lambda a,r,g: update())
y_field.on_change('value', lambda a,r,g: update())
presets.on_change('active', lambda a,r,g: change_preset())
hide_null.on_click(lambda arg: update())

#Add all models and widgets to the document
box = widgetbox(*widgets, sizing_mode='stretch_both')
fig = column(
    row(box, p),
    table,
    sizing_mode='scale_width'
)
update() #initial update
curdoc().add_root(fig)
curdoc().title = sample
예제 #18
0
    else:
        if button_group2.active == 1:
            print (slider.value)
            plotToRemove = curdoc().get_model_by_name('plot')
            listOfSubLayouts.remove(plotToRemove)
            team = map_classes.Team(search_bar.value)
            nextyear = str(slider.value+1)
            years = str(slider.value)+'-'+nextyear[2:4]
            p2 = team.hex_freq(season=years)
            plotToAdd = p2
            listOfSubLayouts.append(plotToAdd)
        else:
            print (slider.value)
            plotToRemove = curdoc().get_model_by_name('plot')
            listOfSubLayouts.remove(plotToRemove)
            team = map_classes.Team(search_bar.value)
            nextyear = str(slider.value+1)
            years = str(slider.value)+'-'+nextyear[2:4]
            p2 = team.hex_accuracy(years)
            plotToAdd = p2
            listOfSubLayouts.append(plotToAdd)

#Set up callback event behavior
""" These define the attribute of each widget that is continiously checked and defines the callback functions to be run when the atribute is changed by the user."""
search.on_change('clicks', update_search_term, update_year)
button_group2.on_change('active', update_plot_type)
slider.on_change('value',update_year)

#Name the html document
curdoc().title = "GUI"
예제 #19
0
def fit_tab(agg, df_all, df_goal, candidates_data, train_dates, test_dates,
            old_to_new_sources, new_to_old_sources, old_to_new_regions):
    ## Get region names
    region_names_new = list(old_to_new_regions.values())
    region_names_new.sort()

    ## Pick gold standard and data source
    gs_name = 'AMAZONAS-VE'
    source_set = 'Best'
    if source_set == 'Best':
        agg_source_best = agg.loc[(
            agg.Region == old_to_new_regions[gs_name[:-3]]),
                                  'BestSource'].values[0]
        source_set = new_to_old_sources[agg_source_best]

    dates_train, dates_test, gs_ts,in_sample_forecast_ts,OOS_forecast_ts,\
            r_squared_in_sample, r_squared_OOS = get_graph_data(\
                df_all,agg,df_goal,candidates_data,gs_name,source_set,
                train_dates,test_dates,old_to_new_regions,old_to_new_sources)

    ### Create Bokeh graph
    all_dates = pd.to_datetime(gs_ts.index.tolist())
    dates_train_plot = pd.to_datetime(dates_train)
    dates_test_plot = pd.to_datetime(dates_test)
    #xs = [dates_train,dates_test,dates_train,dates_test]
    xs = [all_dates, dates_train_plot, dates_test_plot]
    ys = [np.array(gs_ts), in_sample_forecast_ts, OOS_forecast_ts]
    source = ColumnDataSource(data=dict(
        x=xs,
        y=ys,
        color=(Category10[3])[0:len(xs)],
        group=['Gold Standard', 'Forecast In Sample', 'Forecast OOS']))
    TOOLS = "pan,wheel_zoom,box_zoom,reset,hover,save"
    p_ts = figure(
        plot_width=800,
        plot_height=500,
        x_axis_type='datetime',
        title='',
        tools=TOOLS,
    )
    p_ts.multi_line(xs='x',
                    ys='y',
                    legend='group',
                    source=source,
                    line_color='color')

    p_ts.legend.location = (0, 350)

    ## Radio buttons
    # Define buttons
    sourceset_display = ['Best'] + list(old_to_new_sources.keys())[1:]
    #sourceset_display = ['Best'] + sourceset_names_old[1:]
    source_set_button_plot = RadioButtonGroup(labels=sourceset_display,
                                              active=0)
    regions_button_plt = RadioButtonGroup(labels=region_names_new, active=0)

    # Text above graph
    div_text = 'In and Out of Sample fit <br>Gold Standard: ' + \
        old_to_new_regions[gs_name[:-3]] + ' - Predictor Set: ' + \
        old_to_new_sources[source_set] + '<br>' + \
        'In Sample R<sup>2</sup>: ' + np.str(np.round(r_squared_in_sample,2)) + \
        ' - ' + 'Out of Sample R<sup>2</sup>: ' + np.str(np.round(r_squared_OOS,2))
    div = Div(text=div_text, width=700, height=100)

    # Update function
    def plot_callback(attr, old, new):
        # Get new selected value
        #new_score = score_types[new.active]
        gs_name_selected = region_names_new[regions_button_plt.active]
        if gs_name_selected == 'Distrito Federal':
            gs_name = 'DTTOMETRO-VE'
        else:
            gs_name = gs_name_selected.replace(' ', '').upper() + '-VE'

        source_set = sourceset_display[source_set_button_plot.active]
        if source_set == 'Best':
            agg_source_best = agg.loc[(
                agg.Region == old_to_new_regions[gs_name[:-3]]),
                                      'BestSource'].values[0]
            source_set = new_to_old_sources[agg_source_best]

        # Get data to update graph
        dates_train, dates_test, gs_ts,in_sample_forecast_ts,OOS_forecast_ts,\
            r_squared_in_sample, r_squared_OOS = get_graph_data(\
                df_all,agg,df_goal,candidates_data,gs_name,source_set,
                train_dates,test_dates,old_to_new_regions,old_to_new_sources)

        ### Create Bokeh graph
        all_dates = pd.to_datetime(gs_ts.index.tolist())
        dates_train_plot = pd.to_datetime(dates_train)
        dates_test_plot = pd.to_datetime(dates_test)
        #xs = [dates_train,dates_test,dates_train,dates_test]
        xs = [all_dates, dates_train_plot, dates_test_plot]
        ys = [np.array(gs_ts), in_sample_forecast_ts, OOS_forecast_ts]
        #new_data = pd.DataFrame({'x':xs,'y':ys,'color':(Category10[3])[0:len(xs)]})

        new_source = ColumnDataSource(data=dict(
            x=xs,
            y=ys,
            color=(Category10[3])[0:len(xs)],
            group=['Gold Standard', 'Forecast In Sample', 'Forecast OOS']))
        source.data = new_source.data
        # p_ts.source = new_source

        new_text = 'In and Out of Sample fit <br>Gold Standard :' + \
            old_to_new_regions[gs_name[:-3]] + ' - Predictor Set: ' + \
            old_to_new_sources[source_set] + '<br>' + \
            'In Sample R<sup>2</sup>: ' + np.str(np.round(r_squared_in_sample,2)) + \
            '<br>Out of Sample R<sup>2</sup>: ' + np.str(np.round(r_squared_OOS,2))
        div.text = new_text

    source_set_button_plot.on_change('active', plot_callback)
    regions_button_plt.on_change('active', plot_callback)

    # Put controls in a single element
    controls = WidgetBox(regions_button_plt, source_set_button_plot)

    # Create a row layout
    layout = column(controls, div, p_ts)

    # Make a tab with the layout
    tab = Panel(child=layout, title='Regression Fit')

    return tab
            new_source = bats_man_data[
                (bats_man_data['Batsman'] == selected_batsman)
                & (bats_man_data['Year_bats'] == yr1) &
                (bats_man_data['Opposition_bats'] == selected_oppo)].groupby([
                    'Match_ID', '6s'
                ])['Runs'].sum().sort_values().reset_index().sort_values(
                    by=['Match_ID'])
            new_source.rename({'6s': 'shots'}, axis='columns', inplace=True)

    new_runs_cds = ColumnDataSource(new_source[['Match_ID', 'Runs']])
    new_shots_cds = ColumnDataSource(new_source[['Match_ID', 'shots']])
    runs_cds.data = new_runs_cds.data
    shots_cds.data = new_shots_cds.data

    fig.title.text = '%s Performance Across the Seasons in the Year %d' % (
        selected_batsman, yr)
    fig.x_range.factors = []
    fig.x_range.factors = list(np.sort(new_source['Match_ID'].unique()))


run_type.on_change('active', lambda attr, old, new: update(attr, old, new))
slider.on_change('value', update)
radiogroup_batsman.on_change('active', update)
radiogroup_oppo.on_change('active', update)

# Make a row layout of widgetbox(slider) and plot and add it to the current document
layout = row(column(widgetbox(slider), widgetbox(radiogroup_batsman)), fig,
             column(widgetbox(run_type), widgetbox(radiogroup_oppo)))

curdoc().add_root(layout)
예제 #21
0
def map_tab(gdf, gdf_country, agg_all_nfolds, score_types_list, sources_list,
            n_folds_list):
    # Prepare coordinates data
    gdf.sort_values(by=['Region'], inplace=True)
    gdf.reset_index(drop=True, inplace=True)
    gdf = geopd_prepare(gdf)

    # Get lis of coordinates of states limits, and their names
    country_xs = gdf.apply(getPolyCoords,
                           geom='geometry',
                           coord_type='x',
                           axis=1)
    country_ys = gdf.apply(getPolyCoords,
                           geom='geometry',
                           coord_type='y',
                           axis=1)
    states = gdf.Region.tolist()

    # Get entire country coordinates and add them to the list
    total_country_x, total_country_y = country_adjust(gdf_country)
    country_xs = country_xs.append(pd.Series(total_country_x),
                                   ignore_index=True)
    country_ys = country_ys.append(pd.Series(total_country_y),
                                   ignore_index=True)
    states.extend(['Total'] * len(total_country_x))

    # List of n_folds values
    n_folds_display = [np.str(x) for x in n_folds_list]

    # Filter data we start with
    score_type_filter = 'InSample'  #'OOS'
    source_filter = 'Best'
    n_folds_map = n_folds_list[0]
    # t = score_types_list[0]
    # s = sources_list[0]
    df_show = agg_all_nfolds.loc[agg_all_nfolds.NbFolds == n_folds_map, :]
    df_show = df_show.loc[df_show.ScoreType == score_type_filter, :]
    if source_filter == 'Best':
        df_show = df_show.loc[df_show['IsBest'], :]
    else:
        df_show = df_show.loc[df_show['SourcesSet'] == source_filter, :]
    df_show_dict = df_show[['Region', 'Value']].set_index('Region').T.to_dict()
    scores = [df_show_dict[x]['Value'] for x in states]

    # Create bokeh map source data
    col_source = ColumnDataSource(
        data=dict(xs=country_xs, ys=country_ys, Region=states, Score=scores))
    # geosource = GeoJSONDataSource(geojson = \
    #     map_all_data[score_type_filter + '|' + source_filter].geojson)

    ## Map formatting ##################
    palette = Viridis256  # brewer["Spectral"][8] # Category20[20] # brewer["PRGn"][11]
    color_mapper = LinearColorMapper(palette=palette, low=-1, high=1)
    color_bar = ColorBar(color_mapper=color_mapper,
                         width=650,
                         height=20,
                         formatter=NumeralTickFormatter(format=",0.00"),
                         border_line_color=None,
                         orientation='horizontal',
                         location=(0, 0))
    hover = HoverTool(tooltips=[('Region', '@Region'), ('Score', '@Score')])

    #Create figure object.
    width = 750
    height = np.int(width * 13 / 15)
    p = figure(title='Venezuela Situational Awareness',
               plot_height=height,
               plot_width=width,
               tools=[hover, "pan,wheel_zoom,box_zoom,reset"],
               toolbar_location="left")

    p.xgrid.grid_line_color = None
    p.ygrid.grid_line_color = None

    #Add patch renderer to figure.
    p.patches('xs',
              'ys',
              source=col_source,
              fill_color={
                  'field': 'Score',
                  'transform': color_mapper
              },
              line_color='white',
              line_width=1,
              fill_alpha=0.7)
    #Specify figure layout.
    p.add_layout(color_bar, 'below')

    ## Radio buttons
    # Define buttons
    n_folds_button = RadioButtonGroup(labels=n_folds_display, active=0)
    score_types = score_types_list.tolist()
    score_type_button = RadioButtonGroup(labels=score_types, active=0)
    data_sources = sources_list.tolist()
    sources_button = RadioButtonGroup(labels=data_sources, active=0)

    ###########################################################################
    # Update function
    def score_type_callback(attr, old, new):
        # Get new score data to display
        # Filter data we start with
        n_folds_map_new = np.int(n_folds_display[n_folds_button.active])
        new_score = score_types[score_type_button.active]
        new_source = data_sources[sources_button.active]

        df_show = agg_all_nfolds.loc[agg_all_nfolds.NbFolds ==
                                     n_folds_map_new, :]
        df_show = df_show.loc[df_show.ScoreType == new_score, :]
        if new_source == 'Best':
            df_show = df_show.loc[df_show['IsBest'], :]
        else:
            df_show = df_show.loc[df_show['SourcesSet'] == new_source, :]
        df_show_dict = df_show[['Region',
                                'Value']].set_index('Region').T.to_dict()
        new_scores = [df_show_dict[x]['Value'] for x in states]

        # Patch source data with new scores
        col_source.patch({'Score': [(slice(len(new_scores)), new_scores)]})

    ###########################################################################
    # Update events
    n_folds_button.on_change('active', score_type_callback)
    score_type_button.on_change('active', score_type_callback)
    sources_button.on_change('active', score_type_callback)

    # Figure formatting
    set_stype(p)

    # Put controls in a single element
    controls = WidgetBox(n_folds_button, score_type_button, sources_button)

    # Create a row layout
    layout = column(controls, p)

    # Make a tab with the layout
    tab = Panel(child=layout, title='Map of Scores')

    return tab

    # In Notebook (without callbacks)
    #output_notebook()
    #show(p)

    # In Spyder (without callbacks)
    # output_file('foo.html')
    # show(column(widgetbox(score_type_button),p),browser="chrome")

    # To run from command (in the folder of the file) using the command
    # bokeh serve --show situ_map.py
    # curdoc().add_root(column(score_type_button,sources_button,p))
    # curdoc().title = "Venezuela Situational Awareness"


###############################################################################
예제 #22
0
PROPERTY_TYPE_HOST = Select(title='Property Type:',
                            value=c.EMPTY_STRING,
                            options=[c.EMPTY_STRING] + FILTER_PROPERTIES[c.PT])
N_HOST = Select(title='Neighbourhood:',
                value=c.EMPTY_STRING,
                options=[c.EMPTY_STRING] + FILTER_PROPERTIES[c.NC])
NG_HOST = Select(title='Neighbourhood Group:',
                 value=c.EMPTY_STRING,
                 options=[c.EMPTY_STRING] + NG_LIST)
ROOM_TYPE_HOST = Select(title='Room Type:',
                        value=c.EMPTY_STRING,
                        options=[c.EMPTY_STRING] + RT_LIST)

# Radio Button Widget
USER_TYPE = RadioButtonGroup(labels=['Guest', 'Host'], active=0)
USER_TYPE.on_change(c.ACTIVE, update_layout)

# Button Toggle Widget
PREDICT_VALUE = Toggle(label='Submit', button_type='success')
PREDICT_VALUE.on_click(predict_price)

# Text Widget
HOST_PRICE = Paragraph(text="""Select all listing values and press
                        submit to view your listings valued price.""",
                       width=500,
                       height=500)

WIDGETS_BOTH_1 = [USER_TYPE, CITY_INPUT]
WIDGETS_BOTH_2 = [
    ACCOMMODATES_SLIDER, BEDROOM_SLIDER, BED_SLIDER, BATHROOM_SLIDER
]
예제 #23
0
def buildPlot():
    #####################Setup
    # Grab graph colors, pop undesireable ones
    colors = SEABORN_PALETTES['bright']

    #Grab and sort the FQs
    quals = fruit_df.reset_index()
    quals = quals['FruitQuality'].unique().tolist()
    for idx, i in enumerate(list(quals)):
        if type(i) == type(0.5):
            quals.pop(idx)
    unique_FQs = quals

    #a little math to get the epoch time to set the initial x range
    minDate = ts_to_epoch(fruit_df['Date'].min())
    maxDate = ts_to_epoch(fruit_df['Date'].max())

    ###########Create and format the plot
    plot = figure(
        x_axis_type="datetime",
        plot_width=600,
        plot_height=400,
        tools=[PanTool(),
               WheelZoomTool(),
               SaveTool(),
               BoxZoomTool()],
        x_range=DataRange1d(
            start=minDate, end=maxDate
        ),  #sets the initial date range  to the limits of the data
        y_range=DataRange1d(start=0, end=1),
        name='the_plot',
        toolbar_location='above')
    #some styling
    plot.title.text = "Historical Volatility"
    plot.xaxis.axis_label = "Trade Date"
    plot.yaxis.axis_label = "Vol"
    plot.background_fill_color = '#EAEBF0'
    plot.xgrid.grid_line_color = 'white'
    plot.ygrid.grid_line_color = 'white'
    plot.xaxis.axis_line_color = 'white'
    plot.xaxis.major_tick_line_color = 'white'
    plot.xaxis.minor_tick_line_color = 'white'
    plot.yaxis.axis_line_color = 'white'
    plot.yaxis.major_tick_line_color = 'white'
    plot.yaxis.minor_tick_line_color = 'white'
    plot.toolbar.logo = None

    #a list for all of the lines to reside in
    lines = []
    legends = []

    ##############Create the widgets

    #a console style window to show debug messages TODO: add on/off functionality
    debug = PreText(text="", width=1200, height=500)

    #echos the debug in a place more visiable for the user
    user_message = Paragraph(text='')

    #Asset_Class, Product, and From dropdown boxes. Sets dropdown's initial value.
    asCls = Select(title="Asset Class",
                   options=ddOpts['Asset_Class'].unique().tolist())
    asCls.value = asCls.options[0]
    prod = Select(title="Products",
                  options=ddOpts[ddOpts['Asset_Class'] == asCls.value]
                  ['Product'].unique().tolist())
    prod.value = prod.options[0]
    whereFrom = Select(title="From",
                       options=ddOpts[(ddOpts['Asset_Class'] == asCls.value)
                                      & (ddOpts['Product'] == prod.value)]
                       ['From'].unique().tolist())
    whereFrom.value = whereFrom.options[0]
    FQslider = Slider(title='Fruit Quality',
                      start=min(unique_FQs),
                      end=max(unique_FQs),
                      step=1)

    #the amount of days back to look for the data
    days_back = TextInput(title='Days ago', value='365')
    days_back_buttons = RadioButtonGroup(
        labels=['10', '30', '90', '180', '365', '730'], active=4)

    #the date to linear fit to
    fixed_date_buttons = RadioButtonGroup(
        labels=['30', '60', '90', '120', '180', '365'], active=2)
    fixed_date = TextInput(title='Days to Exp', value='90')

    #the amount of days with which to calculate the rolling mean
    rolling_days_buttons = RadioButtonGroup(labels=['1', '2', '5', '10'],
                                            active=0)
    rolling_days = TextInput(title='Rolling Mean Days', value='1')

    #a dynamically resizing checkbox group that allows for the changing of the visablity of any line on the plot
    line_onOff = CheckboxGroup(width=400, name='line_onOff')

    #the associated colors to act as a legend for line_onOff
    legendDiv = Div(width=50)

    #button to add a line
    addLine = Button(label="Add Line")

    #an html rendered visualization of the data for each line
    descriptions = Div(text='', width=500)

    #resizes the plot
    rszButton = Button(label='resize')

    ##########Define functions associated with the widgets

    #concats any dubug call to the end of the current debug text, and changes the user message
    def updateDebug(inString):
        inString = str(inString)
        user_message.text = inString
        oldText = debug.text
        newText = ("*- " + str(datetime.now()) + " : " + inString)
        debug.text = oldText + '\n' + newText

    #changes the potential products and contract categories to match the user selected asset class
    def asClsChange(attrname, old, new):
        prod.options = ddOpts[ddOpts['Asset_Class'] ==
                              asCls.value]['Product'].unique().tolist()
        prod.value = prod.options[0]

    #changes the potential contract categories to match the user selected product
    def prodChange(attrname, old, new):
        whereFrom.options = ddOpts[(ddOpts['Asset_Class'] == asCls.value) & (
            ddOpts['Product'] == prod.value)]['From'].unique().tolist()
        whereFrom.value = whereFrom.options[0]

    #links the days back button and text box
    def days_back_buttonChange(attrname, old, new):
        days_back.value = days_back_buttons.labels[days_back_buttons.active]

    #checks that the users input is an int
    def days_backChange(attrname, old, new):
        try:
            days_back.value = str(int(days_back.value))
        except ValueError:
            days_back.value = '0'
            updateDebug('please type an integer')

    #links the fixed date button and text box
    def fixed_date_buttonChange(attrname, old, new):
        fixed_date.value = fixed_date_buttons.labels[fixed_date_buttons.active]

    #checks that the users input is an int
    def fixed_dateChange(attrname, old, new):
        try:
            fixed_date.value = str(int(fixed_date.value))
        except ValueError:
            fixed_date.value = '0'
            updateDebug('please type an integer')

    #links the rolling days button and text box
    def rolling_days_buttonsChange(attrname, old, new):
        rolling_days.value = rolling_days_buttons.labels[
            rolling_days_buttons.active]

    #checks that the users input is an int
    def rolling_daysChange(attrname, old, new):
        try:
            rolling_days.value = str(int(rolling_days.value))
        except ValueError:
            rolling_days.value = '0'
            updateDebug('please type an integer')

    #fits the plot to the currently visiable lines
    def resize():
        if len(line_onOff.active) == 0 or len(line_onOff.labels) == 0:

            plot.x_range.start = ts_to_epoch(fruit_df['Date'].min())
            plot.x_range.end = ts_to_epoch(fruit_df['Date'].max())
            plot.y_range.start = 0
            plot.y_range.end = 100
        else:
            xmin, xmax, ymin, ymax = calc_range(lines)
            plot.x_range.start = xmin
            plot.x_range.end = xmax
            plot.y_range.start = ymin
            plot.y_range.end = ymax

    #turn lines on or off
    def line_onOffChange(attrname, old, new):
        for i in range(len(line_onOff.labels)):
            if i in line_onOff.active:
                lines[i].glyph.visible = True
            else:
                lines[i].glyph.visible = False
        legendDiv.text = '<div>'
        for line in lines:
            legendDiv.text += '<br><div style="background-color: %s; float:up; padding: 4px 4px 4px 4px"></div><br>' % line.glyph.line_color
        legendDiv.text += '</div>'
        resize()

    #adds a line to the graph
    def grphUpdt():
        #adds some debug messages, grabs the current time as to later show the total time taken to calculate
        updateDebug("Starting")
        updateDebug("total dataframe size: " + str(fruit_df.shape))
        stTime = datetime.now()

        #the value to linear fit to
        fit_to = int(fixed_date.value)

        #instiantiate an empty dataframe that will eventually contain the graphs data
        graphData = pd.DataFrame({
            'Date': [],
            'PriceVolatility': [],
            'Days_to_Exp': []
        })

        #grab the appropriate subset of the whole dataframe based on the users input into the widgets
        updateDebug("querying the data..")

        try:
            workingDf = fruit_df.loc[asCls.value, prod.value, whereFrom.value]
        except KeyError:
            updateDebug(
                'no data with that combination of Asset Class, Product, From')
            return

        try:
            workingDf = workingDf[[
                'Date', 'PriceVolatility', 'Days_to_Exp'
            ]][(workingDf['Date'] >
                (date.today() - timedelta(days=int(days_back.value))))]
        except KeyError:
            updateDebug(
                'no data with that combination of Asset Class, Product, From, and days back'
            )
            return
        updateDebug("done breaking down df")

        #a hook in the case that the users inputs resulted in an empty dataframe
        if (workingDf.empty):
            updateDebug(
                'no data with that combination of Asset Class, Product, From, and days back'
            )
            return

        #widdle down the database to only contain the user specified FQ
        try:
            graphData = workingDf.loc[int(FQslider.value)].copy()
        except KeyError:
            updateDebug('no data with that FQ')

        #another empty graph hook
        if (graphData.empty):
            updateDebug(
                'no data with that combination of Asset Class, Product, Contract Category, FQ, and days back'
            )
            return
        updateDebug('grabed correct FQs')

        #calculate linear fit on the current subset
        updateDebug('calculating linear fit...')
        graphData = mu.linearFit(fit_to=fit_to,
                                 group_on_column='Date',
                                 df=graphData,
                                 fit_column='Days_to_Exp',
                                 on_columns=['PriceVolatility'])
        updateDebug('finished with linear fit')

        # a few more debug messages
        updateDebug(
            "working df qry: Asset_Class = %s and Product = %s and From = %s and Date > %s "
            % (asCls.value, prod.value, whereFrom.value,
               str(date.today() - timedelta(days=int(days_back.value)))))
        updateDebug("graph data shape: " + str(workingDf.shape))

        #makes sure graph data has at least 5 rows, so that rolling mean can be calculated
        if graphData.shape[0] > int(rolling_days.value):

            #make the graph legend, based on if there's a denominator specified or not
            this_legend = '%s - %s FQ: %s Days to Exp: %s From: %s Rolling Days: %s' % (
                prod.value, whereFrom.value, int(
                    FQslider.value), fixed_date.value,
                str(date.today() - timedelta(days=int(days_back.value))),
                rolling_days.value)

            #add a new line to the graph, and add the accosiated GlyphRenderer created by adding the line to the lines list.
            #Set the legend to the previously calculated legend, and set the color to the next color in the current theme (if there are more lines than colors, there will be multiple lines with the same color)
            #Calculates a 5 day rolling mean on the y values. Maybe add a slider/text box/other widget so the user can set the rolling mean themselves
            updateDebug('adding line to plot')
            lines.append(
                plot.line(graphData.index.values[int(rolling_days.value) - 1:],
                          graphData['PriceVolatility'].rolling(
                              window=int(rolling_days.value)).mean()
                          [int(rolling_days.value) - 1:],
                          line_width=3,
                          color=colors[len(lines) % len(colors)]))
            legends.append(this_legend)
            updateDebug("updated graph")

            global descDf

            #either creates, or adds to, a dataframe containing statistics about the data. stats come from pandas DataFrame.describe.
            if descDf is None:
                graphData[this_legend] = graphData['PriceVolatility']
                descDf = graphData[[
                    this_legend
                ]].rolling(window=int(rolling_days.value)).mean(
                )[int(rolling_days.value) -
                  1:].describe(percentiles=[]).transpose().copy()
            else:
                graphData[this_legend] = graphData['PriceVolatility']
                descDf = pd.concat([
                    descDf, graphData[[
                        this_legend
                    ]].rolling(window=int(rolling_days.value)).mean()
                    [int(rolling_days.value) -
                     1:].describe(percentiles=[]).transpose().copy()
                ])

            descDf = descDf.round(1)
            descriptions.text = descDf.to_html().replace('\\n', '')
            graphData.drop(this_legend, 1, inplace=True)

            #add the name of the line to the checkbox so that it can be turned off and o
            line_onOff.labels.append(this_legend)
            line_onOff.active.append(len(line_onOff.labels) - 1)
            legendDiv.text = '<div>'
            for line in lines:
                legendDiv.text += '<br><div style="background-color: %s; float:up; padding: 4px 4px 4px 4px"></div><br>' % line.glyph.line_color
            legendDiv.text += '</div>'
            ##leaving this in case we get around to figuring out the hover tool
            ##formats the date values for the hover tool, currently commented out until we, or bokeh, fix the hover tool for multiple lines
            #formDates= pd.to_datetime(graphData['Date'] ,format="%m-%d-%Y")
            #lines[-1].data_source.data['formDates'] = formDates.apply(lambda x: x.strftime('%m-%d-%Y'))

            ##Displays the amout of time it took to draw the line, as well as the number of points in the graph
            updateDebug("updated y vals, with rolling mean calculated")
            updateDebug(
                str(datetime.now() - stTime) + " FOR " +
                str(len(lines[-1].data_source.data['x'])) + " points")
        else:
            updateDebug("There's no data to display")
        del graphData
        del workingDf

    #######Link widgets to their associated functions
    asCls.on_change('value', asClsChange)
    prod.on_change('value', prodChange)
    days_back_buttons.on_change('active', days_back_buttonChange)
    days_back.on_change('value', days_backChange)
    fixed_date_buttons.on_change('active', fixed_date_buttonChange)
    fixed_date.on_change('value', fixed_dateChange)
    rolling_days_buttons.on_change('active', rolling_days_buttonsChange)
    rolling_days.on_change('value', rolling_daysChange)
    line_onOff.on_change('active', line_onOffChange)
    addLine.on_click(grphUpdt)
    rszButton.on_click(resize)

    #Formatting
    fixed_date_box = WidgetBox(fixed_date, fixed_date_buttons)
    days_back_box = WidgetBox(days_back, days_back_buttons)
    rolling_days_box = WidgetBox(rolling_days, rolling_days_buttons)
    widgets = [
        asCls, prod, whereFrom, FQslider, days_back_box, fixed_date_box,
        rolling_days_box, addLine, rszButton, user_message
    ]
    plot_w_description = VBox(plot, descriptions, width=700)
    pwd_w_leg = HBox(plot_w_description,
                     VBox(legendDiv),
                     VBox(line_onOff),
                     width=plot_w_description.width + line_onOff.width + 100,
                     name='div_to_save')
    input_box = VBox(*widgets, width=400, height=1200)
    total_box = HBox(VBox(input_box),
                     VBox(pwd_w_leg),
                     width=input_box.width + pwd_w_leg.width + 100,
                     height=1200)
    tot_w_debug = VBox(total_box, VBox(HBox(debug)))

    resize()
    return tot_w_debug
예제 #24
0
        Voltage_WE2.visible = True

"""Callback Assignments"""
callback_update_plot = None
callback_acquire_data_fake = None
Connect.on_click(callback_Connect_eLoaD_BLE)
Save.js_on_click(callback_Save)
Start.on_click(callback_Start)
Random_test.on_click(callback_Random_4)
Gain.on_change('value', update_gain)
Scan_rate.on_change('value', update_scan_rate)
Segments.on_change('value', update_segments)
Voltage_Start.on_change('value', update_v1_start)
Voltage_WE2.on_change('value', update_v2)
Voltage_Window.on_change('value', update_v1_window)
Sweep_direction.on_change('active', update_sweep)
Voltammetry_Mode.on_change('active', update_voltammetry_mode)

#---------------------------#
#    Callbacks (Threads)    #
#---------------------------#
#Plotting has no priority, also slower

@gen.coroutine
#It seems that we cannot make streaming unlocked
def stream():
    global acquiredData
    source.stream(
        {'time': acquiredData['timestamp'], 'raw_data': acquiredData['raw_data']})
    clear_dict(acquiredData)
예제 #25
0

def get_radio():
    """
    chooses which of the returned data columns is plotted 
    """
    options_dict = {0: 'f', 1: 'a0', 2: 'a1', 3: 'a2'}
    return options_dict[data_radios.active]


def radio_callback(attr, old, new):
    update_plot_data()


data_radios = RadioButtonGroup(labels=["f", "a0", "a1", "a2"], active=0)
data_radios.on_change('active', radio_callback)

#######
## GENERATE MANIPLUABLE LINE PLOTS
#######

p = figure(width=600, height=600)
manny_on_the_map = Manipulator([0], [0], "empty", p)


def update_plot_data():
    selectionIndex = data_grabber.file_source.selected.indices[0]
    df = H5Scan(data_grabber.file_source.data['address'][selectionIndex]).df(
        'analysis')
    data_option = get_radio()
    manny_on_the_map.update_data(
예제 #26
0
class DVHs:
    def __init__(self, sources, time_series, correlation, regression,
                 custom_title, data_tables):
        self.sources = sources
        self.time_series = time_series
        self.correlation = correlation
        self.regression = regression

        self.dvh_review_rois = []
        self.query = None

        self.temp_dvh_info = Temp_DICOM_FileSet()
        self.dvh_review_mrns = self.temp_dvh_info.mrn
        if self.dvh_review_mrns[0] != '':
            self.dvh_review_rois = self.temp_dvh_info.get_roi_names(
                self.dvh_review_mrns[0]).values()
            self.dvh_review_mrns.append('')
        else:
            self.dvh_review_rois = ['']

        # Add Current row to source
        self.add_endpoint_row_button = Button(label="Add Endpoint",
                                              button_type="primary",
                                              width=200)
        self.add_endpoint_row_button.on_click(self.add_endpoint)

        self.ep_row = Select(value='', options=[''], width=50, title="Row")
        self.ep_options = [
            "Dose (Gy)", "Dose (%)", "Volume (cc)", "Volume (%)"
        ]
        self.select_ep_type = Select(value=self.ep_options[0],
                                     options=self.ep_options,
                                     width=180,
                                     title="Output")
        self.select_ep_type.on_change('value', self.select_ep_type_ticker)
        self.ep_text_input = TextInput(value='',
                                       title="Input Volume (cc):",
                                       width=180)
        self.ep_text_input.on_change('value', self.ep_text_input_ticker)
        self.ep_units_in = RadioButtonGroup(labels=["cc", "%"],
                                            active=0,
                                            width=100)
        self.ep_units_in.on_change('active', self.ep_units_in_ticker)
        self.delete_ep_row_button = Button(label="Delete",
                                           button_type="warning",
                                           width=100)
        self.delete_ep_row_button.on_click(self.delete_ep_row)

        tools = "pan,wheel_zoom,box_zoom,reset,crosshair,save"
        self.plot = figure(plot_width=1050,
                           plot_height=500,
                           tools=tools,
                           logo=None,
                           active_drag="box_zoom")
        self.plot.min_border_left = options.MIN_BORDER
        self.plot.min_border_bottom = options.MIN_BORDER
        self.plot.add_tools(
            HoverTool(show_arrow=False,
                      line_policy='next',
                      tooltips=[('Label', '@mrn @roi_name'), ('Dose', '$x'),
                                ('Volume', '$y')]))
        self.plot.xaxis.axis_label_text_font_size = options.PLOT_AXIS_LABEL_FONT_SIZE
        self.plot.yaxis.axis_label_text_font_size = options.PLOT_AXIS_LABEL_FONT_SIZE
        self.plot.xaxis.major_label_text_font_size = options.PLOT_AXIS_MAJOR_LABEL_FONT_SIZE
        self.plot.yaxis.major_label_text_font_size = options.PLOT_AXIS_MAJOR_LABEL_FONT_SIZE
        self.plot.yaxis.axis_label_text_baseline = "bottom"
        self.plot.lod_factor = options.LOD_FACTOR  # level of detail during interactive plot events

        # Add statistical plots to figure
        stats_median_1 = self.plot.line(
            'x',
            'median',
            source=sources.stats_1,
            line_width=options.STATS_1_MEDIAN_LINE_WIDTH,
            color=options.GROUP_1_COLOR,
            line_dash=options.STATS_1_MEDIAN_LINE_DASH,
            alpha=options.STATS_1_MEDIAN_ALPHA)
        stats_mean_1 = self.plot.line(
            'x',
            'mean',
            source=sources.stats_1,
            line_width=options.STATS_1_MEAN_LINE_WIDTH,
            color=options.GROUP_1_COLOR,
            line_dash=options.STATS_1_MEAN_LINE_DASH,
            alpha=options.STATS_1_MEAN_ALPHA)
        stats_median_2 = self.plot.line(
            'x',
            'median',
            source=sources.stats_2,
            line_width=options.STATS_2_MEDIAN_LINE_WIDTH,
            color=options.GROUP_2_COLOR,
            line_dash=options.STATS_2_MEDIAN_LINE_DASH,
            alpha=options.STATS_2_MEDIAN_ALPHA)
        stats_mean_2 = self.plot.line(
            'x',
            'mean',
            source=sources.stats_2,
            line_width=options.STATS_2_MEAN_LINE_WIDTH,
            color=options.GROUP_2_COLOR,
            line_dash=options.STATS_2_MEAN_LINE_DASH,
            alpha=options.STATS_2_MEAN_ALPHA)

        # Add all DVHs, but hide them until selected
        self.plot.multi_line('x',
                             'y',
                             source=sources.dvhs,
                             selection_color='color',
                             line_width=options.DVH_LINE_WIDTH,
                             alpha=0,
                             line_dash=options.DVH_LINE_DASH,
                             nonselection_alpha=0,
                             selection_alpha=1)

        # Shaded region between Q1 and Q3
        iqr_1 = self.plot.patch('x_patch',
                                'y_patch',
                                source=sources.patch_1,
                                alpha=options.IQR_1_ALPHA,
                                color=options.GROUP_1_COLOR)
        iqr_2 = self.plot.patch('x_patch',
                                'y_patch',
                                source=sources.patch_2,
                                alpha=options.IQR_2_ALPHA,
                                color=options.GROUP_2_COLOR)

        # Set x and y axis labels
        self.plot.xaxis.axis_label = "Dose (Gy)"
        self.plot.yaxis.axis_label = "Normalized Volume"

        # Set the legend (for stat dvhs only)
        legend_stats = Legend(items=[("Median", [stats_median_1]),
                                     ("Mean", [stats_mean_1]),
                                     ("IQR", [iqr_1]),
                                     ("Median", [stats_median_2]),
                                     ("Mean", [stats_mean_2]),
                                     ("IQR", [iqr_2])],
                              location=(25, 0))

        # Add the layout outside the plot, clicking legend item hides the line
        self.plot.add_layout(legend_stats, 'right')
        self.plot.legend.click_policy = "hide"

        self.download_endpoints_button = Button(label="Download Endpoints",
                                                button_type="default",
                                                width=150)
        self.download_endpoints_button.callback = CustomJS(
            args=dict(source=sources.endpoint_calcs),
            code=open(join(dirname(__file__), "download_endpoints.js")).read())

        # Setup axis normalization radio buttons
        self.radio_group_dose = RadioGroup(
            labels=["Absolute Dose", "Relative Dose (Rx)"],
            active=0,
            width=200)
        self.radio_group_dose.on_change('active', self.radio_group_ticker)
        self.radio_group_volume = RadioGroup(
            labels=["Absolute Volume", "Relative Volume"], active=1, width=200)
        self.radio_group_volume.on_change('active', self.radio_group_ticker)

        # Setup selectors for dvh review
        self.select_reviewed_mrn = Select(title='MRN to review',
                                          value='',
                                          options=self.dvh_review_mrns,
                                          width=300)
        self.select_reviewed_mrn.on_change('value',
                                           self.update_dvh_review_rois)

        self.select_reviewed_dvh = Select(title='ROI to review',
                                          value='',
                                          options=[''],
                                          width=360)
        self.select_reviewed_dvh.on_change('value',
                                           self.select_reviewed_dvh_ticker)

        self.review_rx = TextInput(value='', title="Rx Dose (Gy):", width=170)
        self.review_rx.on_change('value', self.review_rx_ticker)

        if options.LITE_VIEW:
            self.layout = column(
                Div(text="<b>DVH Analytics v%s</b>" % options.VERSION),
                row(self.radio_group_dose,
                    self.radio_group_volume), self.add_endpoint_row_button,
                row(self.ep_row, Spacer(width=10), self.select_ep_type,
                    self.ep_text_input, Spacer(width=20),
                    self.ep_units_in, self.delete_ep_row_button,
                    Spacer(width=50), self.download_endpoints_button),
                data_tables.ep)
        else:
            self.layout = column(
                Div(text="<b>DVH Analytics v%s</b>" % options.VERSION),
                row(custom_title['1']['dvhs'], Spacer(width=50),
                    custom_title['2']['dvhs']),
                row(self.radio_group_dose, self.radio_group_volume),
                row(self.select_reviewed_mrn, self.select_reviewed_dvh,
                    self.review_rx), self.plot,
                Div(text="<b>DVHs</b>", width=1200), data_tables.dvhs,
                Div(text="<hr>", width=1050),
                Div(text="<b>Define Endpoints</b>",
                    width=1000), self.add_endpoint_row_button,
                row(self.ep_row, Spacer(width=10), self.select_ep_type,
                    self.ep_text_input, Spacer(width=20),
                    self.ep_units_in, self.delete_ep_row_button,
                    Spacer(width=50), self.download_endpoints_button),
                data_tables.ep, Div(text="<b>DVH Endpoints</b>",
                                    width=1200), data_tables.endpoints)

    def add_endpoint(self):
        if self.sources.endpoint_defs.data['row']:
            temp = self.sources.endpoint_defs.data

            for key in list(temp):
                temp[key].append('')
            temp['row'][-1] = len(temp['row'])
            self.sources.endpoint_defs.data = temp

            new_options = [str(x + 1) for x in range(len(temp['row']))]
            self.ep_row.options = new_options
            self.ep_row.value = new_options[-1]
        else:
            self.ep_row.options = ['1']
            self.ep_row.value = '1'
            self.sources.endpoint_defs.data = dict(row=['1'],
                                                   output_type=[''],
                                                   input_type=[''],
                                                   input_value=[''],
                                                   label=[''],
                                                   units_in=[''],
                                                   units_out=[''])
            if not self.ep_text_input.value:
                self.ep_text_input.value = '1'

        self.update_ep_source()

        clear_source_selection(self.sources, 'endpoint_defs')

    def update_ep_source(self):
        if self.ep_row.value:

            r = int(self.ep_row.value) - 1

            if 'Dose' in self.select_ep_type.value:
                input_type, output_type = 'Volume', 'Dose'
                if '%' in self.select_ep_type.value:
                    units_out = '%'
                else:
                    units_out = 'Gy'
                units_in = ['cc', '%'][self.ep_units_in.active]
                label = "D_%s%s" % (self.ep_text_input.value, units_in)
            else:
                input_type, output_type = 'Dose', 'Volume'
                if '%' in self.select_ep_type.value:
                    units_out = '%'
                else:
                    units_out = 'cc'
                units_in = ['Gy', '%'][self.ep_units_in.active]
                label = "V_%s%s" % (self.ep_text_input.value, units_in)

            try:
                input_value = float(self.ep_text_input.value)
            except:
                input_value = 1

            patch = {
                'output_type': [(r, output_type)],
                'input_type': [(r, input_type)],
                'input_value': [(r, input_value)],
                'label': [(r, label)],
                'units_in': [(r, units_in)],
                'units_out': [(r, units_out)]
            }

            self.sources.endpoint_defs.patch(patch)
            self.update_source_endpoint_calcs()

    def ep_units_in_ticker(self, attr, old, new):
        if self.query.allow_source_update:
            self.update_ep_text_input_title()
            self.update_ep_source()

    def update_ep_text_input_title(self):
        if 'Dose' in self.select_ep_type.value:
            self.ep_text_input.title = "Input Volume (%s):" % [
                'cc', '%'
            ][self.ep_units_in.active]
        else:
            self.ep_text_input.title = "Input Dose (%s):" % [
                'Gy', '%'
            ][self.ep_units_in.active]

    def select_ep_type_ticker(self, attr, old, new):
        if self.query.allow_source_update:
            if 'Dose' in new:
                self.ep_units_in.labels = ['cc', '%']
            else:
                self.ep_units_in.labels = ['Gy', '%']

            self.update_ep_text_input_title()
            self.update_ep_source()

    def ep_text_input_ticker(self, attr, old, new):
        if self.query.allow_source_update:
            self.update_ep_source()

    def delete_ep_row(self):
        if self.ep_row.value:
            new_ep_source = self.sources.endpoint_defs.data
            index_to_delete = int(self.ep_row.value) - 1
            new_source_length = len(
                self.sources.endpoint_defs.data['output_type']) - 1

            if new_source_length == 0:
                clear_source_data(self.sources, 'endpoint_defs')
                self.ep_row.options = ['']
                self.ep_row.value = ''
            else:
                for key in list(new_ep_source):
                    new_ep_source[key].pop(index_to_delete)

                for i in range(index_to_delete, new_source_length):
                    new_ep_source['row'][i] -= 1

                self.ep_row.options = [
                    str(x + 1) for x in range(new_source_length)
                ]
                if self.ep_row.value not in self.ep_row.options:
                    self.ep_row.value = self.ep_row.options[-1]
                self.sources.endpoint_defs.data = new_ep_source

            self.update_source_endpoint_calcs(
            )  # not efficient, but still relatively quick
            clear_source_selection(self.sources, 'endpoint_defs')

    def update_ep_row_on_selection(self, attr, old, new):
        self.query.allow_source_update = False

        if new:
            data = self.sources.endpoint_defs.data
            r = min(new)

            # update row
            self.ep_row.value = self.ep_row.options[r]

            # update input value
            self.ep_text_input.value = str(data['input_value'][r])

            # update input units radio button
            if '%' in data['units_in'][r]:
                self.ep_units_in.active = 1
            else:
                self.ep_units_in.active = 0

            # update output
            if 'Dose' in data['output_type'][r]:
                if '%' in data['units_in'][r]:
                    self.select_ep_type.value = self.ep_options[1]
                else:
                    self.select_ep_type.value = self.ep_options[0]
            else:
                if '%' in data['units_in'][r]:
                    self.select_ep_type.value = self.ep_options[3]
                else:
                    self.select_ep_type.value = self.ep_options[2]

        self.query.allow_source_update = True

    def update_source_endpoint_calcs(self):

        if self.query.current_dvh:
            group_1_constraint_count, group_2_constraint_count = group_constraint_count(
                self.sources)

            ep = {'mrn': ['']}
            ep_group = {'1': {}, '2': {}}

            table_columns = []

            ep['mrn'] = self.query.current_dvh.mrn
            ep['uid'] = self.query.current_dvh.study_instance_uid
            ep['group'] = self.sources.dvhs.data['group']
            ep['roi_name'] = self.sources.dvhs.data['roi_name']

            table_columns.append(TableColumn(field='mrn', title='MRN'))
            table_columns.append(TableColumn(field='group', title='Group'))
            table_columns.append(
                TableColumn(field='roi_name', title='ROI Name'))

            data = self.sources.endpoint_defs.data
            for r in range(len(data['row'])):
                ep_name = str(data['label'][r])
                table_columns.append(
                    TableColumn(field=ep_name,
                                title=ep_name,
                                formatter=NumberFormatter(format="0.00")))
                x = data['input_value'][r]

                if '%' in data['units_in'][r]:
                    endpoint_input = 'relative'
                    x /= 100.
                else:
                    endpoint_input = 'absolute'

                if '%' in data['units_out'][r]:
                    endpoint_output = 'relative'
                else:
                    endpoint_output = 'absolute'

                if 'Dose' in data['output_type'][r]:
                    ep[ep_name] = self.query.current_dvh.get_dose_to_volume(
                        x,
                        volume_scale=endpoint_input,
                        dose_scale=endpoint_output)
                    for g in GROUP_LABELS:
                        if self.time_series.current_dvh_group[g]:
                            ep_group[g][
                                ep_name] = self.time_series.current_dvh_group[
                                    g].get_dose_to_volume(
                                        x,
                                        volume_scale=endpoint_input,
                                        dose_scale=endpoint_output)

                else:
                    ep[ep_name] = self.query.current_dvh.get_volume_of_dose(
                        x,
                        dose_scale=endpoint_input,
                        volume_scale=endpoint_output)
                    for g in GROUP_LABELS:
                        if self.time_series.current_dvh_group[g]:
                            ep_group[g][
                                ep_name] = self.time_series.current_dvh_group[
                                    g].get_volume_of_dose(
                                        x,
                                        dose_scale=endpoint_input,
                                        volume_scale=endpoint_output)

                if group_1_constraint_count and group_2_constraint_count:
                    ep_1_stats = calc_stats(ep_group['1'][ep_name])
                    ep_2_stats = calc_stats(ep_group['2'][ep_name])
                    stats = []
                    for i in range(len(ep_1_stats)):
                        stats.append(ep_1_stats[i])
                        stats.append(ep_2_stats[i])
                    ep[ep_name].extend(stats)
                else:
                    ep[ep_name].extend(calc_stats(ep[ep_name]))

            self.sources.endpoint_calcs.data = ep

            # Update endpoint calc from review_dvh, if available
            if self.sources.dvhs.data['y'][0] != []:
                review_ep = {}
                rx = float(self.sources.dvhs.data['rx_dose'][0])
                volume = float(self.sources.dvhs.data['volume'][0])
                data = self.sources.endpoint_defs.data
                for r in range(len(data['row'])):
                    ep_name = str(data['label'][r])
                    x = data['input_value'][r]

                    if '%' in data['units_in'][r]:
                        endpoint_input = 'relative'
                        x /= 100.
                    else:
                        endpoint_input = 'absolute'

                    if '%' in data['units_out'][r]:
                        endpoint_output = 'relative'
                    else:
                        endpoint_output = 'absolute'

                    if 'Dose' in data['output_type'][r]:
                        if endpoint_input == 'relative':
                            current_ep = dose_to_volume(y, x)
                        else:
                            current_ep = dose_to_volume(y, x / volume)
                        if endpoint_output == 'relative' and rx != 0:
                            current_ep = current_ep / rx

                    else:
                        if endpoint_input == 'relative':
                            current_ep = volume_of_dose(y, x * rx)
                        else:
                            current_ep = volume_of_dose(y, x)
                        if endpoint_output == 'absolute' and volume != 0:
                            current_ep = current_ep * volume

                    review_ep[ep_name] = [(0, current_ep)]

                review_ep['mrn'] = [(0, self.select_reviewed_mrn.value)]
                self.sources.endpoint_calcs.patch(review_ep)

            self.update_endpoint_view()

        if not options.LITE_VIEW:
            self.time_series.update_options()

    def update_endpoint_view(self):
        if self.query.current_dvh:
            rows = len(self.sources.endpoint_calcs.data['mrn'])
            ep_view = {
                'mrn': self.sources.endpoint_calcs.data['mrn'],
                'group': self.sources.endpoint_calcs.data['group'],
                'roi_name': self.sources.endpoint_calcs.data['roi_name']
            }
            for i in range(1, options.ENDPOINT_COUNT + 1):
                ep_view["ep%s" %
                        i] = [''] * rows  # filling table with empty strings

            for r in range(len(self.sources.endpoint_defs.data['row'])):
                if r < options.ENDPOINT_COUNT:  # limiting UI to ENDPOINT_COUNT for efficiency
                    key = self.sources.endpoint_defs.data['label'][r]
                    ep_view["ep%s" %
                            (r + 1)] = self.sources.endpoint_calcs.data[key]

            self.sources.endpoint_view.data = ep_view

            if not options.LITE_VIEW:
                self.correlation.update_or_add_endpoints_to_correlation()
                categories = list(self.correlation.data['1'])
                categories.sort()

                self.regression.x.options = [''] + categories
                self.regression.y.options = [''] + categories

                self.correlation.validate_data()
                self.correlation.update_correlation_matrix()
                self.regression.update_data()

    def update_source_endpoint_view_selection(self, attr, old, new):
        if new:
            self.sources.endpoint_view.selected.indices = new

    def update_dvh_table_selection(self, attr, old, new):
        if new:
            self.sources.dvhs.selected.indices = new

    def update_dvh_review_rois(self, attr, old, new):
        if self.select_reviewed_mrn.value:
            if new != '':
                self.dvh_review_rois = self.temp_dvh_info.get_roi_names(
                    new).values()
                self.select_reviewed_dvh.options = self.dvh_review_rois
                self.select_reviewed_dvh.value = self.dvh_review_rois[0]
            else:
                self.select_reviewed_dvh.options = ['']
                self.select_reviewed_dvh.value = ['']

        else:
            self.select_reviewed_dvh.options = ['']
            self.select_reviewed_dvh.value = ''
            patches = {
                'x': [(0, [])],
                'y': [(0, [])],
                'roi_name': [(0, '')],
                'volume': [(0, '')],
                'min_dose': [(0, '')],
                'mean_dose': [(0, '')],
                'max_dose': [(0, '')],
                'mrn': [(0, '')],
                'rx_dose': [(0, '')]
            }
            self.sources.dvhs.patch(patches)

    def calculate_review_dvh(self):
        global x, y

        patches = {
            'x': [(0, [])],
            'y': [(0, [])],
            'roi_name': [(0, '')],
            'volume': [(0, 1)],
            'min_dose': [(0, '')],
            'mean_dose': [(0, '')],
            'max_dose': [(0, '')],
            'mrn': [(0, '')],
            'rx_dose': [(0, 1)]
        }

        try:
            if not self.sources.dvhs.data['x']:
                self.query.update_data()

            else:
                file_index = self.temp_dvh_info.mrn.index(
                    self.select_reviewed_mrn.value)
                roi_index = self.dvh_review_rois.index(
                    self.select_reviewed_dvh.value)
                structure_file = self.temp_dvh_info.structure[file_index]
                plan_file = self.temp_dvh_info.plan[file_index]
                dose_file = self.temp_dvh_info.dose[file_index]
                key = list(
                    self.temp_dvh_info.get_roi_names(
                        self.select_reviewed_mrn.value))[roi_index]

                rt_st = dicomparser.DicomParser(structure_file)
                rt_structures = rt_st.GetStructures()
                review_dvh = dvhcalc.get_dvh(structure_file, dose_file, key)
                dicompyler_plan = dicomparser.DicomParser(plan_file).GetPlan()

                roi_name = rt_structures[key]['name']
                volume = review_dvh.volume
                min_dose = review_dvh.min
                mean_dose = review_dvh.mean
                max_dose = review_dvh.max
                if not self.review_rx.value:
                    rx_dose = float(dicompyler_plan['rxdose']) / 100.
                    self.review_rx.value = str(round(rx_dose, 2))
                else:
                    rx_dose = round(float(self.review_rx.value), 2)

                x = review_dvh.bincenters
                if max(review_dvh.counts):
                    y = np.divide(review_dvh.counts, max(review_dvh.counts))
                else:
                    y = review_dvh.counts

                if self.radio_group_dose.active == 1:
                    f = 5000
                    bin_count = len(x)
                    new_bin_count = int(bin_count * f / (rx_dose * 100.))

                    x1 = np.linspace(0, bin_count, bin_count)
                    x2 = np.multiply(
                        np.linspace(0, new_bin_count, new_bin_count),
                        rx_dose * 100. / f)
                    y = np.interp(x2, x1, review_dvh.counts)
                    y = np.divide(y, np.max(y))
                    x = np.divide(np.linspace(0, new_bin_count, new_bin_count),
                                  f)

                if self.radio_group_volume.active == 0:
                    y = np.multiply(y, volume)

                patches = {
                    'x': [(0, x)],
                    'y': [(0, y)],
                    'roi_name': [(0, roi_name)],
                    'volume': [(0, volume)],
                    'min_dose': [(0, min_dose)],
                    'mean_dose': [(0, mean_dose)],
                    'max_dose': [(0, max_dose)],
                    'mrn': [(0, self.select_reviewed_mrn.value)],
                    'rx_dose': [(0, rx_dose)]
                }

        except:
            pass

        self.sources.dvhs.patch(patches)

        self.update_source_endpoint_calcs()

    def select_reviewed_dvh_ticker(self, attr, old, new):
        self.calculate_review_dvh()

    def review_rx_ticker(self, attr, old, new):
        if self.radio_group_dose.active == 0:
            self.sources.dvhs.patch(
                {'rx_dose': [(0, round(float(self.review_rx.value), 2))]})
        else:
            self.calculate_review_dvh()

    def radio_group_ticker(self, attr, old, new):
        if self.sources.dvhs.data['x'] != '':
            self.query.update_data()
            self.calculate_review_dvh()

    def add_query_link(self, query):
        self.query = query
예제 #27
0
def make_tab():
    # Slider to select width of bin
    pastdays_select = RangeSlider(start=0,
                                  end=999,
                                  value=(0, 999),
                                  step=1,
                                  title='Past Days',
                                  sizing_mode="stretch_both")
    # Slider to select buffer size
    bufferdays_select = Slider(start=.01,
                               end=9,
                               value=0.01,
                               step=.1,
                               title='Buffer Size (days)',
                               sizing_mode="stretch_both")
    # Re-read
    refresh_button = Button(label="Time window and buffer are up to date",
                            button_type="success",
                            sizing_mode="stretch_both")
    refresh_button.disabled = True

    # read data
    flights, available_carriers = read(pastdays_select, bufferdays_select)

    # CheckboxGroup to select carrier to display
    locationcodes = np.unique(
        ['.'.join(seedid.split('.')[:3]) for seedid in available_carriers])
    selections = []
    for locationcode in locationcodes[:maxnstation]:
        matching = [s for s in available_carriers if locationcode in s]
        active = [i for i, m in enumerate(matching)]  # if "Z" == m[-1]]
        selections += [
            CheckboxButtonGroup(labels=matching,
                                active=active,
                                sizing_mode="stretch_both")
        ]
        #selections += [MultiSelect(#title="Option:",
        #                           value=matching,
        #                           active=active)

    # Find the initially selected carrieres
    initial_carriers = [s.labels[i] for s in selections for i in s.active]

    # Slider to select width of bin
    binwidth_select = Slider(start=16,
                             end=160,
                             step=16,
                             value=80,
                             title='Bin number',
                             sizing_mode="stretch_both")

    # RangeSlider control to select start and end of plotted delays
    range_select = RangeSlider(start=-1,
                               end=999,
                               value=(-.2, 99),
                               step=.1,
                               title='Range (sec)',
                               sizing_mode="stretch_both")

    # Switch from lines to hists
    type_switch = RadioButtonGroup(labels=["Histogram", "Cumulated dist."],
                                   active=0,
                                   sizing_mode="stretch_both")

    # Find the initially selected carrieres
    plottype = type_switch.labels[type_switch.active]

    src = {}
    for output in ['Latencies', 'Delays', 'PSD']:
        src[output] = make_dataset(flights,
                                   initial_carriers,
                                   range_start=range_select.value[0],
                                   range_end=range_select.value[1],
                                   bin_width=binwidth_select.value,
                                   output=output,
                                   plottype=plottype)

    callback = partial(update,
                       output='Delays',
                       type_switch=type_switch,
                       flights=flights,
                       src=src,
                       selections=selections,
                       range_select=range_select,
                       binwidth_select=binwidth_select)

    callbacklat = partial(update,
                          output='Latencies',
                          type_switch=type_switch,
                          flights=flights,
                          src=src,
                          range_select=range_select,
                          selections=selections,
                          binwidth_select=binwidth_select)

    callbackpsd = partial(update,
                          output='PSD',
                          type_switch=type_switch,
                          flights=flights,
                          src=src,
                          range_select=range_select,
                          selections=selections,
                          binwidth_select=binwidth_select)

    callbackneedsread = partial(needsreadupdate, refresh_button=refresh_button)

    callbackread = partial(readupdate,
                           src=src,
                           selections=selections,
                           range_select=range_select,
                           type_switch=type_switch,
                           binwidth_select=binwidth_select,
                           pastdays_select=pastdays_select,
                           bufferdays_select=bufferdays_select,
                           refresh_button=refresh_button)

    [
        s.on_change('active', callback, callbacklat, callbackpsd)
        for s in selections
    ]
    type_switch.on_change('active', callback, callbacklat, callbackpsd)
    binwidth_select.on_change('value', callback, callbacklat, callbackpsd)
    range_select.on_change('value', callback, callbacklat, callbackpsd)
    pastdays_select.on_change('value', callbackneedsread)
    bufferdays_select.on_change('value', callbackneedsread)
    refresh_button.on_click(callbackread)

    p = {}
    for output in ['PSD', 'Latencies', 'Delays']:
        p[output] = make_plot(src[output], output=output)

    # Create a row layout
    graphs = [p[k] for k in p]
    controls = [
        type_switch, binwidth_select, range_select, refresh_button,
        pastdays_select, bufferdays_select, *selections[:maxnstation]
    ]
    graphslayout = column(children=graphs, sizing_mode="stretch_both")
    controlslayout = column(
        children=controls,
        sizing_mode='fixed',  #stretch_width',
        width=400,
    )
    layout = row(children=[graphslayout, controlslayout],
                 sizing_mode='stretch_both')

    # Make a tab with the layout
    return Panel(child=layout, title='Channels')
예제 #28
0
def basic_teams_stats_tab(teams_stats_df):
    """Tab with teams stats."""
    def create_data_source(comparison_stat, aggfunc):
        """Returns a pivoted table by teams and match result (w/d/l).

        Values are index of comparison_stat.

        :param comparison_stat: str. Statistic to show (goals, passes,
        etc.).
        :param aggfunc: str. Aggregate function to calculate by ('mean'
         or 'sum').
        """
        def get_team_indexed_stat(team_row, stat, aggfunc):
            """Returns aggregated team's stat.

            :param team_row: pd.Series. A row from teams stat table.
            :param stat: str. Statistic to calculate (goal, passes,
            etc.)
            :param aggfunc: str. Aggregate function to calculate by
            ('mean' or 'sum')
            """

            if aggfunc == 'mean':
                df = teams_stats_df.groupby(by='team').mean()
            elif aggfunc == 'sum':
                df = teams_stats_df.groupby(by='team').sum()
            return df.loc[team_row.name, stat]

        df = teams_stats_df.pivot_table(index='team',
                                        columns='Match result',
                                        values=comparison_stat,
                                        aggfunc=aggfunc)

        df['Total'] = df.apply(get_team_indexed_stat,
                               args=[comparison_stat, aggfunc],
                               axis=1)

        return df.sort_values(by='Total', ascending=False)

    def plot_team_stat(comparison_stat, agg_func):
        """Creates figures with bars plots of teams stats.

        :param comparison_stat: str. Statistic to plot.
        :param agg_func: str. 'mean' or 'sum'.
        :return: bokeh figures.
        """

        map_agg_func = ('mean', 'sum')
        data = create_data_source(comparison_stat, map_agg_func[agg_func])
        source = ColumnDataSource(data=data)
        teams = list(source.data['team'])

        # Plot avg stat per game

        p_1 = figure(x_range=FactorRange(factors=teams),
                     plot_height=400,
                     plot_width=700)

        hover = HoverTool(tooltips=[('', '@{Total}')])
        hover.point_policy = 'follow_mouse'
        p_1.add_tools(hover)

        p_1.vbar(x='team',
                 top='Total',
                 source=source,
                 width=0.4,
                 color=Spectral4[0])

        p_1.x_range.range_padding = 0.05
        p_1.xaxis.major_label_orientation = 1
        p_1.xaxis.major_label_text_font_size = "10pt"
        p_1.toolbar_location = None

        # Plot breakdown by match result

        p_2 = figure(x_range=FactorRange(factors=teams),
                     plot_height=400,
                     plot_width=700,
                     tools='hover',
                     tooltips='@$name',
                     title='Breakdown by Match Result')

        w = p_2.vbar(x=dodge('team', -0.25, range=p_2.x_range),
                     top='w',
                     width=0.2,
                     source=source,
                     color=Spectral4[1],
                     name='w')
        d = p_2.vbar(x=dodge('team', 0.0, range=p_2.x_range),
                     top='d',
                     width=0.2,
                     source=source,
                     color=Spectral4[2],
                     name='d')
        l = p_2.vbar(x=dodge('team', 0.25, range=p_2.x_range),
                     top='l',
                     width=0.2,
                     source=source,
                     color=Spectral4[3],
                     name='l')

        legend_it = [('Won', [w]), ('Drew', [d]), ('Lost', [l])]
        legend = Legend(items=legend_it, location=(0, 155))

        p_2.add_layout(legend, 'right')
        p_2.title.text_font_size = '12pt'
        p_2.x_range.range_padding = 0.05
        p_2.xgrid.grid_line_color = None
        p_2.xaxis.major_label_text_font_size = "10pt"
        p_2.xaxis.major_label_orientation = 1
        p_2.toolbar_location = None

        return p_1, p_2

    # Update plots on changes

    def update(attrname, old, new):
        """Update plots after widgets changes."""
        stat = select_stat.value
        agg_func = choose_agg_func.active
        p1, p2 = plot_team_stat(stat, agg_func)
        layout.children[1:] = [p1, p2]

    # Widgets
    select_stat = Select(title="Select a Stat for Comparison:",
                         value="goal",
                         options=list(teams_stats_df.columns)[1:-3])
    select_stat.on_change('value', update)

    choose_agg_func = RadioButtonGroup(labels=['Average per Match', 'Total'],
                                       active=0)
    choose_agg_func.on_change('active', update)

    # Wrap widgets
    widgets = widgetbox([select_stat, choose_agg_func])

    comparison_stat = select_stat.value
    agg_func_state = choose_agg_func.active

    # Arrange layout
    p1, p2 = plot_team_stat(comparison_stat, agg_func_state)
    layout = column(row(widgets), p1, p2)
    tab = Panel(child=layout, title='Basic Teams Stats')

    return tab
예제 #29
0
def update(attr, old, new):

    init_time = datetime.now()

    main_doc.clear()
    main_doc.add_root(layout)
    #main_doc.theme = 'dark_minimal'

    title = ticker0.value

    print('-I- selected item is : ', title)

    #     sentiment = ticker1.value
    reviews = reviews_ms1.copy()
    reviews = reviews[reviews.title == title]
    reviews = reviews.drop_duplicates()
    reviews['id'] = range(len(reviews))

    ## sentence making
    full_comments = []
    full_title = []
    l1 = int(len(reviews) / 2)
    c1, t1, id1 = sentence_finder(reviews, 0, l1)
    c2, t2, id2 = sentence_finder(reviews, l1, len(reviews))
    full_comments = c1 + c2
    full_title = t1 + t2
    full_id = id1 + id2

    reviews_old = reviews[['id', 'comments']]
    reviews_old.columns = ['id', 'comments_full']

    def stripper(s):
        return s.strip()

    reviews = pd.DataFrame()
    reviews['comments'] = full_comments
    reviews['comments'] = reviews['comments'].apply(stripper)
    reviews['title'] = full_title
    reviews['id'] = full_id
    reviews = reviews[reviews.comments != '']
    reviews = reviews.drop_duplicates(subset=['comments'])
    reviews = reviews.merge(reviews_old, on='id', how='left')

    title_words = spacy_tokenizer(reviews.title.iloc[0].replace("'",
                                                                "")).split()

    def title_word_remover(s):
        for t in title_words:
            s = s.replace(t, '')
        return s

    ## For finding bigrams
    data = reviews.comments.values.tolist()
    # Remove Emails
    data = [re.sub('\S*@\S*\s?', '', sent) for sent in data]
    # Remove new line characters
    data = [re.sub('\s+', ' ', sent) for sent in data]
    # Remove distracting single quotes
    data = [re.sub("\'", "", sent) for sent in data]
    data_words = list(sent_to_words(data))

    # Build the bigram and trigram models
    bigram = gensim.models.Phrases(
        data_words, min_count=4,
        threshold=50)  # higher threshold fewer phrases.
    # trigram = gensim.models.Phrases(bigram[data_words], threshold=50)
    # Faster way to get a sentence clubbed as a trigram/bigram
    bigram_mod = gensim.models.phrases.Phraser(bigram)
    # trigram_mod = gensim.models.phrases.Phraser(trigram)
    data_words1 = [' '.join(bigram_mod[d]) for d in data_words]
    reviews['comments'] = data_words1

    ## steming lemmetising and stop word removal
    reviews['cleaned_comments'] = reviews.comments.apply(spacy_tokenizer)

    ## sentiment finding
    senti = sentimental_analysis.find_sentiment(reviews)
    reviews['sentiment_pred'] = senti

    ## finding all nouns in the full reviews
    all_nouns = []
    for i in tqdm(range(len(reviews))):
        all_nouns = all_nouns + noun_finder(reviews.cleaned_comments.iloc[i])

    ## Nouns and their count with weight
    noun_df = pd.DataFrame(
        pd.Series(all_nouns).astype('str').value_counts().reset_index())
    noun_df.columns = ['noun', 'count']
    noun_df['weight'] = 0
    noun_df.head()

    print('-I- finding the weight and updating it in df ---------------------')

    ## finding the weight and updating it in df
    for text in tqdm(reviews.cleaned_comments):
        doc = nlp(text)
        noun_adj_pairs = []

        for i, token in enumerate(doc):
            bi_words = str(token).split('_')
            if ((token.pos_ not in ('ADJ')) & (len(bi_words) == 1)):
                continue
            if ((len(bi_words) == 2)):
                if ((nlp(bi_words[0])[0].pos_ == 'ADJ') &
                    (nlp(bi_words[1])[0].pos_ in ('NOUN', 'PROPN')) &
                    (~pd.Series(bi_words[1]).isin(title_words)[0])):
                    noun_adj_pairs.append((bi_words[0], bi_words[1]))
                    try:
                        noun_df.loc[noun_df.noun == str(bi_words[1]),
                                    'weight'] = noun_df.loc[
                                        noun_df.noun == str(bi_words[1]),
                                        'weight'].iloc[0] + 1
                    except:
                        noun_df = noun_df.append(
                            pd.DataFrame(
                                {
                                    'noun': [bi_words[1]],
                                    'count': [1],
                                    'weight': [1]
                                },
                                index=[len(noun_df)]))
                elif ((token.pos_ in ('NOUN', 'PROPN')) &
                      (nlp(bi_words[0])[0].pos_ in ('NOUN', 'PROPN')) &
                      (nlp(bi_words[1])[0].pos_ in ('NOUN', 'PROPN')) &
                      (~pd.Series(bi_words[0]).isin(title_words)[0]) &
                      (~pd.Series(bi_words[1]).isin(title_words)[0])):
                    #             elif((nlp(bi_words[0])[0].pos_ in ('NOUN','PROPN')) & (nlp(bi_words[1])[0].pos_ in ('NOUN','PROPN'))):
                    noun_df.loc[
                        noun_df.noun == str(token),
                        'weight'] = noun_df.loc[noun_df.noun == str(token),
                                                'weight'].iloc[0] + 1
                continue

            if ((pd.Series([str(token)]).isin(positive)[0]) |
                (pd.Series([str(token)]).isin(negative)[0])):
                for j in range(i + 1, min(i + 6, len(doc))):
                    #                 if (doc[j].pos_ in ('NOUN','PROPN')):
                    if ((doc[j].pos_ in ('NOUN', 'PROPN')) &
                        (len(str(doc[j]).split('_')) != 2)):
                        noun_adj_pairs.append((token, doc[j]))
                        noun_df.loc[noun_df.noun == str(doc[j]),
                                    'weight'] = noun_df.loc[
                                        noun_df.noun == str(doc[j]),
                                        'weight'].iloc[0] + 1
                        break

    ## removing words from noun which is in title to find top topics ( topic nouns )
    noun_df = noun_df[~noun_df.noun.isin(
        spacy_tokenizer(reviews.title.iloc[0].replace("'", "")).split())]
    noun_df = noun_df.sort_values(by='weight', ascending=False)
    noun_df = noun_df.iloc[0:20, ]
    reviews.to_csv('./CRM_bokeh_app/static/temp.csv', index=False)

    topic_df = pd.DataFrame()
    topic_df['topics'] = noun_df['noun']
    print('-I- topic sentimental distribution finding---')
    pos_r = []
    neg_r = []
    neu_r = []
    full_l = []
    for t in tqdm(topic_df.topics):
        temp = reviews.copy()
        temp['item_presence'] = temp.apply(
            lambda row: topic_presence(row['cleaned_comments'], t), axis=1)
        temp = temp[temp.item_presence != -1]
        full_l.append(len(temp))
        pos_r.append(len(temp[temp.sentiment_pred == 'positive']))
        neg_r.append(len(temp[temp.sentiment_pred == 'negative']))
        neu_r.append(len(temp[temp.sentiment_pred == 'neutral']))


#     topic_df['length'] = full_l
    topic_df['positive'] = pos_r
    topic_df['negative'] = neg_r
    topic_df['neutral'] = neu_r

    final_time = datetime.now()
    print('-I- Time taken for update is = ', str(final_time - init_time))

    global ticker2
    global radio_button_group
    global text_input

    ticker2 = Select(title='Topic',
                     value='all',
                     options=['all'] + list(noun_df.noun))
    ticker2.on_change('value', update2)
    radio_button_group = RadioButtonGroup(
        labels=['all', 'positive', 'negative', 'neutral'], active=0)
    radio_button_group.on_change('active', update2)

    text_input = TextInput(value="", title="Custom topic search:")
    text_input.on_change("value", update2)

    t_r = column([row([ticker2, text_input]), radio_button_group])

    z = plot_senti(reviews, title)
    z1 = plot_topic(noun_df, title)
    z2 = column([
        row([
            plot_senti_stack(topic_df.sort_values(by='positive'), 1),
            plot_senti_stack(topic_df.sort_values(by='negative'), 2)
        ]),
        plot_senti_stack(topic_df.sort_values(by='neutral'), 3)
    ])
    z = column([row([z, z1]), z2])

    z1 = row([plot_senti(reviews, 'all topics'), summarizer(reviews)])
    z2 = plot_rows(reviews)
    z2 = column([z1, z2])
    z3 = column(column([z, t_r], name='needed1'),
                column([z2], name='review1'),
                name='needed')

    main_doc.add_root(z3)
예제 #30
0
class Page():
    def __init__(self):
        self.WIDTH_MATRIX = 1
        self.HEIGHT_MATRIX = 1
        self.tabs = RadioButtonGroup(labels=['Page1', 'Page2'], active=0)
        self.tabs.on_change('active',
                            lambda attr, old, new: self.change_page())

        self.data_directory = 'data'
        self.data_files = os.listdir(self.data_directory)

        self.select_data_files1 = Select(title="Data files 1:",
                                         value="df1",
                                         options=["df1"] + self.data_files)
        self.select_data_files2 = Select(title="Data files 2:",
                                         value="df2",
                                         options=["df2"] + self.data_files)

        self.select_data_files1.on_change(
            'value', lambda attr, old, new: self.init_dataframes())
        self.select_data_files2.on_change(
            'value', lambda attr, old, new: self.init_dataframes())
        self.refresh_button = Button(label="Refresh",
                                     button_type="success",
                                     width=100)
        self.refresh_button.on_click(self.update_directory)

        self.layout = column(
            self.tabs, row(self.select_data_files1, self.select_data_files2),
            self.refresh_button)
        curdoc().add_root(self.layout)

    # show(self.layout)

    def change_page(self):
        if (self.select_data_files1.value == "df1"
                or self.select_data_files2.value == "df2"):
            self.tabs.active = 0
            return
        if (self.tabs.active == 0):
            self.layout.children = [
                self.tabs,
                row(self.select_data_files1, self.select_data_files2),
                self.refresh_button, self.slider_depth, self.plot_matrix
            ]
        elif (self.tabs.active == 1):
            self.layout.children = [
                self.tabs,
                # self.sel_well,self.pred_button,
                self.plots_rock,
                row(column(self.select_df1_corr, self.checkbox_df1),
                    column(self.select_df2_corr, self.checkbox_df2)),
                self.plot_correlation
            ]

    def init_dataframes(self):
        if (self.select_data_files1.value == "df1"
                or self.select_data_files2.value == "df2"):
            return
        if (not self.select_data_files1.value.endswith('.csv')
                or not self.select_data_files2.value.endswith('.csv')):
            print("incorrect data, expected format csv")
            return

        self.Y_COL = 'Depth'
        self.main_df1, self.main_df2 = self.read_dataframe()
        # self.change
        self.df1 = self.main_df1.copy()
        self.df2 = self.main_df2.copy()
        self.slider_depth = RangeSlider(
            start=self.min_total_depth(self.main_df1, self.main_df2),
            # изменить реализацию мин и мах
            end=self.max_total_depth(self.main_df1, self.main_df2),
            step=1,
            value=(self.min_total_depth(self.main_df1, self.main_df2),
                   self.max_total_depth(self.main_df1, self.main_df2)))
        self.slider_depth.on_change('value', self.change_depth)

        self.update_page()

    def update_page(self):
        print("1")
        self.columns_df1 = [
            col for col in self.df1.columns if self.is_number(col, self.df1)
        ]
        self.columns_df2 = [
            col for col in self.df2.columns if self.is_number(col, self.df2)
        ]
        # self.select_plots = [-1 for i in range(len(self.columns_df1) + len(self.columns_df1))]
        self.select_plots = []
        print("2")
        #	wells = self.df1['Well Name'].unique().tolist()
        #	wells = [well for well in wells if well not in ['Recruit F9']]

        # self.sel_well = Select(title="Well name:", value=wells[0], options=wells)
        # self.pred_button = Button(label="Predict", button_type="success", width=100)
        # self.pred_button.on_click(self.update_predict)

        # self.plots_rock = self.init_plots_rock()

        self.plots_rock = column()
        self.source_correlation_plot = None
        self.plot_correlation = self.init_corr()

        self.select_df1_corr = Select(title="Data frame 1:",
                                      value=self.columns_df1[0],
                                      options=self.columns_df1)
        self.select_df2_corr = Select(title="Data frame 2:",
                                      value=self.columns_df2[1],
                                      options=self.columns_df2)

        self.select_df1_corr.on_change(
            'value', lambda attr, old, new: self.update_set_data())
        self.select_df2_corr.on_change(
            'value', lambda attr, old, new: self.update_set_data())

        self.checkbox_df1 = CheckboxGroup(labels=["log10"], active=[])
        self.checkbox_df2 = CheckboxGroup(labels=["log10"], active=[])
        self.checkbox_df1.on_change(
            'active', lambda attr, old, new: self.update_set_data())
        self.checkbox_df2.on_change(
            'active', lambda attr, old, new: self.update_set_data())
        print("3")
        data_matrix = self.init_data_matrix(self.df1, self.df2)
        self.plot_matrix = self.draw_matrix(data_matrix)
        self.plot_matrix.on_event(Tap, self.update_plots)
        print("4")
        self.layout.children = [
            self.tabs,
            row(self.select_data_files1, self.select_data_files2),
            self.refresh_button, self.slider_depth, self.plot_matrix
        ]
        print("5")

    def change_depth(self, attr, old, new):
        self.df1 = self.df1[
            (self.df1[self.Y_COL] >= self.slider_depth.value[0])
            & (self.df1[self.Y_COL] <= self.slider_depth.value[1])]
        self.df2 = self.df2[
            (self.df2[self.Y_COL] >= self.slider_depth.value[0])
            & (self.df2[self.Y_COL] <= self.slider_depth.value[1])]
        self.update_page()

    def min_total_depth(self, df1, df2):
        return max(df1[self.Y_COL].min(), df2[self.Y_COL].min())

    def max_total_depth(self, df1, df2):
        return min(df1[self.Y_COL].max(), df2[self.Y_COL].max())

    def change_case_depth(self, df1, df2):
        ret_code = self.rename_depth(df1)
        # Обработать код возврата
        ret_code = self.rename_depth(df2)
        # if df1[self.Y_COL][1] - df1[self.Y_COL][0] > df2[self.Y_COL][1] - df2[self.Y_COL][0]:
        if (df1[self.Y_COL].size < df2[self.Y_COL].size):
            df2.set_index([self.Y_COL], inplace=True)
            df2 = df2.reindex(df1[self.Y_COL], method='nearest')
            column_values = pd.Series(df2.index, index=df2.index)
            df2.insert(loc=0, column=self.Y_COL, value=column_values)
            df2.set_index(df1.index, inplace=True)
        else:
            df1.set_index([self.Y_COL], inplace=True)
            df1 = df1.reindex(df2[self.Y_COL], method='nearest')
            column_values = pd.Series(df1.index, index=df1.index)
            df1.insert(loc=0, column=self.Y_COL, value=column_values)
            df1.set_index(df2.index, inplace=True)
        return df1, df2

    def rename_depth(self, df):
        is_renamed_df = False
        dept = "Dept"
        unnamed = "Unnamed: 0"
        exist = 0
        not_exist = 1
        for col in df.columns:
            if (col.lower() == self.Y_COL.lower()
                    or col.lower() == dept.lower()):
                df.rename(columns={col: self.Y_COL}, inplace=True)
                is_renamed_df = True
        if (not is_renamed_df):
            if (df.index.name == None and df.columns[0] == unnamed):
                df.rename(columns={unnamed: self.Y_COL}, inplace=True)
                return not_exist
            elif (df.index.name != None
                  and df.index.name.lower().find(dept.lower()) != -1):
                column_values = pd.Series(df.index, index=df.index)
                df.insert(loc=0, column=self.Y_COL, value=column_values)
                return not_exist
            elif ((df.columns)[0] != unnamed
                  and df.columns[0].lower().find(dept.lower()) != -1):
                df.rename(columns={unnamed: self.Y_COL}, inplace=True)
                return not_exist
            else:
                column_values = pd.Series(df.index, index=df.index)
                df.insert(loc=0, column=self.Y_COL, value=column_values)
                return not_exist
        return exist

    '''	if(df1)
        df_gis = pd.read_csv('gis.csv') 
        df_gis = df_gis.astype(np.float32) 

        df_git = pd.read_csv('git.csv') 
        df_git = df_git.rename(columns={'0':'DEPT'}) 

        df_gis = df_gis.set_index(['DEPT']) 
        df_gis.reindex(df_git['DEPT'], method='nearest')'''

    def is_number(self, col, df):
        if type(df[col][df[col].index[0]]).__name__ == "int64" or type(
                df[col][df[col].index[0]]).__name__ == "float64":
            return True
        return False

    def update_directory(self):
        self.data_directory = 'data'
        self.data_files = os.listdir(self.data_directory)
        self.select_data_files1 = Select(title="Data files 1:",
                                         value="df1",
                                         options=["df1"] + self.data_files)
        self.select_data_files2 = Select(title="Data files 2:",
                                         value="df2",
                                         options=["df2"] + self.data_files)
        self.layout.children[1] = row(self.select_data_files1,
                                      self.select_data_files2)

    def init_corr(self):
        plot_correlation = figure(plot_width=800,
                                  plot_height=500,
                                  title='correlation graph')
        data = pd.DataFrame(data={
            'x': self.df1[self.columns_df1[0]],
            'y': self.df2[self.columns_df2[1]]
        })
        self.source_correlation_plot = ColumnDataSource(data)
        plot_correlation.scatter(x='x',
                                 y='y',
                                 source=self.source_correlation_plot,
                                 line_color='red',
                                 size=2)

        return plot_correlation

    def toFixed(self, numObj, digits=0):
        return f"{numObj:.{digits}f}"

    def get_y_range(self, df):
        ymin = df[self.Y_COL].min()
        ymax = df[self.Y_COL].max()
        res = Range1d(ymin, ymax)
        return res

    def update_plots(self, event):
        length_columns_df1 = len(self.columns_df1)
        length_columns_df2 = len(self.columns_df2)
        x = int((event.x) / self.WIDTH_MATRIX)
        y = int((event.y) / self.HEIGHT_MATRIX)
        if (self.select_plots.count(
            ((1, self.columns_df1[x]),
             (2, self.columns_df2[length_columns_df2 - y - 1]))) == 1):
            self.select_plots.remove(
                ((1, self.columns_df1[x]),
                 (2, self.columns_df2[length_columns_df2 - y - 1])))
            self.delete_select_cell(
                name=self.columns_df1[x] +
                self.columns_df2[length_columns_df2 - y - 1])

        elif (event.x < 0 or event.x > length_columns_df1 or event.y < 0
              or event.y > length_columns_df2):
            return
        else:
            self.draw_select_cell(name=self.columns_df1[x] +
                                  self.columns_df2[length_columns_df2 - y - 1],
                                  x=x + 0.5,
                                  y=y + 0.5,
                                  width=1,
                                  height=1)
            self.select_plots.append(
                ((1, self.columns_df1[x]),
                 (2, self.columns_df2[length_columns_df2 - y - 1])))

        self.plots_rock.children = [
            self.draw_plot_rock(self.df1, self.df2, self.select_plots)
        ]

    def draw_select_cell(self, name, x, y, height, width):
        self.plot_matrix.rect(x=x,
                              y=y,
                              width=width,
                              height=height,
                              fill_alpha=0,
                              line_color="blue",
                              name=name)

    def delete_select_cell(self, name):
        self.plot_matrix.select(name=name).visible = False

    def update_set_data(self):
        r = self.df1[self.select_df1_corr.value]
        col = self.df2[self.select_df2_corr.value]
        if (self.checkbox_df2.active == [0]):
            col = col.apply(numpy.log10)
        if (self.checkbox_df1.active == [0]):
            r = r.apply(numpy.log10)
        data = {'x': r, 'y': col}
        self.source_correlation_plot.data = data

    def update_predict(self):
        return

    def draw_plot_rock(self, df1, df2, select_plots):
        list_plots = []
        source_df1 = ColumnDataSource(data=df1)
        source_df2 = ColumnDataSource(data=df2)
        yrange = self.get_y_range(df1)
        for pair_data in self.select_plots:
            list_plots.append(
                self.init_plot_rock(pair_data[0][1], pair_data[0][1],
                                    self.Y_COL, source_df1, yrange, '#0000ff'))

            list_plots.append(
                self.init_plot_rock(pair_data[1][1], pair_data[1][1],
                                    self.Y_COL, source_df2, yrange, '#008000'))
        return row(list_plots)

    def init_plot_rock(self, title, x, y, source, yrange, color_title):
        plot = figure(plot_width=200,
                      plot_height=400,
                      title=title,
                      toolbar_location=None,
                      tools='ywheel_zoom',
                      active_scroll='ywheel_zoom')
        plot.line(x, y, source=source, line_width=2)
        plot.y_range = yrange
        plot.title.text_color = color_title

        return plot

    def init_data_matrix(self, df1, df2):
        matrix = {col: [] for col in self.columns_df1}
        for r in self.columns_df1:
            for col in self.columns_df2:
                matrix[r].append(
                    float(
                        self.toFixed(
                            numpy.corrcoef(df1[r], df2[col])[0, 1], 3)))
        data = pd.DataFrame(data=matrix)
        data.index = list(self.columns_df2)
        data.index.name = 'rows'
        data.columns.name = 'columns'

        return data

    def draw_matrix(self, data):
        df = pd.DataFrame(data.stack(), columns=['rate']).reset_index()

        source = ColumnDataSource(df)
        colors = [
            "#75968f", "#a5bab7", "#c9d9d3", "#e2e2e2", "#dfccce", "#ddb7b1",
            "#cc7878", "#933b41", "#550b1d"
        ]
        mapper = LinearColorMapper(palette=colors,
                                   low=df.rate.min(),
                                   high=df.rate.max())

        plot_matrix = figure(plot_width=800,
                             plot_height=300,
                             title="",
                             x_range=list(self.columns_df2),
                             y_range=list(reversed(self.columns_df1)),
                             toolbar_location=None,
                             tools="",
                             x_axis_location="above")
        plot_matrix.rect(x="rows",
                         y="columns",
                         width=self.WIDTH_MATRIX,
                         height=self.HEIGHT_MATRIX,
                         source=source,
                         line_color=None,
                         fill_color=transform('rate', mapper))
        plot_matrix.xaxis.major_label_text_color = '#0000ff'  # blue
        plot_matrix.yaxis.major_label_text_color = '#008000'  # green
        self.r = plot_matrix.text(x=dodge("rows",
                                          -0.1,
                                          range=plot_matrix.x_range),
                                  y=dodge("columns",
                                          -0.2,
                                          range=plot_matrix.y_range),
                                  text="rate",
                                  **{"source": df})
        self.r.glyph.text_font_size = "9pt"

        plot_matrix.axis.axis_line_color = None
        plot_matrix.axis.major_tick_line_color = None
        return plot_matrix

    def read_dataframe(self):
        df1 = pd.read_csv('data/' + self.select_data_files1.value)
        df2 = pd.read_csv('data/' + self.select_data_files2.value)
        df1, df2 = self.change_case_depth(df1, df2)
        min = self.min_total_depth(df1, df2)
        max = self.max_total_depth(df1, df2)
        print("@@@")
        df1 = df1[(df1[self.Y_COL] >= min) & (df1[self.Y_COL] <= max)]
        df2 = df2[(df2[self.Y_COL] >= min) & (df2[self.Y_COL] <= max)]
        print("######")
        return (df1, df2)
예제 #31
0
    #new_data = pd.DataFrame({'x':xs,'y':ys,'color':(Category10[3])[0:len(xs)]})

    new_source = ColumnDataSource(data=dict(
        x=xs,
        y=ys,
        color=(Category10[3])[0:len(xs)],
        group=['Gold Standard', 'Forecast In Sample', 'Forecast OOS']))
    source.data = new_source.data
    # p_ts.source = new_source

    new_text = 'In and Out of Sample fit <br>Gold Standard :' + \
        old_to_new_regions[gs_name[:-3]] + ' - Predictor Set: ' + \
        old_to_new_sources[source_set] + '<br>' + \
        'In Sample R<sup>2</sup>: ' + np.str(np.round(r_squared_in_sample,2)) + \
        ' - ' + 'Out of Sample R<sup>2</sup>: ' + np.str(np.round(r_squared_OOS,2))
    div.text = new_text


source_set_button_plot.on_change('active', plot_callback)
regions_button_plt.on_change('active', plot_callback)

# To run from Spyder (radio buttons won't work)
# output_file('foo.html')
# show(column(regions_button_plt,source_set_button_plot,div,p_ts),browser="chrome")

# To run from command (in the folder of the file) using the command
# bokeh serve --show visu_ts.py
curdoc().add_root(column(regions_button_plt, source_set_button_plot, div,
                         p_ts))
curdoc().title = "Venezuela Situational Awareness"
예제 #32
0
def load_page(experiment_df, experiment_db):

    ########## bokeh plots ##########

    # general plot tools
    plot_tools = 'wheel_zoom, pan, reset, save, hover'
    hover = HoverTool(tooltips=[("(x,y)", "($x, $y)")])

    # progress curve plots
    global raw_source
    raw_source = ColumnDataSource(data=dict(x=[], y=[], yr=[], yfit=[]))
    global raw
    raw = figure(title="Initial Rate Fit",
                 x_axis_label="Time",
                 y_axis_label="Signal",
                 plot_width=350,
                 plot_height=300,
                 tools=plot_tools)
    raw.circle('x',
               'y',
               size=2,
               source=raw_source,
               color='gray',
               selection_color="black",
               alpha=0.6,
               nonselection_alpha=0.2,
               selection_alpha=0.6)
    raw.line('x', 'yfit', source=raw_source, color='red')
    global warning_source
    warning_source = ColumnDataSource(data=dict(
        x=[0],
        y=[0],
        t=[
            'Please enter transform equation! \nMust convert signal to [substrate] \nin Schnell-Mendoza mode (e.g. via \nx/6.22/0.45/0.001 for sample data). \nNote: this transform may need \nto be inverted through multiplying \nby -1 when analyzing experiments \nthat measure increasing product \nconcentration over time)'
        ]))
    global warning
    warning = raw.text(x='x',
                       y='y',
                       text='t',
                       text_font_size='12pt',
                       angle=0,
                       source=warning_source)
    warning.visible = False
    global circles_source
    circles_source = ColumnDataSource(
        data=dict(x=[-.05, -.05, 1.6, 1.6], y=[0, 0.6, 0, 0.6]))
    global circles
    circles = raw.circle(x='x', y='y', alpha=0., source=circles_source)
    circles.visible = False
    global resi
    resi = figure(title="Initial Rate Fit Residuals",
                  x_axis_label="Time",
                  y_axis_label="Residual",
                  plot_width=700,
                  plot_height=200,
                  tools='wheel_zoom,pan,reset')
    resi.yaxis.formatter = BasicTickFormatter(precision=2, use_scientific=True)
    resi.circle('x', 'yr', size=5, source=raw_source, color='grey', alpha=0.6)

    # model plot for titration experiments
    global model_data_source
    model_data_source = ColumnDataSource(
        data=dict(xt=[], yt=[], n=[], ct=[], et=[]))
    global model_plot_source
    model_plot_source = ColumnDataSource(
        data=dict(xp=[], yp=[], l=[], u=[], cp=[], ep=[]))
    global model_fit_source
    model_fit_source = ColumnDataSource(data=dict(x=[], y=[]))
    global varea_source
    varea_source = ColumnDataSource(data=dict(x=[], r1=[], r2=[]))
    global model
    model = figure(title='Model Fit',
                   x_axis_label='Concentration',
                   y_axis_label='Rate',
                   plot_width=350,
                   plot_height=300,
                   tools=plot_tools)
    model.circle('xp',
                 'yp',
                 size=8,
                 source=model_plot_source,
                 color='cp',
                 alpha=0.6)
    model.add_layout(
        Whisker(source=model_plot_source, base='xp', upper='u', lower='l'))
    model.line('x',
               'y',
               source=model_fit_source,
               line_width=3,
               color='black',
               alpha=0.8)
    model.varea('x', 'r1', 'r2', source=varea_source, color='grey', alpha=0.3)

    ########## bokeh widgets ##########

    # button for selecting progress curve fitting routine
    global fit_button
    fit_button = RadioButtonGroup(labels=[
        'Maximize Slope Magnitude', 'Linear Fit', 'Logarithmic Fit',
        'Schnell-Mendoza'
    ],
                                  active=0,
                                  width=375)
    fit_button.on_change('active', widget_callback)

    # button for selecting progress curve fitting routine
    global scalex_box
    scalex_box = CheckboxButtonGroup(
        labels=["transform x-axis to Log10 scale"], active=[])
    scalex_box.on_change('active', widget_callback)

    # dropdown menu for selecting titration experiment model
    global model_select
    model_select = Select(
        title='Choose Model',
        value='Michaelis-Menten',
        options=['Michaelis-Menten', 'pEC50/pIC50', 'High-Throughput Screen'],
        width=350)
    model_select.on_change('value', widget_callback)

    # dropdown menu for selecting blank sample to subtract from remaining titration samples
    global subtract_select
    subtract_select = Select(title='Select Blank Sample for Subtraction',
                             value='',
                             options=list(experiment_df)[1:] + [''],
                             width=350)
    subtract_select.on_change('value', widget_callback)

    # dropdown menu for selecting titration sample to plot in current view
    global sample_select
    sample_select = Select(title='Y Axis Sample',
                           value=list(experiment_df)[-1],
                           options=list(experiment_df)[1:],
                           width=350)
    sample_select.on_change('value', sample_callback)

    # text input box for transforming slopes to rates
    global transform_input
    transform_input = TextInput(value='',
                                title="Enter Transform Equation",
                                width=350)
    transform_input.on_change('value', widget_callback)

    # text input box for setting delay time in logarithmic progress curve fitting
    global offset_input
    offset_input = TextInput(value='',
                             title="Enter Time Between Mixing and First Read",
                             width=350)
    offset_input.on_change('value', widget_callback)

    # text input boxes for fixing EC/IC50 parameters
    global bottom_fix
    bottom_fix = TextInput(value='', title="Fix pIC50/pEC50 Bottom")
    bottom_fix.on_change('value', widget_callback)

    global top_fix
    top_fix = TextInput(value='', title="Fix pIC50/pEC50 Top")
    top_fix.on_change('value', widget_callback)

    global slope_fix
    slope_fix = TextInput(value='', title="Fix pIC50/pEC50 Hill Slope")
    slope_fix.on_change('value', widget_callback)

    # text input boxes for progress curve xrange selection
    global start_time
    start_time = TextInput(value=str(
        experiment_df[list(experiment_df)[0]].values[0]),
                           title="Enter Start Time")
    global end_time
    end_time = TextInput(value=str(
        experiment_df[list(experiment_df)[0]].values[-1]),
                         title='Enter End Time')
    start_time.on_change('value', xbox_callback)
    end_time.on_change('value', xbox_callback)

    # range slider to select threshold for hit detection in HTS mode
    global threshold_slider
    threshold_slider = Slider(start=0,
                              end=5,
                              value=2,
                              step=0.1,
                              title='HTS Hit Threshold (Standard Deviation)',
                              width=350)
    threshold_slider.on_change('value', threshold_callback)

    # range slider to update plots according to progress cuve xrange selection
    xmin = experiment_df[experiment_df.columns[0]].values[0]
    xmax = experiment_df[experiment_df.columns[0]].values[-1]
    global range_slider
    range_slider = RangeSlider(
        start=xmin,
        end=xmax,
        value=(xmin, xmax),
        step=experiment_df[experiment_df.columns[0]].values[1] - xmin,
        title='Fine Tune X-Axis Range',
        width=650)
    range_slider.on_change('value', slider_callback)

    # button to upload local data file
    global file_source
    file_source = ColumnDataSource(data=dict(file_contents=[], file_name=[]))
    file_source.on_change('data', file_callback)
    try:
        output_filename = file_source.data['file_name'] + '-out.csv'
    except:
        output_filename = 'output.csv'
    global upload_button
    upload_button = Button(label="Upload Local File",
                           button_type="success",
                           width=350)
    upload_button.callback = CustomJS(args=dict(file_source=file_source),
                                      code=open(
                                          join(dirname(__file__),
                                               "upload.js")).read())

    # table containing rate fits and errors
    template = """
    <div style="background:<%=ct%>"; color="white";>
    <%= value %></div>
    """
    formatter = HTMLTemplateFormatter(template=template)
    columns = [
        TableColumn(field='n', title='Sample'),
        TableColumn(field='yt',
                    title='Slope (Initial Rate)',
                    formatter=formatter),
        TableColumn(field='et', title='Std. Error')
    ]
    global rate_table
    rate_table = DataTable(source=model_data_source,
                           columns=columns,
                           width=350,
                           height=250,
                           selectable=True,
                           editable=True)

    # tables containing model fits and errors
    global mm_source
    mm_source = ColumnDataSource(dict(label=[], Km=[], Vmax=[]))
    columns = [
        TableColumn(field='label', title=''),
        TableColumn(field='Vmax', title='Vmax'),
        TableColumn(field='Km', title='Km')
    ]
    global mm_table
    mm_table = DataTable(source=mm_source,
                         columns=columns,
                         width=350,
                         height=75,
                         selectable=True,
                         editable=True)
    global ic_source
    ic_source = ColumnDataSource(
        dict(label=[], Bottom=[], Top=[], Slope=[], p50=[]))
    columns = [
        TableColumn(field='label', title=''),
        TableColumn(field='Bottom', title='Bottom'),
        TableColumn(field='Top', title='Top'),
        TableColumn(field='Slope', title='Slope'),
        TableColumn(field='p50', title='pEC/IC50')
    ]
    global ic_table
    ic_table = DataTable(source=ic_source,
                         columns=columns,
                         width=350,
                         height=75,
                         selectable=True,
                         editable=True)

    # button for copying rate data table to clipboard
    global copy_button
    copy_button = Button(label="Copy Table to Clipboard",
                         button_type="primary",
                         width=350)
    copy_button.callback = CustomJS(args=dict(source=model_data_source),
                                    code=open(
                                        join(dirname(__file__),
                                             "copy.js")).read())

    # button for downloading rate data table to local csv file
    global download_button
    download_button = Button(label="Download Table to CSV",
                             button_type="primary",
                             width=350)
    download_button.callback = CustomJS(args=dict(source=model_data_source,
                                                  file_name=output_filename),
                                        code=open(
                                            join(dirname(__file__),
                                                 "download.js")).read())

    ########## document formatting #########

    desc = Div(text=open(join(dirname(__file__), "description.html")).read(),
               width=1400)

    advanced = Div(
        text="""<strong>Advanced Settings for \npEC/IC50 Analysis</strong>""")

    widgets = widgetbox(model_select, sample_select, subtract_select,
                        transform_input, offset_input, advanced, scalex_box,
                        bottom_fix, top_fix, slope_fix)
    table = widgetbox(rate_table)
    main_row = row(
        column(upload_button, widgets),
        column(fit_button, row(raw, model), resi, row(start_time, end_time),
               range_slider),
        column(download_button, copy_button, table, mm_table, ic_table,
               threshold_slider))

    sizing_mode = 'scale_width'
    l = layout([[desc], [main_row]], sizing_mode=sizing_mode)

    update()
    curdoc().clear()
    curdoc().add_root(l)
    curdoc().title = "ICEKAT"
예제 #33
0

# Update function
def score_type_callback(attr, old, new):
    # Get new selected value
    #new_score = score_types[new.active]
    new_score = score_types[score_type_button.active]
    new_source = data_sources[sources_button.active]
    new_key = new_score + '|' + new_source
    geosource_new = all_data[new_key]
    geosource.geojson = geosource_new.geojson

    #div.text = new_score # For testing only


score_type_button.on_change('active', score_type_callback)
sources_button.on_change('active', score_type_callback)

#Display figure.
set_stype(p)

# In Notebook (without callbacks)
#output_notebook()
#show(p)

# In Spyder (without callbacks)
# output_file('foo.html')
# show(column(widgetbox(score_type_button),p),browser="chrome")

# To run from command (in the folder of the file) using the command
# bokeh serve --show situ_map.py
예제 #34
0
    button_value = radio_button_group.active
    print('Button value  is : ', button_value)

    if button_value == 0:
        won_source = new_source[new_source['Result'] == 'won']
        new_won_cds = ColumnDataSource(won_source)
        total_cds.data = new_won_cds.data
    if button_value == 1:
        lost_source = new_source[new_source['Result'] == 'lost']
        new_lost_cds = ColumnDataSource(lost_source)
        total_cds.data = new_lost_cds.data
    if button_value == 2:
        tied_source = new_source[new_source['Result'] == 'tied']
        new_tied_cds = ColumnDataSource(tied_source)
        total_cds.data = new_tied_cds.data

    fig.title.text = 'Total Matches Played Aganist Opposition for Year %d' % yr
    fig.x_range.factors = []
    fig.x_range.factors = list(new_source['Opposition'].unique())


radio_button_group.on_change('active',
                             lambda attr, old, new: update(attr, old, new))

slider.on_change('value', update)

# Make a row layout of widgetbox(slider) and plot and add it to the current document
layout = row(column(widgetbox(slider), widgetbox(radio_button_group)), fig)

curdoc().add_root(layout)
예제 #35
0
''' ran change from original '''
def open_local_file(attr,old,new):
    labels.text = ["chosen file is : " + tkFileDialog.askopenfilename()]
    print labels.text

def open_local_file_long(attr,old,new):
    import wx
    import os

    app = wx.PySimpleApp()
    wildcard = "Python source (*.py)|*.py|" \
               "Compiled Python (*.pyc)|*.pyc|" \
               "All files (*.*)|*.*"
    dialog = wx.FileDialog(None, "Choose a file", os.getcwd(), "", wildcard, wx.OPEN)
    if dialog.ShowModal() == wx.ID_OK:
        print dialog.GetPath()

    dialog.Destroy()


#create select widget
options=["average_grades","exam_grades","student_names"]
radio_button_group=RadioButtonGroup(labels=options)
# radio_button_group.on_change("active",update_labels)
radio_button_group.on_change("active",open_local_file)

#create layout and add to curdoc
lay_out=layout([[radio_button_group]])
curdoc().add_root(f)
curdoc().add_root(lay_out)