Example #1
0
def plot_bokeh(df, ticker):
    p = figure(width=800, height=400, title=ticker.upper(), tools="")

    hover = HoverTool(tooltips="""
    <div>
    <table>
    <tr><td class="ttlab">Date:</td><td>@date_str</td></tr>
    <tr><td class="ttlab">Close:</td><td>@close_str</td></tr>
    </table>
    </div>
    """)

    hover.mode = 'vline'
    hover.line_policy = 'nearest'
    p.add_tools(hover)

    crosshair = CrosshairTool()
    crosshair.dimensions = 'height'
    crosshair.line_color = "#ffffff"
    p.add_tools(crosshair)

    dfcds = ColumnDataSource(df)
    p.line('date', 'close', source=dfcds, color="#44ddaa")

    p.xaxis.formatter = DatetimeTickFormatter(days=["%d %b"])
    p.x_range = Range1d(df['date'].min(), df['date'].max())

    p.toolbar.logo = None
    p.toolbar_location = None

    return p
Example #2
0
def plot_setter(df, ticker):

    p = figure(width=700, height=400, title="Ticker=" + ticker, tools="")

    hover = HoverTool(tooltips=[
        ('date', '@date{%F}'),
        ('close', '$@close{%0.2f}'),
    ],
                      formatters={
                          'date': 'datetime',
                          'close': 'printf'
                      })
    hover.mode = 'vline'
    hover.line_policy = 'nearest'
    p.add_tools(hover)

    dfcds = ColumnDataSource(df)
    p.line('date', 'close', source=dfcds, color="#000000")

    p.xaxis.formatter = DatetimeTickFormatter(days=["%d %b"])
    p.x_range = Range1d(df['date'].min(), df['date'].max())
    p.toolbar.logo = None
    p.toolbar_location = None
    p.title.text_color = "#000000"
    p.title.text_font_size = "1.5em"
    p.axis.major_label_text_color = "#000000"
    p.axis.major_label_text_font_size = "1.25em"
    p.xgrid.grid_line_color = None
    p.ygrid.grid_line_alpha = 0.5
    p.ygrid.grid_line_dash = [2, 4]
    p.outline_line_color = None
    p.yaxis.axis_label = "Close"

    return p
Example #3
0
def bokehplot(df_1, ticker):
    """Create a time-series line plot in Bokeh."""
    p = figure(width=600, height=300, title=ticker.upper(), tools="")

    hover = HoverTool(tooltips="""
    <div>
    <table>
    <tr><td class="ttlab">Date:</td><td>@date_str</td></tr>
    <tr><td class="ttlab">Close:</td><td>@close</td></tr>
    </table>
    </div>
    """)

    hover.mode = 'vline'
    hover.line_policy = 'nearest'
    p.add_tools(hover)

    crosshair = CrosshairTool()
    crosshair.dimensions = 'height'
    crosshair.line_color = "#ffffff"
    p.add_tools(crosshair)

    dfcds = ColumnDataSource(df_1)
    p.line('date', 'close', source=dfcds, color="#44ddaa")

    p.xaxis.formatter = DatetimeTickFormatter(days=["%d %b"])
    p.x_range = Range1d(df_1['date'].min(), df_1['date'].max())

    p.toolbar.logo = None
    p.toolbar_location = None

    # Style plot
    p.background_fill_color = "#234567"
    p.border_fill_color = "#234567"
    p.title.text_color = "#ffffff"
    p.title.text_font_size = "1.25em"
    p.axis.major_label_text_color = "#ffffff"
    p.axis.major_label_text_font_size = "0.875em"
    p.axis.axis_line_color = "#ffffff"
    p.axis.major_tick_line_color = "#ffffff"
    p.axis.minor_tick_line_color = "#ffffff"
    p.xgrid.grid_line_color = None
    p.ygrid.grid_line_alpha = 0.5
    p.ygrid.grid_line_dash = [4, 6]
    p.outline_line_color = None
    p.yaxis.axis_label = "Closing price"
    p.yaxis.axis_label_text_color = "#ffffff"
    p.yaxis.axis_label_text_font_size = "1em"
    p.yaxis.axis_label_text_font_style = "normal"
    p.yaxis.axis_label_standoff = 12

    return p
Example #4
0
def plot_ticker(ticker):
    # Retrieve and process data:
    
    url = urlhead + ticker + urltail
    page = requests.get(url)
    json = page.json()
    df = pd.DataFrame(json['Time Series (Daily)'])
    
    # New DataFrame to append values:
    df_1 = pd.DataFrame()
    close = np.asarray(df.iloc[3])
    
    df_1['date'] = pd.to_datetime(list(df))
    df_1['close'] = close
    
    # Last 30 days:
    df_1 = df_1[0:30]
    
    # Create a new column with dates as string:
    df_1['date_str'] = df_1['date'].map(lambda x: x.strftime("%Y-%m-%d"))
    dfcds = ColumnDataSource(df_1)
    
    # Create Bokeh plot:
    p = figure(width=600, height=300, title=ticker.upper(), tools="")

    hover = HoverTool(tooltips = [
        ('Date', '@date_str'),
        ('Close', '@close')])
    
    hover.mode = 'vline'
    hover.line_policy = 'nearest'
    p.add_tools(hover)

    crosshair = CrosshairTool()
    crosshair.dimensions = 'height'
    p.add_tools(crosshair)

    p.line('date', 'close', source =  dfcds)

    p.xaxis.formatter=DatetimeTickFormatter(days=["%d %b"])
    p.x_range=Range1d(df_1['date'].min(), df_1['date'].max())

    p.toolbar.logo = None
    p.toolbar_location = None

    return p
Example #5
0
def show_points(pointsList, w=400, h=400):

    # Ensure it's a list going in
    if type(pointsList) is not list:
        pointsList = [pointsList]

    # Create the hover tool
    hover = HoverTool(tooltips=[("x", "@x"),
                                ("y", "@y")])
    hover.line_policy = "nearest"

    # Make the figure
    fig = figure(plot_width=w, plot_height=h)
    fig.add_tools(hover)

    colors = ['red', 'orange', 'yellow', 'green',
              'blue', 'purple', 'indigo', 'violet']

    # Plot all the points
    for i, points in enumerate(pointsList):

        # Grab the color
        color = colors[i % len(colors)]

        # Plot the vertices
        fig.x(x=points[:, 0],
              y=points[:, 1],
              size=6, line_width=2, line_color=color)

        # Plot the lines
        x, y = [np.hstack([points[:, z], points[0, z]]) for z in [0, 1]]
        fig.line(x=x,
                 y=y,
                 line_width=2,
                 line_color=color)

        # Plot the point index next to the point
        fig.text(x=points[:, 0],
                 y=points[:, 1],
                 text=range(points.shape[0]))

    # Show the plot
    show(fig)
Example #6
0
def createHoverTool(simg, cols):
    """
    """
    # Make the hovertool only follow the patches (still a hack)
    htline = simg

    ht = HoverTool()
    ht.tooltips = [("Time", "@index{%F %T}")]
    for col in cols:
        fStr = "@%s{0.00}" % (col)
        ht.tooltips.append((col, fStr))

    ht.formatters = {'index': 'datetime'}
    ht.show_arrow = False
    ht.point_policy = 'follow_mouse'
    ht.line_policy = 'nearest'
    ht.renderers = [htline]

    return ht
