Пример #1
0
def property_info():

    df_group_tax = pd.read_csv('data/tax_df.csv')

    if request.method == 'GET':
        tax_asr_name = request.args.get("tax_asr_name")

    if tax_asr_name is None:
        tax_asr_name = tax_assessor_list[0]

    # select = Select(title='property assessment attribute', value='assessed_value_total', options=tax_assessor_list, width =500)
    div = Div(text="<iframe src="
              r'html/itax_map.html'
              " style='min-width:calc(100vw - 26px); height: 500px'><iframe>",
              width=500)

    plot_variable = tax_asr_name
    tax_attr_m = create_hex_map(df_group_tax, plot_variable)
    div.text = tax_attr_m._repr_html_()

    tax_asr_script, tax_asr_div = components(div)

    return render_template('tax_asr.html',
                           script=tax_asr_script,
                           div=tax_asr_div,
                           tax_asr_name=tax_asr_name,
                           tax_asr_attributes=tax_assessor_list)
Пример #2
0
    def _get_title_div(self, key, default_fontsize='15pt', width=450):
        title_div = None
        title = self._format_title(key) if self.show_title else ''
        if not title:
            return title_div

        title_json = theme_attr_json(self.renderer.theme, 'Title')
        color = title_json.get('text_color', None)
        font = title_json.get('text_font', 'Arial')
        fontstyle = title_json.get('text_font_style', 'bold')
        fontsize = self._fontsize('title').get('fontsize', default_fontsize)
        if fontsize == default_fontsize:  # if default
            fontsize = title_json.get('text_font_size', default_fontsize)
            if 'em' in fontsize:
                # it's smaller than it shosuld be so add 0.25
                fontsize = str(float(fontsize[:-2]) + 0.25) + 'em'

        title_tags = self._title_template.format(color=color,
                                                 font=font,
                                                 fontstyle=fontstyle,
                                                 fontsize=fontsize,
                                                 title=title)

        if 'title' in self.handles:
            title_div = self.handles['title']
        else:
            title_div = Div(width=width)  # so it won't wrap long titles easily
        title_div.text = title_tags

        return title_div
Пример #3
0
    def _get_title_div(self, key, default_fontsize='15pt', width=450):
        title_div = None
        title = self._format_title(key) if self.show_title else ''
        if not title:
            return title_div

        title_json = theme_attr_json(self.renderer.theme, 'Title')
        color = title_json.get('text_color', None)
        font = title_json.get('text_font', 'Arial')
        fontstyle = title_json.get('text_font_style', 'bold')
        fontsize = self._fontsize('title').get('fontsize', default_fontsize)
        if fontsize == default_fontsize:  # if default
            fontsize = title_json.get('text_font_size', default_fontsize)
            if 'em' in fontsize:
                # it's smaller than it shosuld be so add 0.25
                fontsize = str(float(fontsize[:-2]) + 0.25) + 'em'

        title_tags = self._title_template.format(
            color=color,
            font=font,
            fontstyle=fontstyle,
            fontsize=fontsize,
            title=title)

        if 'title' in self.handles:
            title_div = self.handles['title']
        else:
            title_div = Div(width=width, style={"white-space": "nowrap"})  # so it won't wrap long titles easily
        title_div.text = title_tags

        return title_div
Пример #4
0
def demographics():

    df_demo_join_cherre = pd.read_csv('data/demo_data.csv')
    df_demo_join_cherre['zip'] = df_demo_join_cherre['zip'].astype(str)
    if request.method == 'GET':
        demo_name = request.args.get("demo_name")
    if demo_name is None:
        demo_name = demo_menu_list[0]

    # select = Select(title='Demographic attribute', value='median_employee_salary', options=menu_list, width =500)
    div = Div(text="<iframe src="
              r'html/imap.html'
              " style='min-width:calc(100vw - 26px); height: 500px'><iframe>",
              width=500)

    attribute = demo_name

    demog_m = create_map(df_demo_join_cherre, 'zip', attribute, 'per Zipcode')

    div.text = demog_m._repr_html_()

    demo_script, demo_div = components(div)

    return render_template('demographics.html',
                           script=demo_script,
                           div=demo_div,
                           demo_name=demo_name,
                           demo_attributes=demo_menu_list)
Пример #5
0
def main(images_folder: str, data_json_file: str, img_ref: Optional[str],
         out: str):
    """Project data point into interactive scatter plot

    Arguments:
        images_folder {str} -- Path of the images directory
        data_json_file {str} -- Path of the corresponding json file
        img_ref {Optional[str]} -- Path of the image version of the projection
                                   (see create_image_projection.py script)
        out {str} -- Output html file
    """

    print(
        colored(figlet_format("Scatter plot projection", font="standard"),
                "cyan"))
    assert out[-5:] == ".html", "Please give a html output file"
    images_folder = str(Path(images_folder).resolve())

    with open(data_json_file, "r") as json_file:
        points_projection = json.load(json_file)

    points = np.zeros((len(points_projection), 2))
    for i, point_properties in enumerate(points_projection):
        points[i] = [
            float(point_properties["x"]),
            float(point_properties["y"])
        ]

    domain_min, domain_max = np.min(points[:, 1]), np.max(points[:, 1])
    domain = domain_max - domain_min

    s1 = ColumnDataSource(
        data=dict(x=points[:, 0], y=abs(points[:, 1] - domain)))

    p1 = figure(tools=TOOLS, plot_width=1280)
    p1.circle("x", "y", source=s1, size=10)

    div = Div(width=400, height=p1.plot_height)
    layout = row(p1, div)

    ###
    callback = display_event(
        div,
        root_image=images_folder,
        source=points_projection,
        block_height=p1.plot_height,
    )

    s1.selected.js_on_change("indices", callback)

    ###
    if img_ref:
        div_img_ref = Div(width=1280)
        div_img_ref.text = f"<img style='width:1280px' src='{img_ref}' / >"
        layout = column(layout, div_img_ref)

    output_file(out, title="scatter projection")

    show(layout)