Example #7
0
def create_figure():
    G1 = copy.deepcopy(G)
    nx.set_node_attributes(G1, dict(G1.degree(weight='Weight')), 'WDegree')
    nx.set_node_attributes(G1, dict(G1.degree()), 'Degree')
    rem_edg = []
    for u,v,d in G1.edges(data=True):
        if d['Weight'] < edf.value:
            rem_edg.append((u,v))
    G1.remove_edges_from(rem_edg)
    rem_node = []
    for u in G1.nodes():
        if G1.node[u]['WDegree'] < ndf.value:
            rem_node.append(u)
    rem_node = rem_node+list(nx.isolates(G1))
    G1.remove_nodes_from(rem_node)
    node_df = ColumnDataSource(nodes_df(G1))
    edge_df = ColumnDataSource(edges_df(G1))
    color_mapper = CategoricalColorMapper(factors=states, palette=Category20b[20] + Category20c[20] + Category20[20])
    mapper = LogColorMapper(palette=['#FDFDFD', '#FDF5CA', '#FDF2B1', '#FDEE98', '#FDE665', '#FDDF33', '#FDD700'],
                            low=min(edge_df.data['weight']), high=max(edge_df.data['weight']))

    p = figure(title="US Air Flights in 97", toolbar_location="left", plot_width=1100, plot_height=600,
               match_aspect=True, aspect_scale=0.7, x_range=[-140, -50], y_range=[20, 55])
    r1 = p.circle('x', 'y', source=node_df, size='size', level='overlay',
                  color={'field': 'state', 'transform': color_mapper},
                  alpha=0.8, line_color='#240606', line_width=1, line_alpha=0.3)
    r2 = p.multi_line('xs', 'ys', line_width='line_width', alpha='alphas',
                      color={'field': 'weight', 'transform': mapper},
                      source=edge_df)

    node_hover = HoverTool(tooltips=[('Name', '@Name'), ('State', '@state'), ('No. of Connections', '@Degree'),
                                     ('Total Frequency', '@WDegree')], renderers=[r1])
    node_hover.attachment = 'right'
    edge_hover = HoverTool(tooltips=[('Airports', '@city1'), ('', '@city2'), ('Frequency', '@weight')], renderers=[r2])
    edge_hover.line_policy = 'interp'
    edge_hover.attachment = 'left'
    p.add_tools(node_hover)
    p.add_tools(edge_hover)
    return p
Example #8
0
def plot_filtering(time, raw, filtered):
    """

    :param time:
    :param raw:
    :param filtered:
    :return:
    """

    hover = HoverTool()
    hover.point_policy = 'snap_to_data'
    hover.line_policy = 'nearest'
    TOOLS = ['pan,wheel_zoom,box_zoom']

    time = np.arange(0, raw.size)
    p1 = figure(title="Timeseries SSH (raw vs filtered)",
                tools=TOOLS,
                plot_width=1000)
    p1.grid.grid_line_alpha = 0.3
    p1.xaxis.axis_label = 'Date (Julian days)'
    p1.yaxis.axis_label = 'SSH'
    p1.circle(time, raw, color='lightseagreen')
    p1.line(time, raw, legend='raw', color='lightseagreen')
    p1.circle(time, filtered, color='royalblue')
    p1.line(time, filtered, legend='filtered',
            color='royalblue')  #, line_width=4)

    p1.add_tools(
        HoverTool(tooltips=[
            ('date', '@x'),
            ('SSH', '@y m'),
        ], mode='mouse'))

    p1.legend.location = "top_left"
    # output_file("Hss_timeseries.html", title="Hss_timeseries")
    show(p1, plot_width=1000, plot_height=400)
Example #9
0
def bokehTile(tileFile,
              jsonFile,
              TT=[0, 0, 0],
              DD=[2019, 10, 1],
              dynamic=False,
              plotTitle=''):
    citls, h = fitsio.read(tileFile, header=True)
    w = (np.where(citls['IN_DESI'] == 1)[0])
    inci = citls[w]

    if jsonFile is not None:
        with open(jsonFile, "r") as read_file:
            data = json.load(read_file)

    ## Coloring scheme
    palette = ['green', 'red', 'white']
    dye = []

    for tile in citls['TILEID']:

        rang = 2  # 'orange'

        if jsonFile is not None:
            if str(tile) in data:
                rang = 0  # 'green' #green default
                if len(data[str(tile)]
                       ['unassigned']) > 0:  # not assigned (red)
                    rang = 1  # 'red' #'red'
                if (0 in data[str(tile)]['gfa_stars_percam']):
                    print(data[str(tile)]['gfa_stars_percam'])
                    rang = 1  # 'cyan'
        else:
            rang = 0  # green if qa.json is not provided

        dye.append(rang)

    dye = np.asarray(dye)
    w = (np.where(dye < 2)[0])
    citls = citls[w]
    dye = dye[w]
    mapper = linear_cmap(field_name='DYE', palette=palette, low=0, high=2)

    #########################################################
    TOOLS = [
        'pan', 'tap', 'wheel_zoom', 'box_zoom', 'reset', 'save', 'box_select'
    ]

    obsTime = dt(DD[0], DD[1], DD[2], TT[0], TT[1], TT[2])
    # print(get_kp_twilights(TT,DD))

    if plotTitle == '' or plotTitle is None:
        PTITLE = ''
    else:
        PTITLE = 'Program: ' + plotTitle

    p = figure(tools=TOOLS,
               toolbar_location="right",
               plot_width=800,
               plot_height=450,
               title=PTITLE,
               active_drag='box_select')  # str(DD[1])+" - 2019")
    p.title.text_font_size = '16pt'
    p.title.text_color = 'black'
    p.grid.grid_line_color = "gainsboro"

    ###############################  adding ecliptic plane+ hour grid ####################3
    add_plane(p, color='red', plane='ecliptic', projection='equatorial')

    tiledata = dict(
        RA=citls['RA'],
        DEC=citls['DEC'],
        TILEID=citls['TILEID'],
        BRIGHTRA=citls['BRIGHTRA'][:, 0],
        BRIGHTDEC=citls['BRIGHTDEC'][:, 0],
        BRIGHTVTMAG=citls['BRIGHTVTMAG'][:, 0],
        EBV_MED=np.round(citls['EBV_MED'], 3),
        STAR_DENSITY=citls['STAR_DENSITY'],
        DYE=dye,
        program=citls['PROGRAM'],
        selected=np.ones(len(citls), dtype=bool),
    )

    for colname in ['STAR_DENSITY', 'EBV_MED']:
        if colname in citls.dtype.names:
            tiledata[colname] = citls[colname]

    tiles = ColumnDataSource(data=tiledata)

    colformat = bktables.NumberFormatter(format='0,0.00')
    columns = [
        bktables.TableColumn(field='TILEID', title='TILEID', width=80),
        bktables.TableColumn(field='RA', title='RA', formatter=colformat),
        bktables.TableColumn(field='DEC', title='DEC', formatter=colformat),
    ]

    for colname in ['STAR_DENSITY', 'EBV_MED']:
        if colname in tiledata:
            columns.append(
                bktables.TableColumn(field=colname,
                                     title=colname,
                                     formatter=colformat))

    columns.append(bktables.TableColumn(field='selected', title='Selected'))

    tiletable = bktables.DataTable(columns=columns, source=tiles, width=800)

    tiles.selected.js_on_change(
        'indices',
        CustomJS(args=dict(s1=tiles),
                 code="""
        var inds = cb_obj.indices;
        var d1 = s1.data;
        for (var i=0; i<d1['selected'].length; i++) {
            d1['selected'][i] = false;
        }
        for (var i = 0; i < inds.length; i++) {
            d1['selected'][inds[i]] = true;
        }
        s1.change.emit();
    """))

    render = p.circle(
        'RA',
        'DEC',
        source=tiles,
        size=9,
        line_color='chocolate',
        color=mapper,
        alpha=0.4,
        hover_color='orange',
        hover_alpha=1,
        hover_line_color='red',

        # set visual properties for selected glyphs
        selection_fill_color='orange',
        selection_line_color='white',
        # set visual properties for non-selected glyphs
        nonselection_fill_alpha=0.4,
        nonselection_fill_color=mapper)

    p.xaxis.axis_label = 'RA [deg]'
    p.yaxis.axis_label = 'Dec. [deg]'
    p.xaxis.axis_label_text_font_size = "14pt"
    p.yaxis.axis_label_text_font_size = "14pt"
    p.grid.grid_line_color = "gainsboro"
    p.yaxis.major_label_text_font_size = "12pt"
    p.xaxis.major_label_text_font_size = "12pt"

    p.x_range = Range1d(360, 0)
    p.y_range = Range1d(-40, 95)
    p.toolbar.logo = None
    p.toolbar_location = None

    # mytext = Label(x=180, y=-35, text="S", text_color='gray', text_font_size='12pt') ; p.add_layout(mytext)
    # mytext = Label(x=180, y=88, text="N", text_color='gray', text_font_size='12pt') ; p.add_layout(mytext)
    # mytext = Label(x=350, y=45, text="E", text_color='gray', text_font_size='12pt', angle=np.pi/2) ; p.add_layout(mytext)
    # mytext = Label(x=4, y=45, text="W", text_color='gray', text_font_size='12pt', angle=np.pi/2) ; p.add_layout(mytext)

    ## Javascript code to open up custom html pages, once user click on a tile
    code = """
        var index_selected = source.selected['1d']['indices'][0];
        var tileID = source.data['TILEID'][index_selected];
        if (tileID!==undefined) {
        var win = window.open("http://www.astro.utah.edu/~u6022465/cmx/ALL_SKY/dr8/allSKY_ci_tiles/sub_pages/tile-"+tileID+".html", " ");
        try {win.focus();} catch (e){} }
    """

    taptool = p.select(type=TapTool)
    taptool.callback = CustomJS(args=dict(source=tiles), code=code)

    ## The html code for the hover window that contain tile infrormation
    ttp = """
        <div>
            <div>
                <span style="font-size: 14px; color: blue;">Tile ID:</span>
                <span style="font-size: 14px; font-weight: bold;">@TILEID{int}</span>
            </div>
            <div>
                <span style="font-size: 14px; color: blue;">RA:</span>
                <span style="font-size: 14px; font-weight: bold;">@RA</span>
            </div>  
            <div>
                <span style="font-size: 14px; color: blue;">Dec:</span>
                <span style="font-size: 14px; font-weight: bold;">@DEC</span>
            </div>     
            <div>
                <span style="font-size: 14px; color: blue;">EBV_MED:</span>
                <span style="font-size: 14px; font-weight: bold;">@EBV_MED{0.000}</span>
            </div> 
            <div>
                <span style="font-size: 14px; color: blue;">STAR_DENSITY:</span>
                <span style="font-size: 14px; font-weight: bold;">@STAR_DENSITY{0}</span>
            </div> 
            <div>
                <span style="font-size: 14px; color: blue;">BRIGHTEST_STAR_VTMAG:</span>
                <span style="font-size: 14px; font-weight: bold;">@BRIGHTVTMAG</span>
            </div> 
            <div>
                <span style="font-size: 14px; color: blue;">BRIGHTEST_STAR_LOC:</span>
                <span style="font-size: 14px; font-weight: bold;">(@BRIGHTRA, @BRIGHTDEC)</span>
            </div> 
        </div>
    """

    hover = HoverTool(tooltips=ttp, renderers=[render])

    hover.point_policy = 'snap_to_data'
    hover.line_policy = 'nearest'
    # hover.mode='vline'
    p.add_tools(hover)

    cross = CrosshairTool()
    # cross.dimensions='height'
    cross.line_alpha = 0.3
    cross.line_color = 'gray'
    p.add_tools(cross)

    # Setting the second y axis range name and range
    p.extra_y_ranges = {"foo": p.y_range}
    p.extra_x_ranges = {"joo": p.x_range}

    # Adding the second axis to the plot.
    p.add_layout(LinearAxis(y_range_name="foo"), 'right')
    p.add_layout(LinearAxis(x_range_name="joo"), 'above')

    p.xaxis.major_label_text_font_size = "12pt"
    p.yaxis.major_label_text_font_size = "12pt"

    if dynamic:

        # twilight_source = get_kp_twilights(TT,DD)   # evening and morning twilights at every TT and DD
        circleSource_1 = skyCircle(TT, DD, 1.5)
        p.circle('RA', 'DEC', source=circleSource_1, size=1.5, color='black')
        circleSource_2 = skyCircle(TT, DD, 2.0)
        p.circle('RA', 'DEC', source=circleSource_2, size=0.5, color='gray')

    else:
        circleSource = skyCircle(TT, DD, 1.5)
        p.circle('RA', 'DEC', source=circleSource, size=1.5, color=None)

    ### Dealing with the Moon and Jupiter
    inFile = 'moonLoc_jupLoc_fracPhase.csv'  # 'moon_loc_jup_loc_fracPhase_namePhase.csv'
    tbl_moon_jup = np.genfromtxt(inFile,
                                 delimiter=',',
                                 filling_values=-1,
                                 names=True,
                                 dtype=None)  # , dtype=np.float)

    loc = EarthLocation.of_site('Kitt Peak')
    kp_lat = 31, 57, 48
    kp_lon = -111, 36, 00
    mooninfo_obj = pylunar.MoonInfo((kp_lat), (kp_lon))

    m_ra, m_dec, frac_phase, name_phase = moonLoc(TT, DD, loc, mooninfo_obj)
    j_ra, j_dec = jupLoc(TT, DD, loc)

    #moonSource = ColumnDataSource({"moon_RAS": tbl_moon_jup['moon_ra'], "moon_DECS": tbl_moon_jup['moon_dec'],
    #                               "Phase_frac": tbl_moon_jup['moon_phase_frac']})

    moonSource = ColumnDataSource({
        "moon_RAS":
        tbl_moon_jup['moon_ra'],
        "moon_DECS":
        tbl_moon_jup['moon_dec'],
        "Phase_frac":
        np.round(100 * tbl_moon_jup['moon_phase_frac'])
    })

    ####moon_RADEC = ColumnDataSource({"moon_ra": [m_ra.deg], "moon_dec": [m_dec.deg], "phase_frac": [frac_phase]})
    moon_RADEC_ = ColumnDataSource({
        "moon_ra": [m_ra.deg - 360],
        "moon_dec": [m_dec.deg],
        "phase_frac": [frac_phase]
    })
    moon_RADEC = ColumnDataSource({
        "moon_ra": [m_ra.deg],
        "moon_dec": [m_dec.deg],
        "phase_frac": [frac_phase]
    })

    render_moon = p.circle('moon_ra',
                           'moon_dec',
                           source=moon_RADEC,
                           size=170,
                           color='cyan',
                           alpha=0.2)
    render_moon = p.circle('moon_ra',
                           'moon_dec',
                           source=moon_RADEC,
                           size=4,
                           color='blue')

    render_moon = p.circle('moon_ra',
                           'moon_dec',
                           source=moon_RADEC_,
                           size=170,
                           color='cyan',
                           alpha=0.2)
    render_moon = p.circle('moon_ra',
                           'moon_dec',
                           source=moon_RADEC_,
                           size=4,
                           color='blue')

    jupSource = ColumnDataSource({
        "jup_RAS": tbl_moon_jup['jup_ra'],
        "jup_DECS": tbl_moon_jup['jup_dec']
    })
    jup_RADEC = ColumnDataSource({
        "jup_ra": [j_ra.deg],
        "jup_dec": [j_dec.deg]
    })

    twilight = get_kp_twilights(
        TT, DD)  # evening and morning twilights at every TT and DD
    twilight_source = ColumnDataSource({
        "eve_twilight": [twilight[0]],
        "mor_twilight": [twilight[1]]
    })

    render_jup = p.circle('jup_ra',
                          'jup_dec',
                          source=jup_RADEC,
                          size=5,
                          color='blue')
    render_jup = p.circle('jup_ra',
                          'jup_dec',
                          source=jup_RADEC,
                          size=4,
                          color='gold')

    from bokeh.models.glyphs import Text
    TXTsrc = ColumnDataSource(
        dict(x=[350],
             y=[85],
             text=['Moon Phase: ' + "%.0f" % (frac_phase * 100) + "%"]))
    glyph = Text(x="x", y="y", text="text", angle=0, text_color="black")
    p.add_glyph(TXTsrc, glyph)

    TXTsrc_moon = ColumnDataSource(
        dict(x=[m_ra.deg + 10], y=[m_dec.deg - 10], text=['Moon']))
    glyph = Text(x="x",
                 y="y",
                 text="text",
                 angle=0,
                 text_color="blue",
                 text_alpha=0.3,
                 text_font_size='10pt')
    p.add_glyph(TXTsrc_moon, glyph)

    TXTsrc_jup = ColumnDataSource(
        dict(x=[j_ra.deg + 5], y=[j_dec.deg - 8], text=['Jup.']))
    glyph = Text(x="x",
                 y="y",
                 text="text",
                 angle=0,
                 text_color="black",
                 text_alpha=0.3,
                 text_font_size='10pt')
    p.add_glyph(TXTsrc_jup, glyph)

    callback = CustomJS(args=dict(source_sky1=circleSource_1,
                                  source_sky2=circleSource_2,
                                  source_moon=moonSource,
                                  source_moon_RADEC=moon_RADEC,
                                  source_moon_RADEC_=moon_RADEC_,
                                  source_jup=jupSource,
                                  source_jup_RADEC=jup_RADEC,
                                  sourceTXT=TXTsrc,
                                  sourceTXTmoon=TXTsrc_moon,
                                  sourceTXTjup=TXTsrc_jup),
                        code="""
                // First set times as if they were UTC
                var t = new Date(time_slider.value);
                var d = new Date(date_slider.value);
                var data1 = source_sky1.data;
                var ra_1 = data1['RA'];
                var ra0_1 = data1['RA0'];
                
                var data2 = source_sky2.data;
                var ra_2 = data2['RA'];
                var ra0_2 = data2['RA0'];
                
                var data_moon = source_moon.data;
                var ras_moon = data_moon['moon_RAS'];
                var decs_moon = data_moon['moon_DECS'];
                var phase_frac = data_moon['Phase_frac'];
                
                var moonRADEC = source_moon_RADEC.data;
                var moon_ra = moonRADEC['moon_ra'];
                var moon_dec = moonRADEC['moon_dec'];
     
                var moonRADEC_ = source_moon_RADEC_.data;
                var moon_ra_ = moonRADEC_['moon_ra'];
                var moon_dec_ = moonRADEC_['moon_dec'];
                
                var data_jup = source_jup.data;
                var ras_jup = data_jup['jup_RAS'];
                var decs_jup = data_jup['jup_DECS'];
                
                var jupRADEC = source_jup_RADEC.data;
                var jup_ra = jupRADEC['jup_ra'];
                var jup_dec = jupRADEC['jup_dec'];


                var Hour  = t.getUTCHours();
                var Day   = d.getDate();
                var Month = d.getMonth();
                
                var Year = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
                var all_FULdays = 0;
                for (var i = 0; i < Month; i++)
                    all_FULdays=all_FULdays+Year[i];
                all_FULdays = all_FULdays + (Day-1);
                
                if (Hour<12) all_FULdays=all_FULdays+1;
                
                var all_minutes = all_FULdays*24+Hour;
                
                if (all_minutes<8800) {
                    moon_ra[0] = ras_moon[all_minutes];
                    moon_dec[0] = decs_moon[all_minutes];   
                    moon_ra_[0] = ras_moon[all_minutes]-360.;
                    moon_dec_[0] = decs_moon[all_minutes];   
                                        
                }


                var jupTXTdata = sourceTXTjup.data;
                var x_jup = jupTXTdata['x'];
                var y_jup = jupTXTdata['y'];
                var text_jup = jupTXTdata['text'];  
                
                
                if (all_minutes<8800) {
                    jup_ra[0] = ras_jup[all_minutes];
                    jup_dec[0] = decs_jup[all_minutes];   
                    x_jup[0] = jup_ra[0]+5;
                    y_jup[0] = jup_dec[0]-8;                     
                }
                 
                                
                if (t.getUTCHours() < 12) {
                    d.setTime(date_slider.value + 24*3600*1000);
                } else {
                    d.setTime(date_slider.value);
                }
                d.setUTCHours(t.getUTCHours());
                d.setUTCMinutes(t.getUTCMinutes());
                d.setUTCSeconds(0);        
                
                // Correct to KPNO local time
                // d object still thinks in UTC, which is 7 hours ahead of KPNO
                d.setTime(d.getTime() + 7*3600*1000);
                // noon UT on 2000-01-01
                var reftime = new Date();
                reftime.setUTCFullYear(2000);
                reftime.setUTCMonth(0);   // Months are 0-11 (!)
                reftime.setUTCDate(1);    // Days are 1-31 (!)
                reftime.setUTCHours(12);
                reftime.setUTCMinutes(0);
                reftime.setUTCSeconds(0);
                
                // time difference in days (starting from milliseconds)
                var dt = (d.getTime() - reftime.getTime()) / (24*3600*1000);

                // Convert to LST
                var mayall_longitude_degrees = -(111 + 35/60. + 59.6/3600);
                var LST_hours = ((18.697374558 + 24.06570982441908 * dt) + mayall_longitude_degrees/15) % 24;
                var LST_degrees = LST_hours * 15;
                
                

                for (var i = 0; i < ra_1.length; i++) {
                    ra_1[i] = (ra0_1[i] + LST_degrees) % 360;
                }
                
                for (var i = 0; i < ra_2.length; i++) {
                    ra_2[i] = (ra0_2[i] + LST_degrees) % 360;
                }                

                //// Here we gtake care of the moon phasde text
                var TXTdata = sourceTXT.data;
                var x = TXTdata['x'];
                var y = TXTdata['y'];
                var text = TXTdata['text'];
                
                var moonTXTdata = sourceTXTmoon.data;
                var x_moon = moonTXTdata['x'];
                var y_moon = moonTXTdata['y'];
                var text_moon = moonTXTdata['text'];   

                // x[0] = 1;
                // y[0] = 40;
                if (all_minutes<8800) {
                    text[0] = 'Moon Phase: ' + phase_frac[all_minutes]+'%';
                    x_moon[0] = moon_ra[0]+10;
                    y_moon[0] = moon_dec[0]-10;
                }

                sourceTXT.change.emit();
                /////////////////////////////// Moon phase code ends.

                source_sky1.change.emit();
                source_sky2.change.emit();
                //source_moon_RADEC.change.emit();
                //source_moon_RADEC_.change.emit();
                //source_jup_RADEC.change.emit();
                sourceTXTmoon.change.emit();
                sourceTXTjup.change.emit();

                //alert(d);
    """)

    if dynamic:
        ### TIME
        Timeslider = DateSlider(start=dt(2019, 9, 1, 16, 0, 0),
                                end=dt(2019, 9, 2, 8, 0, 0),
                                value=dt(2019, 9, 1, 16, 0, 0),
                                step=1,
                                title="KPNO local time(hh:mm)",
                                format="%H:%M",
                                width=800)

        ## DATE
        Dateslider = DateSlider(start=dt(2019, 9, 1, 16, 0, 0),
                                end=dt(2020, 8, 31, 8, 0, 0),
                                value=dt(2019, 10, 1, 16, 0, 0),
                                step=1,
                                title="Date of sunset(4pm-8am)",
                                format="%B:%d",
                                width=800)

        callback.args['time_slider'] = Timeslider
        callback.args['date_slider'] = Dateslider

        Dateslider.js_on_change('value', callback)
        Timeslider.js_on_change('value', callback)

        layout = column(p, Dateslider, Timeslider, tiletable)
        # show(p)
        return layout

    return p