Пример #6
0
 def _get_title(self, key):
     title_div = None
     title = self._format_title(key) if self.show_title else ''
     if title:
         fontsize = self._fontsize('title')
         title_tags = self._title_template.format(title=title, **fontsize)
         if 'title' in self.handles:
             title_div = self.handles['title']
         else:
             title_div = Div()
         title_div.text = title_tags
     return title_div
Пример #7
0
 def _get_title(self, key):
     title_div = None
     title = self._format_title(key) if self.show_title else ''
     if title:
         fontsize = self._fontsize('title')
         title_tags = self._title_template.format(title=title,
                                                  **fontsize)
         if 'title' in self.handles:
             title_div = self.handles['title']
         else:
             title_div = Div()
         title_div.text = title_tags
     return title_div
Пример #8
0
def places_of_interest():

    df = pd.read_csv('data/poi_data_NY.csv')

    if request.method == 'GET':
        place_type_name = request.args.get("poi_type_name", 'RETAIL')
        if request.args.get("poi_rad") == None:
            place_radius = 2.0
        else:
            place_radius = float(request.args.get("poi_rad").strip())

        if request.args.get("center_lat") == None:
            center_latitude = 40.753912
        else:
            center_latitude = float(
                request.args.get("center_lat", 40.753912).strip())
        if request.args.get("center_long") == None:
            center_longitude = -73.981205
        else:
            center_longitude = float(
                request.args.get("center_long", -73.981205).strip())

    # select = Select(title="Type of rental space", value="RETAIL", options=poi_options_list)
    # radius = TextInput(title="Radius of places to check in km", value="5.0")
    # center_latitude = TextInput(title="Enter the latitude as float", value="40.753912")
    # center_longitude = TextInput(title="Enter the longitude as float", value="-73.981205")
    div = Div(text="<iframe src="
              r'html/i_poi_map.html'
              " style='min-width:calc(100vw - 26px); height: 500px'><iframe>",
              width=500)

    # place_type = select.value
    # place_radius=float(radius.value)
    # c_lat=float(center_latitude.value)
    # c_long=float(center_longitude.value)

    poi_map = select_df_and_map(df, place_type_name, place_radius,
                                center_latitude, center_longitude)

    div.text = poi_map._repr_html_()

    poi_script, poi_div = components(div)

    return render_template('poi.html',
                           script=poi_script,
                           div=poi_div,
                           poi_type_name=place_type_name,
                           poi_rad=place_radius,
                           center_lat=center_latitude,
                           center_long=center_longitude,
                           place_type_list=poi_list)
Пример #9
0
def generate_color_description_component() -> column:
    """
    色の説明書きのコンポーネントを作成する
    :return:
    """
    div = Div(width=200, height=8, style={'color': '#545454'})
    left_div = Div(width=120, height=55, style={'color': '#545454'})
    right_div = Div(width=120, height=55, style={'color': '#545454'})

    div.text = 'Color means the status of PEPs.'
    left_div.text = """<img src="image/draft_icon.png"> Draft<br>
<img src="image/provisional_icon.png"> Provisional<br>
<img src="image/accepted_icon.png"> Accepted<br>
<img src="image/final_icon.png"> Final<br>
<img src="image/active_icon.png"> Active<br><br>
"""
    right_div.text = """<img src="image/deffered_icon.png"> Deferred<br>
<img src="image/withdrawned_icon.png"> Withdrawn<br>
<img src="image/rejected_icon.png"> Rejected<br>
<img src="image/superseded_icon.png"> Superseded<br><br>
"""

    component = column(div, row(left_div, right_div))
    return component
Пример #10
0
# Unicode options for Anaconda python
conn.setencoding("utf-8")
conn.setdecoding(pyodbc.SQL_CHAR, encoding='utf-32le')
conn.setdecoding(pyodbc.SQL_WMETADATA, encoding='utf-32le')
# Unicode options for python (not anaconda)
#conn.setdecoding(pyodbc.SQL_CHAR, encoding='utf-32le', to=str)
#conn.setdecoding(pyodbc.SQL_WMETADATA, encoding='utf-32le', to=str)

cursor = conn.cursor()

sql = "SELECT _id, name, address, email, phone_number, latitude, longitude, first_visit, churn_risk, sentiment " \
      "FROM `dfs.default`.`./apps/crm` limit 10000"
logger.debug("executing SQL: " + sql)
customer_directory_df = pd.read_sql(sql, conn)
logger.debug("records returned: " + str(len(customer_directory_df.index)))
query_performance.text = "<div class=\"small\">" + str(
    len(customer_directory_df.index)) + " rows selected</div>"

text_input = TextInput(title="Search String:", value='')
filter_options = ['name', 'phone_number', 'email']
filterby = Select(title="Search Field:",
                  width=100,
                  value="name",
                  options=filter_options)

sort_options = ['name', 'phone_number', 'email', 'first_visit']
sortby = Select(title="Order By:",
                width=100,
                value="name",
                options=sort_options)