Example #10
0
def plot_model(
        model,
        x_column,
        y_column=None,
        fig=None,
        x_slider=None,
        y_slider=None,
        show_expt_data=True,
        figsize=(10, 10),
        dpi=100,
        **kwargs,
):
    """
    Plots a `model` object with a given model input as `x_column` against
    the model's output: `y_column`. If `y_column` is not specified, then it
    found from the `model`.

    For model's with more than 1 inputs, the `y_column` is the variable to be
    plotted on the y-axis, and then the plot type is a contour plot.
    """
    pure_factors = model.get_factor_names(level=1)
    dpi_max = dpi**3.5  # should be reasonable for most modern computers
    per_axis_points = min(dpi, np.power(dpi_max, 1 / len(pure_factors)))

    # `oneD=True`: the x-variable is a model input, and the y-axis is a response
    oneD = False
    if y_column and y_column not in pure_factors:
        oneD = True
        y_column = model.get_response_name()

    param_names = [
        model.get_response_name(),
    ]
    param_names.extend(model.get_factor_names())

    # Not always a great test: y = I(1/d) does not pick up that "d" is the model
    # even though it is via the term encapsulated in I(...)
    # assert x_column in param_names, "x_column must exist in the model."
    assert y_column in param_names, "y_column must exist in the model."

    xrange = model.data[x_column].min(), model.data[x_column].max()
    xdelta = xrange[1] - xrange[0]
    xlim = kwargs.get("xlim",
                      (xrange[0] - xdelta * 0.05, xrange[1] + xdelta * 0.05))
    h_grid = np.linspace(xlim[0], xlim[1], num=per_axis_points)
    plotdata = {x_column: h_grid}

    if not oneD:
        yrange = model.data[y_column].min(), model.data[y_column].max()
        ydelta = yrange[1] - yrange[0]
        ylim = kwargs.get(
            "ylim", (yrange[0] - ydelta * 0.05, yrange[1] + ydelta * 0.05))

        v_grid = np.linspace(ylim[0], ylim[1], num=per_axis_points)
        H, V = np.meshgrid(h_grid, v_grid)
        h_grid, v_grid = H.ravel(), V.ravel()
        plotdata[x_column] = h_grid
        plotdata[y_column] = v_grid

    # TODO: handle the 2D case later

    # if other_factors is not None and isinstance(other_factors, dict):
    # plotdata = kwargs.update(other_factors)

    ## Look at which factors are included, and pop them out. The remaining
    ## factors are specified at their zero level

    # unspecified_factors = [i for i in pure_factors if i not in kwargs.keys()]
    # for factor in unspecified_factors:
    # plotdata[factor] = np.zeros_like(h_grid)

    # assert sorted(kwargs.keys()) == sorted(pure_factors), ("Not all factors "
    # "were specified.")

    Z = predict(model, **plotdata)

    if not oneD:
        assert False
    else:
        plotdata[y_column] = Z
        yrange = Z.min(), Z.max()
        ydelta = yrange[1] - yrange[0]
        ylim = kwargs.get(
            "ylim", (yrange[0] - ydelta * 0.05, yrange[1] + ydelta * 0.05))

    if fig:
        p = fig
        prior_figure = True
    else:
        prior_figure = False
        p = figure(
            x_range=xlim,
            y_range=ylim,
            # https://github.com/bokeh/bokeh/issues/2351
            tools="pan,wheel_zoom,box_zoom, box_select,lasso_select,reset,save",
        )

    h_line = p.line(
        plotdata[x_column],
        plotdata[y_column],
        line_dash="solid",
        color=kwargs.get("color", "black"),
        line_width=kwargs.get("line_width", 2),
    )
    y_units = model.data.pi_units[y_column]

    tooltips = [(x_column, "$x")]
    if y_units:
        tooltips.append((f"Prediction of {y_column} [{y_units}]", "$y"))
    else:
        tooltips.append((f"Prediction of {y_column}", "$y"))

    tooltips.append(("Source", model.name or ""))

    # custom tooltip for the predicted prediction line
    h1 = HoverTool(tooltips=tooltips, renderers=[h_line])
    h1.line_policy = "nearest"

    if show_expt_data:
        source = ColumnDataSource(data=dict(
            x=model.data[x_column],
            y=model.data[y_column],
            output=model.data[model.get_response_name()].to_list(),
        ))
        h_expts = p.circle(
            x="x",
            y="y",
            color="black",
            source=source,
            size=10,
            line_width=2,
            name="Experimental_points",
        )
        h2 = HoverTool(
            tooltips=[
                (x_column, "$x"),
                (y_column, "$y"),
                ("Experimental value", "@output"),
            ],
            renderers=[h_expts],
        )

        h2.point_policy = "snap_to_data"
        h2.line_policy = "none"

    # Axis labels:
    p.xaxis.axis_label_text_font_size = "14pt"
    p.xaxis.axis_label = x_column
    p.xaxis.major_label_text_font_size = "14pt"
    p.xaxis.axis_label_text_font_style = "bold"

    p.yaxis.major_label_text_font_size = "14pt"
    p.yaxis.axis_label = y_column
    p.yaxis.axis_label_text_font_size = "14pt"
    p.yaxis.axis_label_text_font_style = "bold"
    if prior_figure:

        # p.xaxis.bounds =
        p.x_range = Range1d(
            min(xlim[0], p.x_range.start, min(model.data[x_column])),
            max(xlim[1], p.x_range.end, max(model.data[x_column])),
        )
        p.y_range = Range1d(
            min(ylim[0], p.y_range.start, min(model.data[y_column])),
            max(ylim[1], p.y_range.end, max(model.data[y_column])),
        )

    else:
        p.x_range = Range1d(xlim[0], xlim[1])
        p.y_range = Range1d(ylim[0], ylim[1])

    # Add the hover tooltips:
    p.add_tools(h1)
    p.add_tools(h2)

    show_plot(p)
    return p
Example #11
0
def contour_plot_bokeh(
        model,
        xlabel=None,
        ylabel=None,
        main=None,
        xlim=(-3.2, 3.2),
        ylim=(-3.2, 3.2),
        colour_function="terrain",
        show=True,
        show_expt_data=True,
        figsize=(10, 10),
        dpi=50,
        other_factors=None,
):

    # TODO: show labels of contour plot

    # https://stackoverflow.com/questions/33533047/how-to-make-a-contour-plot-in-python-using-bokeh-or-other-libs

    dpi_max = dpi**3.5
    N = min(dpi, np.power(dpi_max, 0.5))

    h_grid = np.linspace(xlim[0], xlim[1], num=N)
    v_grid = np.linspace(ylim[0], ylim[1], num=N)
    H, V = np.meshgrid(h_grid, v_grid)
    h_grid, v_grid = H.ravel(), V.ravel()

    pure_factors = model.get_factor_names(level=1)
    if xlabel is None:
        xlabel = pure_factors[0]
    else:
        xlabel = str(xlabel)

    if ylabel is None:
        ylabel = pure_factors[1]
    else:
        ylabel = str(ylabel)

    kwargs = {xlabel: h_grid, ylabel: v_grid}
    if other_factors is not None and isinstance(other_factors, dict):
        kwargs = kwargs.update(other_factors)

    # Look at which factors are included, and pop them out. The remaining
    # factors are specified at their zero level

    unspecified_factors = [i for i in pure_factors if i not in kwargs.keys()]
    for factor in unspecified_factors:
        kwargs[factor] = np.zeros_like(h_grid)

    assert sorted(kwargs.keys()) == sorted(pure_factors), ("Not all factors "
                                                           "were specified.")
    Z = predict(model, **kwargs)
    Z = Z.values.reshape(N, N)
    z_min, z_max = Z.min(), Z.max()
    levels = np.linspace(z_min, z_max, N)

    from matplotlib.pyplot import contour, clabel
    import matplotlib.pyplot as plt
    import matplotlib

    matplotlib.use("Agg")
    # Turn interactive plotting off
    plt.ioff()
    CS = contour(H, V, Z, levels=levels, linestyles="dotted")
    clabel(CS, inline=True, fontsize=10, fmt="%1.0f")
    # contour_labels = [(float(q._x), float(q._y), float(q._text))\
    #                                                 for q in CS.labelTexts]

    # Convert the Matplotlib colour mapper to Bokeh
    # https://stackoverflow.com/questions/49931311/using-matplotlibs-colormap-for-bokehs-color-bar
    mapper = getattr(cm, colour_function)
    colours = (255 * mapper(range(256))).astype("int")
    colour_palette = [RGB(*tuple(rgb)).to_hex() for rgb in colours]
    color_mapper = LinearColorMapper(palette=colour_palette,
                                     low=z_min,
                                     high=z_max)

    # Another alternative:
    # https://stackoverflow.com/questions/35315259/using-colormap-with-bokeh-scatter
    # colors = ["#%02x%02x%02x" % (int(r), int(g), int(b)) for \
    #        r, g, b, _ in 255*mpl.cm.viridis(mpl.colors.Normalize()(radii))]

    p = figure(
        x_range=xlim,
        y_range=ylim,
        # https://github.com/bokeh/bokeh/issues/2351
        tools="pan,wheel_zoom,box_zoom,box_select,lasso_select,reset,save",
    )
    # Create the image layer
    source = {"Xax": [h_grid], "Yax": [v_grid], "predictions": [Z]}
    h_image = p.image(
        source=source,
        image="predictions",
        x=xlim[0],
        y=ylim[0],
        dw=xlim[1] - xlim[0],
        dh=ylim[1] - ylim[0],
        color_mapper=color_mapper,
        global_alpha=0.5,  # with some transparency
        name="contour_image",
    )
    h1 = HoverTool(
        tooltips=[
            (xlabel, "@{Xax}{0.4g}"),
            (ylabel, "@{Yax}{0.4f}"),
            ("Predicted", "@{predictions}{0.4g}"),
        ],
        renderers=[h_image],
        formatters={
            "Predicted": "printf",
            xlabel: "printf",
            ylabel: "printf"
        },
    )

    color_bar = ColorBar(
        color_mapper=color_mapper,
        major_label_text_font_size="8pt",
        ticker=BasicTicker(max_interval=(z_max - z_min) / N * 2),
        formatter=PrintfTickFormatter(format="%.2f"),
        label_standoff=6,
        border_line_color=None,
        location=(0, 0),
    )

    p.add_layout(color_bar, "right")

    # Contour lines using Scipy:
    # scaler_y = (ylim[1] - ylim[0]) / (N - 1)
    # scaler_x = (xlim[1] - xlim[0]) / (N - 1)
    # for level in levels:
    # contours = measure.find_contours(Z, level)
    # for contour in contours:
    # x = contour[:, 1] * scaler_y + ylim[0]
    # y = contour[:, 0] * scaler_x + xlim[0]

    for _, cccontour in enumerate(CS.allsegs):
        if cccontour:
            x = cccontour[0][:, 0]
            y = cccontour[0][:, 1]
            p.line(x, y, line_dash="dashed", color="darkgrey", line_width=1)

    # TODO: bigger experimental markers
    # TODO: hover for the data point shows the factor settings for the data point

    if show_expt_data:

        source = ColumnDataSource(data=dict(
            x=model.data[xlabel],
            y=model.data[ylabel],
            output=model.data[model.get_response_name()].to_list(),
        ))
        h_expts = p.circle(
            x="x",
            y="y",
            color="black",
            source=source,
            # linestyle='',
            # marker='o',
            size=10,
            line_width=2,
            name="experimental_points",
        )
        # custom tooltip for the experimental points
        h2 = HoverTool(
            tooltips=[
                (xlabel, "$x{0.4g}"),
                (ylabel, "$y{0.4g}"),
                ("Actual value", "@{output}{0.4g}"),  # why not working???
            ],
            renderers=[h_expts],
            formatters={
                "Actual value": "printf",
                xlabel: "printf",
                ylabel: "printf"
            },
        )
        h2.point_policy = "snap_to_data"
        h2.line_policy = "none"

    # Axis labels:
    p.xaxis.axis_label_text_font_size = "14pt"
    p.xaxis.axis_label = xlabel
    p.xaxis.major_label_text_font_size = "14pt"
    p.xaxis.axis_label_text_font_style = "bold"
    p.xaxis.bounds = (xlim[0], xlim[1])

    p.yaxis.major_label_text_font_size = "14pt"
    p.yaxis.axis_label = ylabel
    p.yaxis.axis_label_text_font_size = "14pt"
    p.yaxis.axis_label_text_font_style = "bold"
    p.yaxis.bounds = (ylim[0], ylim[1])

    # Add the hover tooltips:
    p.add_tools(h1)
    p.add_tools(h2)

    if show:
        show_plot(p)
    return p