controls = [text_input, filterby, sortby]
for control in controls:
Пример #11
0
def stream_news():
    plot_symbol = '^GSPC'
    database = CassandraStorage(plot_symbol)
    database.session.row_factory = pandas_factory
    database.session.default_fetch_size = None

    def make_dataset(date='2019-08-01'):
        query = "SELECT * FROM NEWS WHERE DATE>='{}' ALLOW FILTERING;".format(
            date)
        rslt = database.session.execute(query, timeout=None)
        df = rslt._current_rows

        df.publishedat = pd.DatetimeIndex(
            pd.to_datetime(df.publishedat, unit='ms')).tz_localize(
                'GMT').tz_convert('US/Pacific').to_pydatetime()
        df = df.sort_values('publishedat').tail(4)
        df.description = df.description.str.replace('@@', "'")
        df.title = df.title.str.replace('@@', "'")
        return df

    def make_text(source):
        text="""<b><p style="color:blue;">News: </p></b> 
                          <b>{}</b><br>
                          {}<br>
                          <i>Source: {} &nbsp;&nbsp; Published At: {} </i><br>
                          <br>
                          <br>
                          
                          <b>{}</b><br>
                          {}<br>
                          <i>Source: {} &nbsp;&nbsp; Published At: {} </i><br>
                          <br>
                          <br>
    
    
                          <b>{}</b><br>
                          {}<br>
                          <i>Source: {} &nbsp;&nbsp; Published At: {} </i><br>
                          <br>
                          <br>
    
                          <b>{}</b><br>
                          {}<br>
                          <i>Source: {} &nbsp;&nbsp; Published At: {} </i><br>
                          <br>
                          <br>
                          """.format(source.data['title'][-1], \
                                     source.data['description'][-1], \
                                     source.data['source'][-1], \
                                     str(pd.to_datetime(source.data['publishedat'][-1])), \
                                     source.data['title'][-2], \
                                     source.data['description'][-2], \
                                     source.data['source'][-2], \
                                     str(pd.to_datetime(source.data['publishedat'][-2])),
                                     source.data['title'][-3], \
                                     source.data['description'][-3], \
                                     source.data['source'][3], \
                                     str(pd.to_datetime(source.data['publishedat'][-3])),
                                     source.data['title'][-4], \
                                     source.data['description'][-4], \
                                     source.data['source'][-4], \
                                     str(pd.to_datetime(source.data['publishedat'][-4])))
        return text

    df = make_dataset(date='2019-08-01')
    source = ColumnDataSource(data=df.to_dict('list'))
    div = Div(text="", width=600)
    div.text = make_text(source)

    def update():
        df = make_dataset(date='2019-08-01')
        source.stream(df.to_dict('list'))
        #print(str(source.data['title']))
        #print()

    return div, update