Example #12
0
# instantiate shared generic plot settings
# hover tool
hoverm = HoverTool(tooltips=[
    ("Age", "(@Age)"),
    ("Incidents", "(@Males{0,0.000})"),
],
                   mode='vline')

hoverf = HoverTool(tooltips=[
    ("Age", "(@Age)"),
    ("Incidents", "(@Females{0,0.000})"),
],
                   mode='hline')

hoverm.point_policy = 'snap_to_data'
hoverm.line_policy = 'nearest'

# graph tool bar
toolsm = ["box_select", hoverm, "reset"]
toolsf = ["box_select", hoverf, "reset"]


#create the main plot
def create_figure(*args):
    #create new plot with sizing & labels
    mplot = figure(x_range=label,
                   plot_width=900,
                   plot_height=400,
                   tools=toolsm,
                   title="Cancer Incidents in Males 2016")
    #remove bokeh logo, add axis labels
std = np.std(delta)
rms = np.sqrt(np.mean(delta**2))

date = datetime.date.today().strftime("%B") + " " + datetime.date.today(
).strftime("%d") + ", " + datetime.date.today().strftime("%Y")

output_file("test.html")

hover = HoverTool(tooltips=[
    ("inc_x", "@inc_x"),
    ("inc_y", "@inc_y"),
    ("PGC", "@PGC"),
])

hover.point_policy = 'snap_to_data'
hover.line_policy = 'nearest'  #'prev'

TOOLS = [hover, 'pan', 'tap', 'wheel_zoom', 'box_zoom', 'reset', 'save']

p = figure(tools=TOOLS,
           toolbar_location="below",
           logo="grey",
           plot_width=500,
           plot_height=400,
           title="Last update: " + date)

p.grid.grid_line_color = "gainsboro"

p.line([0, 100], [0, 100], line_width=2, color="black", legend="equality")
p.line([0, 100], [5, 105],
       line_width=1,
Example #14
0
def plot_classification_probabilities(
    x,
    y,
    cv_results,
    output_html=None,
    width=500,
    height=500,
    sizing_mode="stretch_both",
):
    """Plot the classification probabilities for each cross-validation split

    Parameters
    ----------
    x : numpy.ndarray
        The original feature matrix

    y : numpy.ndarray
        The target array (i.e. "ground truth")

    cv_results : list of SGLResult namedtuples
        Results of each cross-validation split

    output_html : string or None, default=None
        Filename for bokeh html output. If None, figure will not be saved

    width : int, default=500
        Width of each beta plot (in pixels)

    height : int, default=500
        Height of each beta plot (in pixels)

    sizing_mode : string
        One of ("fixed", "stretch_both", "scale_width", "scale_height",
        "scale_both"). Specifies how will the items in the layout resize to
        fill the available space. Default is "stretch_both". For more
        information on the different modes see
        https://bokeh.pydata.org/en/latest/docs/reference/models/layouts.html#bokeh.models.layouts.LayoutDOM
    """
    p = figure(plot_width=width, plot_height=height, toolbar_location="above")
    p.title.text = "Classification probabilities for each CV split"
    p.add_layout(
        Title(
            text="Click on legend entries to hide/show corresponding lines",
            align="left",
        ),
        "right",
    )

    names = ["cv_idx = {i:d}".format(i=i) for i in range(len(cv_results))]

    hover = HoverTool(tooltips=[("Subject", "$index")], mode="vline")
    hover.point_policy = "snap_to_data"
    hover.line_policy = "nearest"

    for res, color, name in zip(cv_results, Spectral10, names):
        p.line(
            np.arange(len(y)),
            _sigmoid(x.dot(res.beta_hat)),
            line_width=2,
            color=color,
            alpha=0.8,
            legend=name,
        )

    p.line(np.arange(len(y)), y, line_width=3, alpha=0.8, legend="ground truth")
    p.line(
        np.arange(len(y)),
        0.5 * np.ones(len(y)),
        line_width=2,
        line_dash="dashed",
        alpha=0.8,
        legend="threshold",
    )
    p.add_tools(hover)
    p.legend.location = "top_right"
    p.legend.click_policy = "hide"
    p.xaxis.axis_label = "Subject ID"
    p.xaxis.major_tick_line_color = None  # turn off x-axis major ticks
    p.xaxis.minor_tick_line_color = None  # turn off x-axis minor ticks
    p.xaxis.major_label_text_font_size = "0pt"  # turn off x-axis tick labels
    p.yaxis.axis_label = "Classification Probability"
    p.sizing_mode = sizing_mode

    if output_html is not None:
        html = file_html(p, CDN, "my plot")
        with open(op.abspath(output_html), "w") as fp:
            fp.write(html)
    else:
        show(p)
Example #15
0
def rca2(functionNode):
    logger = functionNode.get_logger()
    logger.info("==>>>> in rca2 (root cause analysis " +
                functionNode.get_browse_path())
    progressNode = functionNode.get_child("control").get_child("progress")
    progressNode.set_value(0.1)
    m = functionNode.get_model()

    report = '<i>REPORT</i><br><div style="font-size:85%">'

    annotations = functionNode.get_child("annotations").get_leaves()
    #order = ["Step"+str(no) for no in range(1,19)]
    order = ["Phase" + str(no) for no in range(3, 28)]
    order = functionNode.get_child("annotationsOrder").get_value()
    annotations = data_cleaning(annotations, order=order,
                                logger=logger)  #Step1,Step2,...Step18
    report += (f"found {len(annotations)} valid processes <br>")

    #for now, flatten them out
    annotations = [
        subprocess for process in annotations for subprocess in process
    ]

    algo = functionNode.get_child("selectedAlgorithm").get_value()
    target = functionNode.get_child("selectedTarget").get_target()

    progressNode.set_value(0.3)
    #now we are building up the table by iterating all the children in "selection"
    entries = functionNode.get_child("selection").get_children()

    table = {"target": []}
    firstVariable = True

    for entry in entries:
        logger.debug(f"entry {entry.get_name()}")
        #each entry is a combination of variable, tags and feature
        vars = entry.get_child("selectedVariables").get_targets()
        tags = entry.get_child("selectedTags").get_value()
        features = entry.get_child("selectedFeatures").get_value()
        #for iterate over variables
        for var in vars:
            logger.debug(
                f"processing variable: {var.get_name()} with tags {tags} and features {features}"
            )
            #columnName = var.get_name()+str(tags)+m.getRandomId()
            for tag in tags:
                row = 0
                #table[columnName]=[]# make a column
                for idx, anno in enumerate(annotations):
                    if anno.get_child("type").get_value() != "time":
                        continue
                    if tag in anno.get_child("tags").get_value():
                        startTime = anno.get_child("startTime").get_value()
                        endTime = anno.get_child("endTime").get_value()
                        data = var.get_time_series(startTime,
                                                   endTime)["values"]
                        #we take only the values "inside" the annotation
                        if len(data) > 2:
                            data = data[1:-1]
                        #now create the features
                        for feature in features:
                            feat = calc_feature(data, feature)
                            columnName = var.get_name(
                            ) + "_" + tag + "_" + feature
                            if not columnName in table:
                                table[columnName] = []
                            table[columnName].append(feat)

                        targetValue = get_target(
                            target,
                            (date2secs(startTime) + date2secs(endTime)) / 2)
                        if targetValue:
                            if firstVariable:
                                #for the first variable we also write the target
                                table["target"].append(targetValue)
                            else:
                                #for all others we make sure we have the same target value for that case (sanity check)
                                if table["target"][row] != targetValue:
                                    logger.warning(
                                        f'problem target {table["target"][row]} !=> {targetValue}'
                                    )
                            row = row + 1
                        else:
                            logger.warning(
                                f"no corrrect target value for {startTime} - {endTime}"
                            )

                firstVariable = False
    #now we have the table, plot it
    import json
    #print(json.dumps(table,indent=2))
    progressNode.set_value(0.5)
    #try a model

    algo = functionNode.get_child("selectedAlgorithm").get_value()
    if algo == "lasso":
        reg = linear_model.LassoCV()
        report += " using lasso Regression with auto-hyperparams <br>"
    else:
        #default
        report += " using linear Regression <br>"
        reg = linear_model.LinearRegression()  #try rigde, lasso

    columnNames = []
    dataTable = []
    for k, v in table.items():
        if k == "target":
            continue
        dataTable.append(v)
        columnNames.append(k)

    dataTable = numpy.asarray(dataTable)
    x = dataTable.T
    y = table["target"]
    x_train, x_test, y_train, y_test = train_test_split(x, y)
    reg.fit(x_train, y_train)

    print(reg.coef_)
    y_hat = reg.predict(x_test)
    y_repeat = reg.predict(x_train)
    print(f"predict: {y_hat} vs real: {y_test}")

    #check over/underfitting
    r_train = r2_score(y_train, y_repeat)
    r_test = r2_score(y_test, y_hat)

    report += "R<sup>2</sup> train= %.4g, R<sup>2</sup> test = %.4g <br>" % (
        r_train, r_test)

    pearsons = []
    for col in x.T:
        pearsons.append(pearsonr(col, y)[0])

    #and finally the correlations between y and yhat
    y_pearson_train = pearsonr(y_train, y_repeat)[0]
    y_pearson_test = pearsonr(y_test, y_hat)[0]

    report += "pearsonCorr y/y_hat train:%.4g , test:%.4g <br>" % (
        y_pearson_train, y_pearson_test)

    report += "regression coefficients, pearsons correlations:<br>"
    for col, coef, pear in zip(columnNames, reg.coef_, pearsons):
        report += "&nbsp &nbsp %s:%.4g, &nbsp %.4g <br>" % (col, coef, pear)

    #write report
    progressNode.set_value(0.8)
    report += "<div>"  #close the style div
    functionNode.get_child("report").set_value(report)
    #make a plot
    hover1 = HoverTool(tooltips=[('x,y', '$x,$y')], mode='mouse')
    hover1.point_policy = 'snap_to_data'
    hover1.line_policy = "nearest"
    tools = [
        PanTool(),
        WheelZoomTool(),
        BoxZoomTool(),
        ResetTool(),
        SaveTool(), hover1
    ]
    title = "prediction results on " + functionNode.get_child(
        "selectedAlgorithm").get_value()
    fig = figure(title=title, tools=tools, plot_height=400, plot_width=500)
    fig.toolbar.logo = None

    curdoc().theme = Theme(json=themes.darkTheme)
    fig.xaxis.major_label_text_color = themes.darkTickColor
    fig.yaxis.major_label_text_color = themes.darkTickColor
    fig.xaxis.axis_label = target.get_name()
    fig.xaxis.axis_label_text_color = "white"
    fig.yaxis.axis_label = "predicted Values for " + target.get_name()
    fig.yaxis.axis_label_text_color = "white"
    fig.circle(y_train,
               y_repeat,
               size=4,
               line_color="white",
               fill_color="white",
               name="train",
               legend_label="train")
    fig.circle(y_test,
               y_hat,
               line_color="#d9b100",
               fill_color="#d9b100",
               size=4,
               name="test",
               legend_label="test")

    fileName = functionNode.get_child("outputFileName").get_value()
    filePath = os.path.join(myDir, './../web/customui/' + fileName)
    fig.legend.location = "top_left"
    output_file(filePath, mode="inline")
    save(fig)

    return True
x = [0, 1, 2, 3, 4, 5]
y = [0, 1, 4, 9, 16, 25]

# set output to static HTML file
output_file("quad.html")

# create a new plot with a title and axis labels
p = figure(title="x^2 example",
           x_axis_label='x',
           y_axis_label='y',
           active_scroll="wheel_zoom")

# add a line renderer with legend and line thickness
p.line(x, y, legend_label="x-val legend label", line_width=2)

# customize the hover_tool/tooltip

hover_tool = HoverTool(tooltips=[("x val", "$x"), ("y val", "$y")])
hover_tool.point_policy = "snap_to_data"
hover_tool.line_policy = "interp"  # interpolate if between data points, makes it more continuous
hover_tool.mode = "vline"  #shows value if in line vertically (no need to be directly on top of graph line)

p.add_tools(hover_tool)

# remove tools on the right and logo for clean look
p.toolbar.logo = None
p.toolbar_location = None

# show graph
show(p)
Example #17
0
x_range = []
# for level in range (1,7):
#     search_str_list.append ("LTE.*RSSI Level " + str(level))
#
# print (search_str_list)

hover = HoverTool(tooltips=[
    ("index", "$index"),
    ("Power", "@y"),
    ("desc", "@desc"),
],
                  names=["foo"],
                  mode='vline')

hover.point_policy = "snap_to_data"
hover.line_policy = "nearest"
p = figure(
    plot_width=800,
    plot_height=800,
    title="LTE RSSI",
    tools=[hover,
           BoxZoomTool(),
           ResetTool(),
           PanTool(),
           BoxSelectTool()])

for row in reader:

    if row['Product'] == "Upper Limits----->":
        for field in search_fieldlist:
            upper_limit.append(float(row[field]))
Example #18
0
source_im = ColumnDataSource(data=dict(x=x, y=y7))
source_na = ColumnDataSource(data=dict(x=x, y=y8))
source_ra = ColumnDataSource(data=dict(x=x, y=y9))
source_da = ColumnDataSource(data=dict(x=x, y=y10))
source_ic = ColumnDataSource(data=dict(x=x, y=y11))
source_pr = ColumnDataSource(data=dict(x=x, y=y12))

# Incidence vs Rt
source_phase_space = ColumnDataSource(data=dict(x=y5, y=y11))

# plot 1

hover = HoverTool(tooltips=[(PLOT_X_LABEL, "@x{0}"), (PLOT_Y_LABEL, "@y{0}")],
                  mode="vline")
hover.point_policy = 'snap_to_data'
hover.line_policy = 'nearest'

plot = figure(
    plot_height=PLOT_HEIGHT,
    plot_width=PLOT_WIDTH,
    title=PLOT_TITLE,
    tools=PLOT_TOOLS,
    x_range=[0, DAYS],
)

plot.line('x',
          'y',
          source=source_active,
          line_width=PLOT_LINE_WIDTH,
          line_alpha=PLOT_LINE_ALPHA,
          line_color=PLOT_LINE_ACTIVE_COLOR,
Example #19
0
def varstatistics(functionNode):
    logger = functionNode.get_logger()
    logger.info("==>>>> statistics " + functionNode.get_browse_path())
    progressNode = functionNode.get_child("control").get_child("progress")
    progressNode.set_value(0)
    #functionNode.get_child("control.signal").set_value(None)

    vars = functionNode.get_child("variable").get_targets()
    widget = functionNode.get_child("widget").get_target()
    bins = functionNode.get_child("bins").get_value()
    tags = functionNode.get_child("annotations").get_value()
    startTime = date2secs(widget.get_child("startTime").get_value())
    endTime = date2secs(widget.get_child("endTime").get_value())

    vars = {var.get_id(): {"node": var} for var in vars}

    #first 30% progress:
    prog = Progress(progressNode)
    progressNode.set_value(0.1)
    prog.set_offset(0.1)
    #prog.set_divisor()

    if tags:
        allAnnoNodes = widget.get_child(
            "hasAnnotation.annotations").get_leaves()
        allAnnos = []
        prog.set_divisor(len(allAnnoNodes) / 0.2)
        for index, node in enumerate(allAnnoNodes):
            prog.set_progress(index)
            if node.get_child("type").get_value() == "time":
                thisTags = node.get_child("tags").get_value()
                if any(tag in tags for tag in thisTags):
                    anno = {}
                    for child in node.get_children():
                        anno[child.get_name()] = child.get_value()
                    if date2secs(anno["startTime"]) >= startTime and date2secs(
                            anno["endTime"]
                    ) <= endTime:  #take this anno only if it is inside the current start/end time
                        allAnnos.append(anno)
        if allAnnos == []:
            give_up(functionNode, "no matching annotations in selected time")
            return False
    else:
        allAnnos = []

    progressNode.set_value(0.3)

    logger.debug(f"statistics annotations to look at: {len(allAnnos)}")
    prog.set_offset(0.3)
    totalAnnos = max(len(allAnnos), 1)
    totalCount = len(vars) * totalAnnos

    prog.set_divisor(totalCount / 0.3)
    totalValids = 0
    for varIndex, var in enumerate(vars):
        info = vars[var]
        if tags:
            #iterate over all start and end times
            values = numpy.asarray([], dtype=numpy.float64)
            for annoIndex, anno in enumerate(allAnnos):
                thisValues = info["node"].get_time_series(
                    anno["startTime"], anno["endTime"])["values"]
                values = numpy.append(values, thisValues)
                myCount = varIndex * totalAnnos + annoIndex
                prog.set_progress(myCount)
        else:
            values = info["node"].get_time_series(startTime, endTime)["values"]

        valids = numpy.count_nonzero(~numpy.isfinite(values))
        totalValids += valids
        hist, edges = numpy.histogram(values, bins=bins)
        hist = hist / len(values)  #normalize
        info["hist"] = hist
        info["edges"] = edges

    #make a plot
    if totalValids == 0:
        give_up(
            functionNode,
            "all Variables are have no data in the time and annotations selected"
        )
        return False

    progressNode.set_value(0.6)

    hover1 = HoverTool(tooltips=[('x,y', '$x,$y')], mode='mouse')
    hover1.point_policy = 'snap_to_data'
    hover1.line_policy = "nearest"

    tools = [
        PanTool(),
        WheelZoomTool(),
        BoxZoomTool(),
        ResetTool(),
        SaveTool(), hover1
    ]

    title = "Statistics of " + str(
        [info["node"].get_name() for var, info in vars.items()])
    if tags:
        title = title + " in annotation: " + str(tags)

    fig = figure(title=title, tools=tools, plot_height=300)
    fig.toolbar.logo = None

    curdoc().theme = Theme(json=themes.darkTheme)
    fig.xaxis.major_label_text_color = themes.darkTickColor
    fig.yaxis.major_label_text_color = themes.darkTickColor

    for index, var in enumerate(vars):
        info = vars[var]
        col = themes.darkLineColors[index]
        hist = info["hist"]
        edges = info["edges"]

        fig.quad(top=hist,
                 bottom=0,
                 left=edges[:-1],
                 right=edges[1:],
                 fill_color=col,
                 line_color=col,
                 alpha=0.8,
                 legend_label=info["node"].get_name())

    fig.legend.location = "top_left"
    fileName = functionNode.get_child("fileName").get_value()
    filePath = os.path.join(myDir, './../web/customui/' + fileName)

    # now make the trend box plot, but only for tags
    # for each variable we create statistics for the annotations and prepare the data
    # {"node":Node(), "boxLower":[], "boxUpper", "mean", "limitUpper", "limitLower"}
    #

    startTime = date2secs(widget.get_child("startTime").get_value(
    ))  #we only take tags that are inside the current zoom of the widgets
    endTime = date2secs(widget.get_child("endTime").get_value())

    boxPlots = []
    allTimes = []
    if tags:
        for index, var in enumerate(vars):
            info = {
                "node": vars[var]["node"],
                "boxLower": [],
                "boxUpper": [],
                "median": [],
                "time": [],
                "limitUpper": [],
                "limitLower": [],
                "mean": []
            }
            for anno in allAnnos:
                data = info["node"].get_time_series(anno["startTime"],
                                                    anno["endTime"])
                if len(data["values"]):
                    data["values"] = data["values"][numpy.isfinite(
                        data["values"])]
                    #remove the nan
                if len(data["values"]):

                    #make the statistics
                    info["time"].append(numpy.median(data["__time"]) * 1000)
                    allTimes.append(numpy.median(data["__time"]) * 1000)
                    info["limitLower"].append(
                        numpy.quantile(data["values"], 0.01))
                    info["limitUpper"].append(
                        numpy.quantile(data["values"], 0.99))
                    info["boxLower"].append(
                        numpy.quantile(data["values"], 0.25))
                    info["boxUpper"].append(
                        numpy.quantile(data["values"], 0.75))
                    info["median"].append(numpy.median(data["values"]))
                    info["mean"].append(numpy.mean(data["values"]))
            boxPlots.append(info)

        format = "%Y-%m-%d-T%H:%M:%S"
        custom = """var local = moment(value).tz('UTC'); return local.format();"""  #%self.server.get_settings()["timeZone"]

        hover = HoverTool(tooltips=[('date', '@x{%F}')],
                          formatters={'@x': CustomJSHover(code=custom)},
                          mode='mouse')
        hover.point_policy = 'snap_to_data'
        hover.line_policy = "nearest"
        tools = [
            PanTool(),
            BoxZoomTool(),
            WheelZoomTool(),
            ResetTool(), hover,
            SaveTool()
        ]

        fig2 = figure(title="trends",
                      tools=tools,
                      plot_height=300,
                      x_axis_type='datetime')
        fig2.xaxis.major_label_text_color = themes.darkTickColor
        fig2.yaxis.major_label_text_color = themes.darkTickColor

        progressNode.set_value(0.7)

        fig2.xaxis.formatter = DatetimeTickFormatter(years=format,
                                                     days=format,
                                                     months=format,
                                                     hours=format,
                                                     hourmin=format,
                                                     minutes=format,
                                                     minsec=format,
                                                     seconds=format)
        fig2.toolbar.logo = None
        #fig2.line([1,2,3],[1,2,3])
        #calc with of vbars
        if len(allAnnos) > 1:
            xTimesStart = min(allTimes)
            xTimesEnd = max(allTimes)
            width = (xTimesEnd - xTimesStart) / 2 / len(allAnnos)
        else:
            width = 1000000

        for index, info in enumerate(boxPlots):
            #each info is for one variable
            col = themes.darkLineColors[index]
            fig2.segment(info["time"],
                         info["limitUpper"],
                         info["time"],
                         info["boxUpper"],
                         line_color=col)
            fig2.segment(info["time"],
                         info["limitLower"],
                         info["time"],
                         info["boxLower"],
                         line_color=col)

            width = 20
            #fig2.vbar(info["time"],width=width,bottom=info["median"],top=info["boxUpper"],fill_color=col,line_color="black",width_units='screen')
            #fig2.vbar(info["time"],width=width,bottom=info["boxLower"],top=info["median"],fill_color=col,line_color="black",width_units='screen')
            #upper box
            sizUpper = numpy.asarray(info["boxUpper"]) - numpy.asarray(
                info["median"])
            medUpper = numpy.asarray(info["median"]) + sizUpper / 2
            fig2.rect(x=info["time"],
                      y=medUpper,
                      width_units='screen',
                      width=20,
                      height=sizUpper,
                      fill_color=col,
                      line_color="black")

            #lower box
            sizLower = numpy.asarray(info["median"]) - numpy.asarray(
                info["boxLower"])
            medLower = numpy.asarray(info["median"]) - sizLower / 2
            fig2.rect(x=info["time"],
                      y=medLower,
                      width_units='screen',
                      width=20,
                      height=sizLower,
                      fill_color=col,
                      line_color="black")

            #sort data for line
            x = numpy.asarray(info["time"])
            y = numpy.asarray(info["mean"])
            order = numpy.argsort(x)
            x = x[order]
            y = y[order]
            fig2.line(x, y, line_color=col)

        progressNode.set_value(0.8)
    else:
        #no fig2
        pass

    output_file(
        filePath, mode="inline"
    )  #inline: put the bokeh .js into this html, otherwise the default cdn will be taken, might cause CORS problems
    if tags:
        save(layout([[fig], [fig2]]))
    else:
        save(fig)

    return True