Пример #12
0
def make_figures(scan):
    ub_matrix = ft.UBMatrix(scan['latparam'], scan['hkl1'], scan['hkl2'],
                            scan['plot_x'], scan['plot_y'])
    A3_starts, A3_ends, A4_starts, A4_ends = (scan['A3_starts'],
                                              scan['A3_ends'],
                                              scan['A4_starts'],
                                              scan['A4_ends'])
    NPs = [int(x) for x in scan['NPs']]
    kis = [ft.e_to_k(e) for e in scan['eis']]
    kfs = [ft.e_to_k(e) for e in ft.EF_LIST]
    hm = scan['hm']
    hm_hkl = scan['hm_hkl']
    hm_ssr = scan['hm_ssr']
    unique_kis = sorted(list(set(kis)))
    indexes_of_ki = [[ind for ind in range(len(kis)) if kis[ind] == ki]
                     for ki in unique_kis]
    locus_palette = ['#FFCE98', '#F6FF8D', '#94FFD5', '#909CFF', '#FF8AD8']

    locuses_dict = {}
    scatters_dict = {}
    colors_dict = {}
    senses_dict = {}
    for nth_ki, ki in enumerate(unique_kis):
        clippers = [Pyclipper() for _ in range(ft.EF_CHANNELS)]
        scatter_arrays = [[] for _ in range(ft.EF_CHANNELS)]
        color_arrays = [[] for _ in range(ft.EF_CHANNELS)]
        for scan_no in indexes_of_ki[nth_ki]:
            angles = (A3_starts[scan_no], A3_ends[scan_no], A4_starts[scan_no],
                      A4_ends[scan_no], ub_matrix)
            locuses = [
                ft.calculate_locus(ki, kf, *angles, no_points=NPs[scan_no])
                for kf in kfs
            ]
            scatter_coords = [
                ft.scatter_coords(ki, kf, *angles, no_points=NPs[scan_no])
                for kf in kfs
            ]
            scatter_colors = [
                ft.scatter_color(ki,
                                 kf,
                                 *angles,
                                 name=hm,
                                 ssr=hm_ssr,
                                 north=hm_hkl,
                                 no_points=NPs[scan_no]) for kf in kfs
            ]
            if A4_starts[scan_no] > 0:
                senses_dict[ki] = 1
            else:
                senses_dict[ki] = -1

            for i in range(ft.EF_CHANNELS):
                clippers[i].AddPath(scale_to_clipper(locuses[i]), PT_SUBJECT)
                scatter_arrays[i] += scatter_coords[i]
                color_arrays[i] += scatter_colors[i]
        locuses_ki = [
            scale_from_clipper(clippers[i].Execute(CT_UNION, PFT_NONZERO))
            for i in range(ft.EF_CHANNELS)
        ]
        locuses_ki_x, locuses_ki_y = split_locus_lists(locuses_ki)
        scatters_ki_x, scatters_ki_y = split_scatter_lists(scatter_arrays)
        common_locus_x, common_locus_y = find_common_coverage(locuses_ki)
        locuses_dict[ki] = [
            locuses_ki_x, locuses_ki_y, common_locus_x, common_locus_y
        ]
        scatters_dict[ki] = [scatters_ki_x, scatters_ki_y, color_arrays]

    p_col = []
    plots = []
    x_axis = np.array(scan['plot_x'])
    y_axis = np.array(scan['plot_y'])
    for ki in unique_kis:
        TOOLS = "pan,wheel_zoom,reset,save"
        main_plot = figure(plot_width=700,
                           plot_height=600,
                           title='Ei = %s meV' % fmt2(ft.k_to_e(ki)),
                           tools=TOOLS)
        main_plot.xaxis.axis_label = 'x * %s' % bracketed_vector(x_axis)
        main_plot.yaxis.axis_label = 'y * %s' % bracketed_vector(y_axis)
        ticker = SingleIntervalTicker(interval=0.5, num_minor_ticks=1)
        main_plot.axis.ticker = ticker
        main_plot.grid.ticker = ticker
        locus = locuses_dict[ki]
        efs_str = [fmt1(ft.k_to_e(ki) - ft.k_to_e(kf)) for kf in kfs]
        sources = []
        source_handle = ColumnDataSource(dict(x=[], y=[], colors=[]))
        scatter_off = ColumnDataSource(dict(x=[], y=[], colors=[]))
        for i in reversed(range(ft.EF_CHANNELS)):
            color = locus_palette[i]
            x_list = locus[0][i]
            y_list = locus[1][i]
            main_plot.patches(x_list,
                              y_list,
                              alpha=0.35,
                              fill_color=color,
                              muted_fill_color='black',
                              muted_fill_alpha=0.01,
                              muted_line_alpha=0.1,
                              line_width=1,
                              legend='dE=' + efs_str[i])
            set_aspect(main_plot,
                       x_list[0],
                       y_list[0],
                       aspect=ub_matrix.figure_aspect)
        for i in range(ft.EF_CHANNELS):
            sources.append(
                ColumnDataSource(
                    dict(x=scatters_dict[ki][0][i],
                         y=scatters_dict[ki][1][i],
                         colors=scatters_dict[ki][2][i])))
        main_plot.circle(x='x',
                         y='y',
                         size=4.5,
                         fill_alpha=1,
                         visible=True,
                         fill_color='colors',
                         line_alpha=0.2,
                         source=source_handle)
        main_plot.patches(locus[2][0],
                          locus[3][0],
                          fill_alpha=0.0,
                          line_width=1.2,
                          legend='Common',
                          line_color='red',
                          muted_line_alpha=0.0,
                          muted_fill_alpha=0.0)
        glyph_dots = plot_lattice_points(main_plot, x_axis, y_axis)
        main_plot.legend.click_policy = 'mute'
        plot_brillouin_zones(main_plot, x_axis, y_axis)
        cs = sources
        callback = CustomJS(args=dict(s0=cs[0],
                                      s1=cs[1],
                                      s2=cs[2],
                                      s3=cs[3],
                                      s4=cs[4],
                                      s5=scatter_off,
                                      source=source_handle),
                            code="""
                var f = cb_obj.active;                
                switch (f) {
                    case 0:
                        source.data = s0.data;
                        break;
                    case 1:
                        source.data = s1.data;
                        break;
                    case 2:
                        source.data = s2.data;
                        break;
                    case 3:
                        source.data = s3.data;
                        break;
                    case 4:
                        source.data = s4.data;
                        break;
                    case 5:
                        source.data = s5.data;
                        break;
                }
                source.change.emit();       
            """)
        en_buttons = RadioButtonGroup(
            labels=['2.5', '3.0', '3.5', '4.0', '4.5', 'Off'],
            active=5,
            callback=callback)
        en_button_caption = Div()
        en_button_caption.text = """<span style="font-weight: bold;">Active channel:</span>"""
        hover = HoverTool(renderers=[glyph_dots], tooltips=[('Q', '@coord')])
        main_plot.add_tools(hover)
        message_div = Div(width=600, height=200)
        if hm != 'no':
            plot_radar = draw_radar(main_plot, message_div, en_buttons, hm, ki,
                                    scan['hkl1'], hm_hkl, hm_ssr, ub_matrix,
                                    senses_dict[ki])
            plot_radar.axis.visible = False
            ctrl_col = column(
                [en_button_caption, en_buttons, plot_radar, message_div])
        else:
            plot_radar = draw_radar(main_plot, message_div, en_buttons, hm, ki,
                                    scan['hkl1'], scan['hkl1'], 0, ub_matrix,
                                    senses_dict[ki])
            plot_radar.axis.visible = False
            ctrl_col = column(
                [en_button_caption, en_buttons, plot_radar, message_div])
            # ctrl_col = column([en_button_caption, en_buttons, message_div])

        plots.append(main_plot)
        p_col.append([main_plot, ctrl_col])
    for each in plots:
        each.x_range = plots[0].x_range
        each.y_range = plots[0].y_range

    grid = layout(p_col)
    script, div = components(grid)

    return script, div
Пример #13
0
p0 = [5.0, 22., 0.08, 0.15, 6.15, 7., 0.08, 0.15, 6.9, 12., 0.08, 0.15]

lower_bounds = [
    4.85, 1, 0.001, 0.0001, 6.0, 1., 0.001, 0.0001, 6.75, 1., 0.001, 0.0001
]
upper_bounds = [5.15, 30, 0.3, 0.6, 6.3, 20, 0.3, 0.6, 7.0, 20, 0.3, 0.6]

bounds = (lower_bounds, upper_bounds)

y = exp_gauss_3(x, *p0)

lnlike_val = lnlike(p0, x, y_data)
lnlikebox = Div(text="", width=800)
lnliketext = open(os.path.join(os.path.dirname(__file__),
                               "lnlike.html")).read()
lnlikebox.text = lnliketext + "<h1> lnlikelihood:</p>%.3f </h1>" % lnlike_val

# Set up data
source = ColumnDataSource(data=dict(x=x, y=y, res=(y - y_data)))

# Set up plot
plot = figure(plot_height=500,
              plot_width=700,
              title="FRB181017",
              tools="crosshair,pan,reset,save,wheel_zoom",
              x_range=[0, np.max(x)],
              y_range=[-3, 1.2 * np.max(y_data)])

plot_residual = figure(plot_height=200,
                       plot_width=700,
                       title="Residuals",
Пример #14
0

height_dropdown.on_change('value', redraw_pixels)
width_dropdown.on_change('value', redraw_pixels)
colour_depth_selector.on_change('value', redraw_pixels)

## Content sections

intro_div = Div(width=600,
                height=300,
                style={
                    'font-size': '1.2em',
                    'font-family': 'Helvetica'
                })

intro_div.text = f"""
<h3>The Curse of Dimensionality and Density in the Structure of Nature</h3>

<p>This interactive application was inspired by Richard Hamming's<a href="https://en.wikipedia.org/wiki/Richard_Hamming"><em>"Art of Doing Science and Engineering"</em>, and the compressibility and compressed sensing video lecture series by <a href="https://www.youtube.com/watch?v=Dt2WYkqZfbs&list=PLMrJAkhIeNNQV7wi9r7Kut8liLFMWQOXn&index=89">Steve Brunton</a></a> </p>
<p>The <a href="https://blogs.scientificamerican.com/observations/ada-lovelace-day-honors-the-first-computer-programmer/">iconic image</a> below at left is {original_dims[0]} pixels wide by {original_dims[1]} pixels high in the original file.  Since the image is in greyscale, the colour of each pixel is represented by an integer value in the range [0, 255].</p> 

<p>Thinking more generally about images as systems, there are ({original_dims[0]}x{original_dims[1]})<sup>256</sup> possible configurations (states), making this image unimaginably unique.</p>  

<p>In other words, if each pixel can take one of 256 values, and there are {original_dims[0]} x {original_dims[1]} = {n_pixels:,} pixels, the number of possible <em>unique</em> images is {n_pixels:,}<sup>256</sup>, or roughly <strong>10<sup>262</sup></strong>.  It's not really possible to imagine such large numbers, but as a basis of comparison, the <a href="https://physics.stackexchange.com/questions/47941/dumbed-down-explanation-how-scientists-know-the-number-of-atoms-in-the-universe">cosmological estimate of the number of <strong>atoms in the universe is about 10<sup>80</sup></strong></a>.  The set of all {original_dims[0]} x {original_dims[1]} images that humans can recognize is <strong>effectively zero</strong> against such an unimaginable proportion of possibilities.  If every human that ever lived spent their entire lifetime looking at {original_dims[0]} x {original_dims[1]} images with randomly assigned pixel values, virtually all images drawn would be like <a href="https://www.youtube.com/watch?v=Un8yRBC84Yk">static on an old CRT TV</a>.</p>

<p>So what is it about natural systems that makes pattern recognition not just possible, but common?</p>

"""

system_description_div = Div(width=350,
                             height=400,
Пример #15
0
def make_timeline_html(input_dir_path: str, output_path: str) -> None:
    # Load Data
    path = Path(input_dir_path) / 'pep_graph.gpickle'
    pep_graph = nx.read_gpickle(path)

    path = Path(input_dir_path) / 'python_release_info.csv'
    release_df = pd.read_csv(path,
                             encoding='utf-8',
                             parse_dates=['release_date'])

    release_df = release_df[release_df.micro == 0]
    release_df['color'] = release_df.major.apply(lambda x: PYTHON_YELLOW_COLOR_CODE if x == 2 else PYTHON_BLUE_COLOR_CODE)  # 2, 3以外が出てきたら再考すること

    node_dict = dict(pep_graph.nodes(data=True))
    date_list = [value['Created_dt'] for key, value in node_dict.items()]

    min_date = datetime.datetime(min(date_list).year, 1, 1)
    py2_release_label_data_source = tl_compo.generate_release_label_source(release_df,
                                                                           major_version=2,
                                                                           pos_x=min_date)
    py3_release_label_data_source = tl_compo.generate_release_label_source(release_df,
                                                                           major_version=3,
                                                                           pos_x=min_date)

    py2_release_line_data_source = tl_compo.generate_release_line_data_source(release_df,
                                                                              major_version=2)
    py3_release_line_data_source = tl_compo.generate_release_line_data_source(release_df,
                                                                              major_version=3)

    release_source_dict = {'py2_release_label_source': py2_release_label_data_source,
                           'py3_release_label_source': py3_release_label_data_source,
                           'py2_release_line_source': py2_release_line_data_source,
                           'py3_release_line_source': py3_release_line_data_source
                           }

    all_pep_data_source = compo.generate_node_data_source(pep_graph)

    # DataTable用のデータソースを用意する
    linked_from_table_source = table_compo.generate_data_table_data_source(pep_graph)
    link_to_table_source = table_compo.generate_data_table_data_source(pep_graph)

    linked_from_data_table = table_compo.generate_data_table(linked_from_table_source)
    link_to_data_table = table_compo.generate_data_table(link_to_table_source)

    linked_from_table_title_div = Div(text='<strong>PEP N is linked from ...</strong>',
                                      style={'color': BASE_FONT_COLOR})
    link_to_table_title_div = Div(text='<strong>PEP N links to ...</strong>',
                                  style={'color': BASE_FONT_COLOR})

    # Timeline用のデータソースを用意する
    timeline_display_circle_source = tl_compo.generate_timeline_data_source(pep_graph)
    timeline_label_source = tl_compo.generate_timeline_label_data_source(pep_graph)
    desc_start_date, _ = tl_compo.get_timeline_plot_range(pep_graph)
    desc_start_date = desc_start_date + datetime.timedelta(days=30)
    timeline_desc_label_source = tl_compo.generate_timeline_desc_data_source(xs=[desc_start_date, desc_start_date],
                                                                             ys=[1.7, 0.1],
                                                                             font_size=15)

    # 入力ボックス用のデータソース生成
    error_message_div = Div(width=300, height=8, style={'color': 'red'}, text='')

    title_div = Div(width=700,
                    style={'font-size': 'large',
                           'line-height': '1.5',
                           'color': BASE_FONT_COLOR})
    title_div.text = """
    Let's enter the PEP number in the left text box.<br>
    Then you can see the following information.
    <li>Which PEPs do link that PEP?</li>
    <li>Which PEPs are linked from that PEP?</li>
    """

    checkbox_group = CheckboxGroup(labels=["Show Python 2 release dates",
                                           "Show Python 3 release dates"],
                                   active=[0, 1])

    def callback_input_pep_number(all_pep_data_source=all_pep_data_source,
                                  link_to_table_source=link_to_table_source,
                                  linked_from_table_source=linked_from_table_source,
                                  link_to_table_title_div=link_to_table_title_div,
                                  linked_from_table_title_div=linked_from_table_title_div,
                                  timeline_display_circle_source=timeline_display_circle_source,
                                  timeline_label_source=timeline_label_source,
                                  timeline_desc_source=timeline_desc_label_source,
                                  title_div=title_div,
                                  error_message_div=error_message_div) -> None:
        """
        テキストボックスに文字入力されたときに実行される関数。
        PyScriptを使ってJavaScriptに変換される。
        参考: https://bokeh.pydata.org/en/latest/docs/user_guide/interaction/callbacks.html#customjs-with-a-python-function
        * このパラメータの設定の仕方以外にもあるかもしれない

        :param all_pep_data_source:
        :param link_to_table_source:
        :param linked_from_table_source:
        :param link_to_table_title_div:
        :param linked_from_table_title_div:
        :param timeline_display_circle_source:
        :param timeline_label_source:
        :param timeline_desc_source:
        :param title_div:
        :param error_message_div:
        :param debug_div:
        :return:
        """

        inputed_text = cb_obj['value']

        # PyScriptで変換するときに自分の外側の関数の呼び方がわからないので、
        # 暫定で関数内関数で定義する。
        def create_header_text(pep_dict: dict) -> str:
            if not pep_dict:
                return 'Not Found.'

            # .formatを使うとJavaScript変換後に実行時エラーになるので、
            # +演算子で連結している
            description_text = "<span>Created: " \
                               + pep_dict['Created_str'] \
                               + "&nbsp;&nbsp;&nbsp;&nbsp;Type: " \
                               + pep_dict['Type'] \
                               + "<br>"

            link_text = "<a href='https://www.python.org/dev/peps/pep-" \
                        + pep_dict['index'] \
                        + "/' target='_blank'>PEP " \
                        + pep_dict['PEP'] \
                        + "</a>"
            title_text = "<span style='font-size : xx-large;'>" \
                         + link_text \
                         + "<br>" \
                         + pep_dict['Title'] \
                         + " (" + pep_dict['Status'] + ")" \
                         + "</span>"

            return description_text + title_text

        def search_index_by_pep_number(text: str) -> int:
            index = 0

            for pep_number in all_pep_data_source['data']['PEP']:
                if text == str(pep_number):
                    return index
                index += 1

            return -1  # Not Found

        def get_inputed_pep_dict(text: int) -> dict:
            selected_index = search_index_by_pep_number(text)
            if selected_index == -1:
                return {}
            pep_dict = {}

            for key, value in all_pep_data_source.data.items():
                pep_dict[key] = value[selected_index]

            return pep_dict

        def get_pep_info(list_index: int) -> dict:
            pep_dict = {}

            for key, value in all_pep_data_source['data'].items():
                pep_dict[key] = value[list_index]

            return pep_dict

        def get_neighbor_node_info(pep_dict: dict,
                                   neighbor_type: str) -> dict:
            neighbors = dict()
            for key, value in all_pep_data_source.data.items():
                neighbors[key] = []

            for pep_id in pep_dict[neighbor_type]:
                index = search_index_by_pep_number(int(pep_id))
                work_pep_dict = get_pep_info(index)
                for key in neighbors.keys():
                    neighbors[key].append(work_pep_dict[key])
            return neighbors

        def generate_timeline_source_dict(pep_dict: dict) -> dict:

            in_edge_nodes_dict = get_neighbor_node_info(pep_dict,
                                                        'in_edge_nodes')
            out_edge_nodes_dict = get_neighbor_node_info(pep_dict,
                                                         'out_edge_nodes')
            in_edge_nodes_dict['y'] = [1.5] * len(in_edge_nodes_dict['PEP'])
            out_edge_nodes_dict['y'] = [0.5] * len(out_edge_nodes_dict['PEP'])

            timeline_source_dict = in_edge_nodes_dict
            del timeline_source_dict['in_degree']
            del timeline_source_dict['in_edge_nodes']
            del timeline_source_dict['out_degree']
            del timeline_source_dict['out_edge_nodes']
            for key, value in timeline_source_dict.items():
                timeline_source_dict[key] += out_edge_nodes_dict[key]
                if key == 'y':
                    timeline_source_dict[key].append(1)
                else:
                    timeline_source_dict[key].append(pep_dict[key])

            return timeline_source_dict

        def update_data_table(pep_dict: dict) -> None:
            linked_from_table_title_div.text = '<strong>PEP ' \
                                               + pep_dict['PEP'] \
                                               + ' is linked from ...</strong>'
            link_to_table_title_div.text = '<strong>PEP ' \
                                           + pep_dict['PEP'] \
                                           + ' links to ...</strong>'

            in_edge_nodes_dict = get_neighbor_node_info(pep_dict,
                                                        'in_edge_nodes')
            linked_from_table_source.data = in_edge_nodes_dict
            linked_from_table_source.change.emit()

            out_edge_nodes_dict = get_neighbor_node_info(pep_dict,
                                                         'out_edge_nodes')
            link_to_table_source.data = out_edge_nodes_dict
            link_to_table_source.change.emit()

        def update_timeline(pep_dict: dict) -> None:
            timeline_circle_dict = generate_timeline_source_dict(pep_dict)
            timeline_display_circle_source.data = timeline_circle_dict
            timeline_display_circle_source.change.emit()

            timeline_label_dict = generate_timeline_source_dict(pep_dict)
            timeline_label_dict['displayed_text'] = timeline_label_dict['PEP']
            timeline_label_source.data = timeline_label_dict
            timeline_label_source.change.emit()

            texts = ['PEP ' + pep_dict['PEP'] + ' is linked from...',
                     'PEP ' + pep_dict['PEP'] + ' links to...', ]

            timeline_desc_dict = dict(x=timeline_desc_source.data['x'],
                                      y=timeline_desc_source.data['y'],
                                      text=texts,
                                      size=timeline_desc_source.data['size'],
                                      )

            timeline_desc_source.data = timeline_desc_dict
            timeline_desc_source.change.emit()

        # 正規表現でタグを除去したいけど、JS変換後にreを呼べないので、暫定で<と>を外す
        inputed_text = inputed_text.replace('<', '')
        inputed_text = inputed_text.replace('>', '')
        inputed_text = inputed_text.strip()

        # 入力文字のチェック
        if not inputed_text:
            error_message_div.text = 'Please enter the number of PEP' + inputed_text
            return

        inputed_text = inputed_text.lstrip('0')

        # 表示の更新
        selected_pep_dict = get_inputed_pep_dict(inputed_text)

        if selected_pep_dict:
            title_div.text = create_header_text(selected_pep_dict)
            update_data_table(selected_pep_dict)
            update_timeline(selected_pep_dict)
            error_message_div.text = ""
        else:
            error_message_div.text = "Not Found: PEP " + inputed_text

    def callback_change_checkbox(py2_label_source=py2_release_label_data_source,
                                 py2_line_source=py2_release_line_data_source,
                                 py3_label_source=py3_release_label_data_source,
                                 py3_line_source=py3_release_line_data_source):

        def switch_show_or_hide(label_source, line_source, major_version):

            label_data = label_source.data
            line_data = line_source.data

            check_box_index_map = {2: 0, 3: 1}

            if check_box_index_map[major_version] in cb_obj.active:
                label_data['alpha'] = [1] * len(label_data['alpha'])
                line_data['alpha'] = [1] * len(line_data['alpha'])
            else:
                label_data['alpha'] = [0] * len(label_data['alpha'])
                line_data['alpha'] = [0] * len(line_data['alpha'])

            label_source.data = label_data
            label_source.change.emit()

            line_source.data = line_data
            line_source.change.emit()

        switch_show_or_hide(py2_label_source, py2_line_source, 2)
        switch_show_or_hide(py3_label_source, py3_line_source, 3)

    # Header Component
    pep_textinput = TextInput(title='PEP:',
                              placeholder='Please enter the PEP number.',
                              callback=CustomJS.from_py_func(callback_input_pep_number),
                              width=190)
    info_div = Div(width=200, height=20,
                   style={'background-color': '#175A89',
                          'padding': '5px',
                          'color': '#FFFFFF'})
    info_div.text = "<li>" \
                    "<a href='https://github.com/komo-fr/pep_map_site'><font color='#FFFFFF'>repository</font></a>" \
                    "</li>"
    inputbox_component = column(pep_textinput, error_message_div)
    color_desc_component = compo.generate_color_description_component()
    header_component = row(column(inputbox_component, color_desc_component),
                           title_div,
                           info_div)

    # Timeline Component
    checkbox_group.callback = CustomJS.from_py_func(callback_change_checkbox)
    timeline_plot = tl_compo.generate_timeline_plot(pep_graph,
                                                    timeline_display_circle_source,
                                                    timeline_label_source,
                                                    timeline_desc_label_source,
                                                    release_source_dict)
    as_of_date_div = Div(width=200, height=8, style={'color': 'red'})
    # TODO: fetch_start_datetimeを持っていないときの対応について決める
    fetch_datetime = pep_graph.graph['fetch_start_datetime'].strftime('%Y/%m/%d') \
        if 'fetch_start_datetime' in pep_graph.graph else 'Unknown'

    as_of_date_div.text = '* Data as of {}'.format(fetch_datetime)  # データ取得日
    timeline_component = column(timeline_plot, as_of_date_div)

    # Table Component
    margin_div = Div(width=50)
    link_to_table_component = column(link_to_table_title_div, link_to_data_table)
    linked_from_table_component = column(linked_from_table_title_div, linked_from_data_table)
    table_component = row(linked_from_table_component, margin_div, link_to_table_component)

    # TODO: ここでshowしなくても出力できる方法はないか?
    output_file(output_path, 'PEP Map | Timeline')

    show(column(header_component, checkbox_group, timeline_component, table_component))
Пример #16
0
    tools=PLOT_TOOLS,
    x_range=[0, DAYS],
)

plot9.line('x',
           'y',
           source=source_pr,
           line_width=PLOT_LINE_WIDTH,
           line_alpha=PLOT_LINE_ALPHA,
           line_color=PLOT_LINE_NEW_COLOR,
           legend_label='% Prevalence')

set_plot_details(plot9, hover9, PLOT_Y_LABEL2)

# misc text
intro.text = TEXT_INTRO
summary.text = TEXT_SUMMARY
summary.style = {'font-weight': 'bold'}

beta = round(h1.value * p1.value / 100, 4)
R0 = round(beta * period.value, 4)
im_threshold = max(round((1 - 1 / R0) * 100, 2),
                   0)  # could go negative for R0 < 1
pre_str = '&beta;: ' + str(beta) + '<br/>R<sub>0</sub>: ' + str(
    R0) + '<br/>Immunity threshold: ' + str(im_threshold) + '%'
extra_str = ''
stats_str = pre_str + '<br/>Transmissions: ' + str(ar_stats[0]) + ' / ' + str(
    ar_stats[3]) + '%' '<br/>Recoveries: ' + str(
        ar_stats[1]) + '<br/>Deaths: ' + str(ar_stats[2]) + extra_str
stats.text = stats_str
notes.text = TEXT_NOTES
def get_attributes(source, df, radius_source, radius_slider, point_info,
                   point_probabilities):
    try:
        idx = source.selected._property_values['indices'][0]
    except KeyError:
        radius_source.data = {'x': [], 'y': [], 'rad': []}
        return
    dpoint = df.iloc[source.data['index'][idx]]

    # check if selection didn't change when doing ColumnDataSource.stream(...)
    global source_idx
    global real_idx
    if source_idx == idx:
        # if index in the source didn't change, we assume the selection wasn't changed by the user
        dpoint = df.iloc[real_idx]
    else:
        source_idx = idx
        real_idx = dpoint.name

    # draw radius circle
    radius_source.data = {
        'x': [dpoint.x_cats_ae],
        'y': [dpoint.y_cats_ae],
        'rad': [radius_slider.value]
    }

    # update table with point info
    bin_class = {0: 'Normal', 1: 'Attack'}
    visual_pred = visual_classifier.predict(np.array(
        [dpoint.x_cats_ae, dpoint.y_cats_ae]),
                                            eps=radius_slider.value)
    visual_pred_str = ('✅ ' if visual_pred == dpoint.category else '❌ ') + \
        categories_short[visual_pred] if visual_pred != -1 else 'Unknown'

    cats_pred = ('✅ ' if dpoint.cats_ae_pred == (dpoint.category != 9) else
                 '❌ ') + bin_class[dpoint.cats_ae_pred]
    orig_pred = ('✅ ' if dpoint.original_pred == (dpoint.category != 9) else
                 '❌ ') + bin_class[dpoint.original_pred]
    point_info.text = '<div class="attrs">' + tabulate(
        [
            ['index', real_idx],
            ['category', categories_short[dpoint.category]],
            ['cats_ae_pred', cats_pred],
            ['original_pred', orig_pred],
            ['x', '{:.4f}'.format(dpoint.x_cats_ae)],
            ['y', '{:.4f}'.format(dpoint.y_cats_ae)],
            ['visual_pred', visual_pred_str],
        ],
        tablefmt='html') + '</div>'
    probs, counts = visual_classifier.predict_proba(np.array(
        [dpoint.x_cats_ae, dpoint.y_cats_ae]),
                                                    eps=radius_slider.value,
                                                    return_counts=True)

    source = ColumnDataSource(data={
        'category': categories_short,
        'probability': probs
    })
    barplot = figure(
        x_range=(0, 1),
        y_range=categories_short,
        plot_height=200,
        toolbar_location=None,
        title='Visual classifier\'s categories probability distribution')
    barplot.hbar(y='category',
                 right='probability',
                 source=source,
                 height=0.95,
                 fill_color=factor_cmap('category',
                                        palette=cmap,
                                        factors=categories_short),
                 line_color='white')

    outlierness_score = Div()
    outlierness_score.text = "<p>#(samples within eps) = %d</p>" \
                             "<p>eps / #(samples within eps) = %s</p>" % \
                             (counts,
                              counts / radius_slider.value)
    point_probabilities.children = [barplot, outlierness_score]