Exemple #1
0
    def __init__(self, lineFig):
        """
        """

        self.lineFig = lineFig
        self.marks = self.lineFig.marks
        y_ord = bq.OrdinalScale()
        x_sc = bq.LinearScale()

        legendLabels = []
        self.colours = []
        for mark in self.marks:
            legendLabels += mark.labels
            self.colours += mark.colors[:len(mark.labels)]

        self.bars = bq.Bars(
            y=[1] *
            len(legendLabels),  # all bars have a amplitude of 1    ## ?
            x=legendLabels,
            scales={
                'y': x_sc,
                'x': y_ord
            },
            colors=self.colours,
            padding=0.6,
            orientation='horizontal',
            stroke='white')  ## remove the black border around the bar

        self.bars.opacities = [ScatLegend.OPAC_BRIGHT] * len(self.bars.x)

        ax_y = bq.Axis(scale=y_ord, orientation="vertical")
        ax_x = bq.Axis(scale=x_sc)
        ax_x.visible = False
        margin = dict(top=40, bottom=0, left=110, right=5)
        self.fig = bq.Figure(marks=[self.bars],
                             axes=[ax_y, ax_x],
                             fig_margin=margin)

        # Variable height depending on number of bars in legend
        self.fig.layout.height = str(45 + 20 * len(legendLabels)) + 'px'
        self.fig.layout.width = '170px'

        self.fig.min_aspect_ratio = 0.000000000001  # effectively remove aspect ratio constraint
        self.fig.max_aspect_ratio = 999999999999999  # effectively remove aspect ratio constraint
        self.fig.background_style = {'fill': 'White'}

        self.bars.on_element_click(self.changeOpacity)
Exemple #2
0
def create_plot():
    index = pd.date_range(start='2000-06-01', end='2001-06-01',
                          freq='30min') + timedelta(minutes=15)
    s = pd.Series(np.full(len(index), np.nan), index=index)

    x = index.values
    y = s

    x_sc = bq.DateScale()
    y_sc = bq.LinearScale(min=0)

    line = bq.Lines(
        x=x,
        y=y,
        scales={
            'x': x_sc,
            'y': y_sc
        },
        #display_legend=True, labels=["line 1"],
        #fill='bottom', # opacity does work with this option
        #fill_opacities = [0.5] *len(x)
    )

    panzoom = bq.PanZoom(scales={'x': [x_sc], 'y': [y_sc]})
    #p = bq.Scatter(x=x, y=y, scales= {'x': x_sc, 'y': y_sc})
    ax_x = bq.Axis(scale=x_sc)
    ax_y = bq.Axis(scale=y_sc, orientation='vertical', tick_format='0.2f')

    #fig = bq.Figure(marks=[line, p], axes=[ax_x, ax_y])
    fig = bq.Figure(marks=[line], axes=[ax_x, ax_y], interaction=panzoom)
    fig.layout.width = '95%'

    #p.interactions = {'click': 'select', 'hover': 'tooltip'}
    #p.selected_style = {'opacity': 1.0, 'fill': 'DarkOrange', 'stroke': 'Red'}
    #p.unselected_style = {'opacity': 0.5}
    #p.tooltip = bq.Tooltip(fields=['x', 'y'], formats=['', '.2f'])

    #sel = bq.interacts.IndexSelector(scale=p.scales['x'])
    #
    #def update_range(*args):
    #    if sel.selected:
    #        print(sel.selected[0])
    #
    #sel.observe(update_range, 'selected')
    #fig.interaction = sel

    return fig, line
Exemple #3
0
 def gui_tips (self) :
     sc_r = bq.LinearScale(min=0, max=np.pi)
     sc_c = bq.ColorScale(colors=Palette.mkpal(["#000000", "#FFFFFF"]))
     tips = {}
     for shape in self._tips_shapes :
         for end in ("src", "dst") :
             edges = self.edges[self.edges["_shape_" + end] == shape]
             scatt = bq.Scatter(x=edges["_x_" + end],
                                y=edges["_y_" + end],
                                marker=shape,
                                rotation=edges["_angle_" + end],
                                color=edges["_color_" + end],
                                stroke="#000000",
                                scales={"x" : self._x_scale,
                                        "y" : self._y_scale,
                                        "color" : sc_c,
                                        "rotation" : sc_r,
                                })
             tips[end,shape] = scatt
     return tips
Exemple #4
0
def scale_x():
    return bqplot.LinearScale(min=0, max=1, allow_padding=False)
Exemple #5
0
def scale_y():
    return bqplot.LinearScale(min=2, max=3, allow_padding=False)
Exemple #6
0
class Plot_interface(object):
    """Class to handle map and plot interaction"""

    # Declare class attributes
    pyccd_flag = False
    pyccd_flag2 = False
    current_band = ''
    band_index1 = 4
    band_index2 = 4
    click_col = ''
    point_color = ['#43a2ca']
    click_df = pd.DataFrame()
    sample_col = ''
    sample_df = pd.DataFrame()
    PyCCDdf = pd.DataFrame()
    table = pd.DataFrame()
    band_list = ['BLUE', 'GREEN', 'RED', 'NIR', 'SWIR1', 'SWIR2']
    year_range = [1986, 2018]
    doy_range = [1, 365]
    step = 1  #in years

    # Create widget controls
    next_pt = Button(value=False, description='Next point', disabled=False)
    previous_pt = Button(value=False,
                         description='Previous point',
                         disabled=False)
    pyccd_button = Button(value=False,
                          description='Run PyCCD 1',
                          disabled=False)
    pyccd_button2 = Button(value=False,
                           description='Run PyCCD 2',
                           disabled=False)
    band_selector1 = Dropdown(
        options=['BLUE', 'GREEN', 'RED', 'NIR', 'SWIR1', 'SWIR2'],
        description='Select band',
        value=None)
    band_selector2 = Dropdown(
        options=['BLUE', 'GREEN', 'RED', 'NIR', 'SWIR1', 'SWIR2'],
        description='Select band',
        value=None)
    image_band_1 = Dropdown(
        options=['BLUE', 'GREEN', 'RED', 'NIR', 'SWIR1', 'SWIR2'],
        description='Red:',
        value='SWIR1')
    image_band_2 = Dropdown(
        options=['BLUE', 'GREEN', 'RED', 'NIR', 'SWIR1', 'SWIR2'],
        description='Green:',
        value='NIR')
    image_band_3 = Dropdown(
        options=['BLUE', 'GREEN', 'RED', 'NIR', 'SWIR1', 'SWIR2'],
        description='Blue:',
        value='RED')
    stretch_min = FloatText(value=0, description='Min:', disabled=False)
    stretch_max = FloatText(value=6000, description='Min:', disabled=False)

    # Clear layers on map
    clear_layers = Button(value=False, description='Clear Map', disabled=False)

    # Color points by DOY
    color_check = widgets.Checkbox(value=False,
                                   description='Color DOY',
                                   disabled=False)

    idBox = widgets.Text(value='0', description='ID:', disabled=False)

    ylim = widgets.IntRangeSlider(value=[0, 4000],
                                  min=0,
                                  max=10000,
                                  step=500,
                                  description='YLim:',
                                  disabled=False,
                                  continuous_update=False,
                                  orientation='horizontal',
                                  readout=True,
                                  readout_format='d')

    xlim = widgets.IntRangeSlider(value=[2000, 2018],
                                  min=1986,
                                  max=2018,
                                  step=1,
                                  description='XLim:',
                                  disabled=False,
                                  continuous_update=False,
                                  orientation='horizontal',
                                  readout=True,
                                  readout_format='d')

    ylim2 = widgets.IntRangeSlider(value=[0, 4000],
                                   min=0,
                                   max=10000,
                                   step=500,
                                   description='YLim:',
                                   disabled=False,
                                   continuous_update=False,
                                   orientation='horizontal',
                                   readout=True,
                                   readout_format='d')

    xlim2 = widgets.IntRangeSlider(value=[2000, 2018],
                                   min=1986,
                                   max=2018,
                                   step=1,
                                   description='XLim:',
                                   disabled=False,
                                   continuous_update=False,
                                   orientation='horizontal',
                                   readout=True,
                                   readout_format='d')

    coords_label = Label()
    pt_message = HTML("Current ID: ")
    time_label = HTML(value='')
    selected_label = HTML("ID of selected point")
    hover_label = HTML("test value")
    text_brush = HTML(value='Selected year range:')
    kml_link = HTML(value='KML:')
    error_label = HTML(value='Load a point')

    # Create map including streets and satellite and controls
    m = ipyleaflet.Map(zoom=5,
                       layout={'height': '400px'},
                       center=(3.3890701010382958, -67.32297252983098),
                       dragging=True,
                       close_popup_on_click=False,
                       basemap=ipyleaflet.basemaps.Esri.WorldStreetMap)

    streets = ipyleaflet.basemap_to_tiles(
        ipyleaflet.basemaps.Esri.WorldImagery)
    m.add_layer(streets)

    dc = ipyleaflet.DrawControl(marker={'shapeOptions': {
        'color': '#ff0000'
    }},
                                polygon={},
                                circle={},
                                circlemarker={},
                                polyline={})

    # Table widget
    table_widget = qgrid.show_grid(table, show_toolbar=False)

    # Set plots
    # Plot scales. HERE
    lc1_x = bqplot.DateScale(min=datetime.date(xlim.value[0], 2, 1),
                             max=datetime.date(xlim.value[1], 1, 1))

    # DOY scale
    lc1_x3 = bqplot.LinearScale(min=0, max=365)

    lc2_y = bqplot.LinearScale(min=ylim.value[0], max=ylim.value[1])

    lc1_x2 = bqplot.DateScale(min=datetime.date(xlim.value[0], 2, 1),
                              max=datetime.date(xlim.value[1], 1, 1))
    lc2_y2 = bqplot.LinearScale(min=ylim.value[0], max=ylim.value[1])

    # Main scatter plot for samples
    lc2 = bqplot.Scatter(x=[],
                         y=[],
                         scales={
                             'x': lc1_x,
                             'y': lc2_y
                         },
                         size=[1, 1],
                         interactions={
                             'click': 'select',
                             'hover': 'tooltip'
                         },
                         selected_style={
                             'opacity': 1.0,
                             'fill': 'DarkOrange',
                             'stroke': 'Red'
                         },
                         unselected_style={'opacity': 0.5},
                         display_legend=True,
                         labels=['Sample point'])

    # Pyccd model fit
    lc4 = bqplot.Lines(
        x=[],
        y=[],
        colors=['black'],
        stroke_width=3,
        scales={
            'x': lc1_x,
            'y': lc2_y
        },
        size=[1, 1],
    )

    # Pyccd model break
    lc5 = bqplot.Scatter(x=[],
                         y=[],
                         marker='triangle-up',
                         colors=['red'],
                         scales={
                             'x': lc1_x,
                             'y': lc2_y
                         },
                         size=[1, 1],
                         display_legend=False,
                         labels=['Model Endpoint'])

    # Scatter plot for clicked points in map
    lc3 = bqplot.Scatter(x=[],
                         y=[],
                         scales={
                             'x': lc1_x2,
                             'y': lc2_y2
                         },
                         size=[1, 1],
                         colors=['gray'],
                         interactions={
                             'click': 'select',
                             'hover': 'tooltip'
                         },
                         selected_style={
                             'opacity': 1.0,
                             'fill': 'DarkOrange',
                             'stroke': 'Red'
                         },
                         unselected_style={'opacity': 0.5},
                         display_legend=True,
                         labels=['Clicked point'])

    # Pyccd model fit for clicked point
    lc6 = bqplot.Lines(
        x=[],
        y=[],
        colors=['black'],
        stroke_width=3,
        scales={
            'x': lc1_x2,
            'y': lc2_y2
        },
        size=[1, 1],
    )

    # Pyccd model break for clicked point
    lc7 = bqplot.Scatter(x=[],
                         y=[],
                         marker='triangle-up',
                         colors=['red'],
                         scales={
                             'x': lc1_x2,
                             'y': lc2_y2
                         },
                         size=[1, 1],
                         display_legend=False,
                         labels=['Model Endpoint'])

    # Scatter for sample DOY
    lc8 = bqplot.Scatter(x=[],
                         y=[],
                         scales={
                             'x': lc1_x3,
                             'y': lc2_y
                         },
                         size=[1, 1],
                         interactions={
                             'click': 'select',
                             'hover': 'tooltip'
                         },
                         selected_style={
                             'opacity': 1.0,
                             'fill': 'DarkOrange',
                             'stroke': 'Red'
                         },
                         unselected_style={'opacity': 0.5},
                         display_legend=True,
                         labels=['Sample point'])

    # Plot axes.
    x_ax1 = bqplot.Axis(label='Date',
                        scale=lc1_x,
                        num_ticks=6,
                        tick_format='%Y')
    x_ax2 = bqplot.Axis(label='Date',
                        scale=lc1_x2,
                        num_ticks=6,
                        tick_format='%Y')
    x_ax3 = bqplot.Axis(label='DOY', scale=lc1_x3, num_ticks=6)

    x_ay1 = bqplot.Axis(label='SWIR1', scale=lc2_y, orientation='vertical')
    x_ay2 = bqplot.Axis(label='SWIR1', scale=lc2_y2, orientation='vertical')

    # Create a figure for sample points.
    fig = bqplot.Figure(marks=[lc2, lc4, lc5],
                        axes=[x_ax1, x_ay1],
                        layout={
                            'height': '300px',
                            'width': '100%'
                        },
                        title="Sample TS")

    # Create a figure for clicked points.
    fig2 = bqplot.Figure(marks=[lc3, lc6, lc7],
                         axes=[x_ax2, x_ay2],
                         layout={
                             'height': '300px',
                             'width': '100%'
                         },
                         title="Clicked TS")

    # Create a figure for sample DOY.
    fig3 = bqplot.Figure(marks=[lc8],
                         axes=[x_ax3, x_ay1],
                         layout={
                             'height': '300px',
                             'width': '100%'
                         },
                         title="Clicked TS")

    def __init__(self, navigate):
        Plot_interface.navigate = navigate
        Plot_interface.band_index1 = 4
        Plot_interface.band_index2 = 4
        Plot_interface.pyccd_flag = False
        Plot_interface.pyccd_flag2 = False
        Plot_interface.table = None
        # Set up database
        conn = sqlite3.connect(Plot_interface.navigate.dbPath)
        Plot_interface.current_id = Plot_interface.navigate.current_id
        Plot_interface.c = conn.cursor()
        Plot_interface.minv = 0
        Plot_interface.maxv = 6000
        Plot_interface.b1 = 'SWIR1'
        Plot_interface.b2 = 'NIR'
        Plot_interface.b3 = 'RED'

    @classmethod
    def map_point(self):
        gjson = ipyleaflet.GeoJSON(
            data=Plot_interface.navigate.fc_df['geometry'][
                Plot_interface.current_id],
            name="Sample point")
        Plot_interface.m.center = gjson.data['coordinates'][::-1]
        Plot_interface.m.zoom = 12
        Plot_interface.m.add_layer(gjson)
        kmlstr = ee.FeatureCollection(
            ee.Geometry.Point(Plot_interface.navigate.fc_df['geometry'][
                Plot_interface.current_id]['coordinates'])).getDownloadURL(
                    "kml")
        Plot_interface.kml_link.value = "<a '_blank' rel='noopener noreferrer' href={}>KML Link</a>".format(
            kmlstr)

    @classmethod
    def get_ts(self):
        #try:
        Plot_interface.error_label.value = 'Loading'
        Plot_interface.current_band = Plot_interface.band_list[
            Plot_interface.band_index1]
        Plot_interface.sample_col = get_full_collection(
            Plot_interface.navigate.fc_df['geometry'][
                Plot_interface.current_id]['coordinates'],
            Plot_interface.year_range, Plot_interface.doy_range)
        Plot_interface.sample_df = get_df_full(
            Plot_interface.sample_col,
            Plot_interface.navigate.fc_df['geometry'][
                Plot_interface.current_id]['coordinates']).dropna()
        Plot_interface.error_label.value = 'Point loaded!'
        #except:
        #    Plot_interface.error_label.value = 'Point could not be loaded!'

    def clear_map(b):
        Plot_interface.m.clear_layers()
        Plot_interface.m.add_layer(Plot_interface.streets)
        Plot_interface.map_point()

    @classmethod
    def plot_ts(self):
        current_band = Plot_interface.band_list[Plot_interface.band_index1]
        Plot_interface.lc2.x = Plot_interface.sample_df['datetime']
        if Plot_interface.color_check.value == False:
            Plot_interface.lc2.colors = list(Plot_interface.point_color)
            Plot_interface.lc8.colors = list(Plot_interface.point_color)
        else:
            Plot_interface.lc2.colors = list(
                Plot_interface.sample_df['color'].values)
            Plot_interface.lc8.colors = list(
                Plot_interface.sample_df['color'].values)
        Plot_interface.lc2.y = Plot_interface.sample_df[current_band]
        Plot_interface.x_ay1.label = current_band
        Plot_interface.lc4.x = []
        Plot_interface.lc4.y = []
        Plot_interface.lc5.x = []
        Plot_interface.lc5.y = []

        Plot_interface.lc8.x = Plot_interface.sample_df['doy']
        Plot_interface.lc8.y = Plot_interface.sample_df[current_band]

        #if pyccd_flag:
        if Plot_interface.pyccd_flag:
            Plot_interface.run_pyccd(0)

    # Go back or forth between sample points
    def advance(b):
        # Plot point in map
        Plot_interface.lc4.x = []
        Plot_interface.lc4.y = []
        Plot_interface.lc5.x = []
        Plot_interface.lc5.y = []
        Plot_interface.lc5.display_legend = False
        Plot_interface.pyccd_flag = False
        Plot_interface.current_id += 1
        Plot_interface.pt_message.value = "Point ID: {}".format(
            Plot_interface.current_id)
        Plot_interface.map_point()
        Plot_interface.get_ts()
        Plot_interface.plot_ts()
        Plot_interface.change_table(0)
        Plot_interface.navigate.valid.value = False
        Plot_interface.navigate.description = 'Not Saved'

    def decrease(b):
        # Plot point in map
        Plot_interface.lc4.x = []
        Plot_interface.lc4.y = []
        Plot_interface.lc5.x = []
        Plot_interface.lc5.y = []
        Plot_interface.lc5.display_legend = False
        Plot_interface.pyccd_flag = False
        Plot_interface.current_id -= 1
        Plot_interface.pt_message.value = "Point ID: {}".format(
            Plot_interface.current_id)
        Plot_interface.map_point()
        Plot_interface.get_ts()
        Plot_interface.plot_ts()
        Plot_interface.change_table(0)
        Plot_interface.navigate.valid.value = False
        Plot_interface.navigate.description = 'Not Saved'

    def change_table(b):
        # Update the table based on current ID

        # Get header
        cursor = Plot_interface.c.execute('select * from measures')
        names = list(map(lambda x: x[0], cursor.description))
        previous_inputs = pd.DataFrame()
        for i, row in enumerate(
                Plot_interface.c.execute(
                    "SELECT * FROM measures WHERE id = '%s'" %
                    Plot_interface.current_id)):
            previous_inputs[i] = row
        previous_inputs = previous_inputs.T
        if previous_inputs.shape[0] > 0:
            previous_inputs.columns = names
        Plot_interface.table_widget.df = previous_inputs

    # Functions for changing image stretch
    def change_image_band1(change):
        new_band = change['new']
        Plot_interface.b1 = new_band

    def change_image_band2(change):
        new_band = change['new']
        Plot_interface.b2 = new_band

    def change_image_band3(change):
        new_band = change['new']
        Plot_interface.b3 = new_band

    # Band selection for sample point
    def on_band_selection1(change):
        new_band = change['new']
        #global band_index
        band_index = change['owner'].index
        Plot_interface.band_index1 = band_index
        Plot_interface.plot_ts()

    # Band selection for clicked point
    def on_band_selection2(change):
        new_band = change['new']
        band_index = change['owner'].index
        Plot_interface.band_index2 = band_index
        Plot_interface.lc3.x = Plot_interface.click_df['datetime']
        Plot_interface.lc3.y = Plot_interface.click_df[new_band]
        Plot_interface.x_ay2.label = new_band
        if Plot_interface.pyccd_flag2:
            Plot_interface.run_pyccd2(0)

    def change_yaxis(value):
        Plot_interface.lc2_y.min = Plot_interface.ylim.value[0]
        Plot_interface.lc2_y.max = Plot_interface.ylim.value[1]

    def change_xaxis(value):
        Plot_interface.lc1_x.min = datetime.date(Plot_interface.xlim.value[0],
                                                 2, 1)
        Plot_interface.lc1_x.max = datetime.date(Plot_interface.xlim.value[1],
                                                 2, 1)
        Plot_interface.year_range = [
            Plot_interface.xlim.value[0], Plot_interface.xlim.value[1]
        ]

    def change_yaxis2(value):
        Plot_interface.lc2_y2.min = Plot_interface.ylim2.value[0]
        Plot_interface.lc2_y2.max = Plot_interface.ylim2.value[1]

    def change_xaxis2(value):
        Plot_interface.lc1_x2.min = datetime.date(
            Plot_interface.xlim2.value[0], 2, 1)
        Plot_interface.lc1_x2.max = datetime.date(
            Plot_interface.xlim2.value[1], 2, 1)

    def hover_event(self, target):
        Plot_interface.hover_label.value = str(target['data']['x'])

    # Add layer from clicked point in sample TS figure
    def click_event(self, target):
        pt_index = target['data']['index']
        current_band = Plot_interface.band_list[Plot_interface.band_index1]
        image_id = Plot_interface.sample_df['id'].values[pt_index]
        selected_image = ee.Image(
            Plot_interface.sample_col.filterMetadata('system:index', 'equals',
                                                     image_id).first())
        tile_url = GetTileLayerUrl(
            selected_image.visualize(min=Plot_interface.stretch_min.value,
                                     max=Plot_interface.stretch_max.value,
                                     bands=[
                                         Plot_interface.b1, Plot_interface.b2,
                                         Plot_interface.b3
                                     ]))

        Plot_interface.m.add_layer(
            ipyleaflet.TileLayer(url=tile_url, name=image_id))

    # Add layer from clicked point in clicked TS figure
    def click_event2(self, target):
        pt_index = target['data']['index']
        current_band = Plot_interface.band_list[Plot_interface.band_index2]
        #Find clicked image. .values needed to access the nth element of that list instead of indexing by ID
        image_id = Plot_interface.click_df['id'].values[pt_index]
        selected_image = ee.Image(
            Plot_interface.click_col.filterMetadata('system:index', 'equals',
                                                    image_id).first())
        tile_url = GetTileLayerUrl(
            selected_image.visualize(min=Plot_interface.minv,
                                     max=Plot_interface.maxv,
                                     bands=[
                                         Plot_interface.b1, Plot_interface.b2,
                                         Plot_interface.b3
                                     ]))

        Plot_interface.m.add_layer(
            ipyleaflet.TileLayer(url=tile_url, name=image_id))

    # Plot TS from clicked point
    def handle_draw(self, action, geo_json):
        # Get the selected coordinates from the map's drawing control.
        current_band = Plot_interface.band_list[Plot_interface.band_index2]
        coords = geo_json['geometry']['coordinates']
        Plot_interface.click_col = get_full_collection(
            coords, Plot_interface.year_range, Plot_interface.doy_range)
        Plot_interface.click_df = get_df_full(Plot_interface.click_col,
                                              coords).dropna()
        Plot_interface.lc6.x = []
        Plot_interface.lc6.y = []
        Plot_interface.lc7.x = []
        Plot_interface.lc7.y = []
        Plot_interface.lc3.x = Plot_interface.click_df['datetime']
        Plot_interface.lc3.y = Plot_interface.click_df[current_band]

        if Plot_interface.color_check.value == False:
            Plot_interface.lc3.colors = list(Plot_interface.point_color)
        else:
            Plot_interface.lc3.colors = list(
                Plot_interface.click_df['color'].values)

    # Plotting pyccd
    def plot_pyccd(results, band, plotband, dates, yl, ylabel, ts_type):
        mask = np.array(results['processing_mask']).astype(np.bool_)
        predicted_values = []
        prediction_dates = []
        break_dates = []
        start_dates = []

        for num, result in enumerate(results['change_models']):
            days = np.arange(result['start_day'], result['end_day'] + 1)
            prediction_dates.append(days)
            break_dates.append(result['break_day'])
            start_dates.append(result['start_day'])
            intercept = result[list(result.keys())[6 + band]]['intercept']
            coef = result[list(result.keys())[6 + band]]['coefficients']

            predicted_values.append(
                intercept + coef[0] * days +
                coef[1] * np.cos(days * 1 * 2 * np.pi / 365.25) +
                coef[2] * np.sin(days * 1 * 2 * np.pi / 365.25) +
                coef[3] * np.cos(days * 2 * 2 * np.pi / 365.25) +
                coef[4] * np.sin(days * 2 * 2 * np.pi / 365.25) +
                coef[5] * np.cos(days * 3 * 2 * np.pi / 365.25) +
                coef[6] * np.sin(days * 3 * 2 * np.pi / 365.25))

        num_breaks = len(break_dates)

        break_y = [plotband[dates == i][0] for i in break_dates]

        #break_y = [0] * num_breaks
        break_dates_plot = [
            datetime.datetime.fromordinal(i).strftime('%Y-%m-%d %H:%M:%S.%f')
            for i in break_dates
        ]

        plot_dates = np.array(
            [datetime.datetime.fromordinal(i) for i in (dates)])

        # Predicted curves
        all_dates = []
        all_preds = []
        for _preddate, _predvalue in zip(prediction_dates, predicted_values):
            all_dates.append(_preddate)
            all_preds.append(_predvalue)

        all_preds = [item for sublist in all_preds for item in sublist]
        all_dates = [item for sublist in all_dates for item in sublist]

        date_ord = [
            datetime.datetime.fromordinal(i).strftime('%Y-%m-%d %H:%M:%S.%f')
            for i in all_dates
        ]
        _x = np.array(date_ord, dtype='datetime64')
        _y = all_preds

        if ts_type == 'sample_ts':
            Plot_interface.lc4.x = _x
            Plot_interface.lc4.y = _y
            Plot_interface.lc5.x = np.array(break_dates_plot,
                                            dtype='datetime64')
            Plot_interface.lc5.y = break_y
        elif ts_type == 'clicked_ts':
            Plot_interface.lc6.x = _x
            Plot_interface.lc6.y = _y
            Plot_interface.lc7.x = np.array(break_dates_plot,
                                            dtype='datetime64')
            Plot_interface.lc7.y = break_y

    # Go to a specific sample
    def go_to_sample(b):
        # Plot point in map
        Plot_interface.lc4.x = []
        Plot_interface.lc4.y = []
        Plot_interface.lc5.x = []
        Plot_interface.lc5.y = []
        Plot_interface.lc5.display_legend = False
        Plot_interface.pyccd_flag = False
        Plot_interface.current_id = int(b.value)
        Plot_interface.pt_message.value = "Point ID: {}".format(
            Plot_interface.current_id)
        Plot_interface.navigate.valid.value = False
        Plot_interface.navigate.description = 'Not Saved'
        Plot_interface.map_point()
        Plot_interface.get_ts()
        Plot_interface.plot_ts()

    # Run pyccd
    def run_pyccd(b):
        # Run pyCCD on current point
        Plot_interface.pyccd_flag = True
        Plot_interface.lc5.display_legend = True
        dfPyCCD = Plot_interface.sample_df

        dfPyCCD['pixel_qa'][dfPyCCD['pixel_qa'] > 4] = 0

        #TODO: Paramaterize everything
        params = {
            'QA_BITPACKED': False,
            'QA_FILL': 255,
            'QA_CLEAR': 0,
            'QA_WATER': 1,
            'QA_SHADOW': 2,
            'QA_SNOW': 3,
            'QA_CLOUD': 4
        }

        dates = np.array(dfPyCCD['ord_time'])
        blues = np.array(dfPyCCD['BLUE'])
        greens = np.array(dfPyCCD['GREEN'])
        reds = np.array(dfPyCCD['RED'])
        nirs = np.array(dfPyCCD['NIR'])
        swir1s = np.array(dfPyCCD['SWIR1'])
        swir2s = np.array(dfPyCCD['SWIR2'])
        thermals = np.array(dfPyCCD['THERMAL'])
        qas = np.array(dfPyCCD['pixel_qa'])
        results = ccd.detect(dates,
                             blues,
                             greens,
                             reds,
                             nirs,
                             swir1s,
                             swir2s,
                             thermals,
                             qas,
                             params=params)

        band_names = [
            'Blue SR', 'Green SR', 'Red SR', 'NIR SR', 'SWIR1 SR', 'SWIR2 SR',
            'THERMAL'
        ]
        plotlabel = band_names[Plot_interface.band_index1]

        plot_arrays = [blues, greens, reds, nirs, swir1s, swir2s]
        plotband = plot_arrays[Plot_interface.band_index1]
        Plot_interface.plot_pyccd(results, Plot_interface.band_index1,
                                  plotband, dates, (0, 4000), 'PyCCD Results',
                                  'sample_ts')

    def run_pyccd2(b):
        # Run pyCCD on current point
        Plot_interface.pyccd_flag2 = True

        # Display the legend
        Plot_interface.lc7.display_legend = True

        dfPyCCD = Plot_interface.click_df

        # First two lines no longer required bc we are removing NA's when we load the TS
        dfPyCCD['pixel_qa'][dfPyCCD['pixel_qa'] > 4] = 0

        #TODO: Paramaterize everything
        params = {
            'QA_BITPACKED': False,
            'QA_FILL': 255,
            'QA_CLEAR': 0,
            'QA_WATER': 1,
            'QA_SHADOW': 2,
            'QA_SNOW': 3,
            'QA_CLOUD': 4
        }

        dates = np.array(dfPyCCD['ord_time'])
        blues = np.array(dfPyCCD['BLUE'])
        greens = np.array(dfPyCCD['GREEN'])
        reds = np.array(dfPyCCD['RED'])
        nirs = np.array(dfPyCCD['NIR'])
        swir1s = np.array(dfPyCCD['SWIR1'])
        swir2s = np.array(dfPyCCD['SWIR2'])
        thermals = np.array(dfPyCCD['THERMAL'])
        qas = np.array(dfPyCCD['pixel_qa'])
        results = ccd.detect(dates,
                             blues,
                             greens,
                             reds,
                             nirs,
                             swir1s,
                             swir2s,
                             thermals,
                             qas,
                             params=params)

        band_names = [
            'Blue SR', 'Green SR', 'Red SR', 'NIR SR', 'SWIR1 SR', 'SWIR2 SR',
            'THERMAL'
        ]
        plotlabel = band_names[Plot_interface.band_index2]

        plot_arrays = [blues, greens, reds, nirs, swir1s, swir2s]
        plotband = plot_arrays[Plot_interface.band_index2]
        Plot_interface.plot_pyccd(results, Plot_interface.band_index2,
                                  plotband, dates, (0, 4000), 'PyCCD Results',
                                  'clicked_ts')

    ylim.observe(change_yaxis)
    xlim.observe(change_xaxis)
    ylim2.observe(change_yaxis2)
    xlim2.observe(change_xaxis2)
    next_pt.on_click(advance)
    previous_pt.on_click(decrease)
    pyccd_button.on_click(run_pyccd)
    pyccd_button2.on_click(run_pyccd2)
    clear_layers.on_click(clear_map)
    band_selector1.observe(on_band_selection1, names='value')
    band_selector2.observe(on_band_selection2, names='value')

    image_band_1.observe(change_image_band1, names='value')
    image_band_2.observe(change_image_band2, names='value')
    image_band_3.observe(change_image_band3, names='value')

    lc2.on_element_click(click_event)
    lc2.tooltip = hover_label
    lc2.on_hover(hover_event)

    lc3.on_element_click(click_event2)
    lc3.tooltip = hover_label
    lc3.on_hover(hover_event)

    idBox.on_submit(go_to_sample)

    dc.on_draw(handle_draw)
    m.add_control(dc)
    m.add_control(ipyleaflet.LayersControl())
Exemple #7
0
    def __init__(self, zoom_y=True, **kwargs):
        super().__init__(**kwargs)
        self.x_scale = bqplot.LinearScale(allow_padding=False)
        self.y_scale = bqplot.LinearScale(allow_padding=False)
        widgets.link((self, 'x_min'), (self.x_scale, 'min'))
        widgets.link((self, 'x_max'), (self.x_scale, 'max'))
        widgets.link((self, 'y_min'), (self.y_scale, 'min'))
        widgets.link((self, 'y_max'), (self.y_scale, 'max'))

        self.x_axis = bqplot.Axis(scale=self.x_scale)
        self.y_axis = bqplot.Axis(scale=self.y_scale, orientation='vertical')
        widgets.link((self, 'x_label'), (self.x_axis, 'label'))
        widgets.link((self, 'y_label'), (self.y_axis, 'label'))
        self.x_axis.color = blackish
        self.y_axis.color = blackish
        self.x_axis.label_color = blackish
        self.y_axis.label_color = blackish
        # self.y_axis.tick_style = {'fill': blackish, 'stroke':'none'}
        self.y_axis.grid_color = blackish
        self.x_axis.grid_color = blackish
        self.x_axis.label_offset = "2em"
        self.y_axis.label_offset = "3em"
        self.x_axis.grid_lines = 'none'
        self.y_axis.grid_lines = 'none'

        self.axes = [self.x_axis, self.y_axis]
        self.scales = {'x': self.x_scale, 'y': self.y_scale}

        self.figure = bqplot.Figure(axes=self.axes)
        self.figure.background_style = {'fill': 'none'}
        self.figure.padding_y = 0
        self.figure.fig_margin = {'bottom': 40, 'left': 60, 'right': 10, 'top': 10}

        self.interacts = {}
        self.interacts['pan-zoom'] = bqplot.PanZoom(scales={'x': [self.x_scale], 'y': [self.y_scale] if zoom_y else []})
        self.interacts['select-rect'] = bqplot.interacts.BrushSelector(x_scale=self.x_scale, y_scale=self.y_scale, color="green")
        self.interacts['select-x'] = bqplot.interacts.BrushIntervalSelector(scale=self.x_scale, color="green")
        self._brush = self.interacts['select-rect']
        self._brush_interval = self.interacts['select-x']

        # TODO: put the debounce in the presenter?
        @vaex.jupyter.debounced(DEBOUNCE_SELECT)
        def update_brush(*args):
            with self.output:
                if not self._brush.brushing:  # if we ended _brushing, reset it
                    self.figure.interaction = None
                if self._brush.selected is not None:
                    x1, x2 = self._brush.selected_x
                    y1, y2 = self._brush.selected_y
                    # (x1, y1), (x2, y2) = self._brush.selected
                    # mode = self.modes_names[self.modes_labels.index(self.button_selection_mode.value)]
                    self.presenter.select_rectangle(x1, x2, y1, y2)
                else:
                    self.presenter.select_nothing()
                if not self._brush.brushing:  # but then put it back again so the rectangle is gone,
                    self.figure.interaction = self._brush

        self._brush.observe(update_brush, ["selected", "selected_x"])

        @vaex.jupyter.debounced(DEBOUNCE_SELECT)
        def update_brush(*args):
            with self.output:
                if not self._brush_interval.brushing:  # if we ended _brushing, reset it
                    self.figure.interaction = None
                if self._brush_interval.selected is not None and len(self._brush_interval.selected):
                    x1, x2 = self._brush_interval.selected
                    self.presenter.select_x_range(x1, x2)
                else:
                    self.presenter.select_nothing()
                if not self._brush_interval.brushing:  # but then put it back again so the rectangle is gone,
                    self.figure.interaction = self._brush_interval

        self._brush_interval.observe(update_brush, ["selected"])

        def tool_change(change=None):
            self.figure.interaction = self.interacts.get(self.tool, None)
        self.observe(tool_change, 'tool')
        self.widget = self.figure
def line_profile(
        image,
        order=2,
        plotter=None,  # noqa: C901
        comparisons=None,
        **viewer_kwargs):
    """View the image with a line profile.

    Creates and returns an ipywidget to visualize the image along with a line
    profile.

    The image can be 2D or 3D.

    Parameters
    ----------
    image : array_like, itk.Image, or vtk.vtkImageData
        The 2D or 3D image to visualize.

    order : int, optional
        Spline order for line profile interpolation. The order has to be in the
        range 0-5.

    plotter : 'plotly', 'bqplot', or 'ipympl', optional
        Plotting library to use. If not defined, use plotly if available,
        otherwise bqplot if available, otherwise ipympl.

    comparisons: dict, optional
        A dictionary whose keys are legend labels and whose values are other
        images whose intensities to plot over the same line.

    viewer_kwargs : optional
        Keyword arguments for the viewer. See help(itkwidgets.view).

    """

    profiler = LineProfiler(image=image, order=order, **viewer_kwargs)

    if not plotter:
        try:
            import plotly.graph_objs as go
            plotter = 'plotly'
        except ImportError:
            pass
    if not plotter:
        try:
            import bqplot
            plotter = 'bqplot'
        except ImportError:
            pass
    if not plotter:
        plotter = 'ipympl'

    if plotter == 'plotly':
        import plotly.graph_objs as go
        layout = go.Layout(xaxis=dict(title='Distance'),
                           yaxis=dict(title='Intensity'))
        fig = go.FigureWidget(layout=layout)
    elif plotter == 'bqplot':
        import bqplot
        x_scale = bqplot.LinearScale()
        y_scale = bqplot.LinearScale()
        x_axis = bqplot.Axis(scale=x_scale,
                             grid_lines='solid',
                             label='Distance')
        y_axis = bqplot.Axis(scale=y_scale,
                             orientation='vertical',
                             grid_lines='solid',
                             label='Intensity')
        labels = ['Reference']
        display_legend = False
        if comparisons:
            display_legend = True
            labels += [label for label in comparisons.keys()]
        lines = [
            bqplot.Lines(scales={
                'x': x_scale,
                'y': y_scale
            },
                         labels=labels,
                         display_legend=display_legend,
                         enable_hover=True)
        ]
        fig = bqplot.Figure(marks=lines, axes=[x_axis, y_axis])
    elif plotter == 'ipympl':
        ipython = IPython.get_ipython()
        ipython.enable_matplotlib('widget')

        matplotlib.interactive(False)

        fig, ax = plt.subplots()
    else:
        raise ValueError('Invalid plotter: ' + plotter)

    def update_plot():
        if plotter == 'plotly':
            distance, intensity = profiler.get_profile(image)
            fig.data[0]['x'] = distance
            fig.data[0]['y'] = intensity
            if comparisons:
                for ii, image_ in enumerate(comparisons.values()):
                    distance, intensity = profiler.get_profile(image_)
                    fig.data[ii + 1]['x'] = distance
                    fig.data[ii + 1]['y'] = intensity
        elif plotter == 'bqplot':
            distance, intensity = profiler.get_profile(image)
            if comparisons:
                for image_ in comparisons.values():
                    distance_, intensity_ = profiler.get_profile(image_)
                    distance = np.vstack((distance, distance_))
                    intensity = np.vstack((intensity, intensity_))
            fig.marks[0].x = distance
            fig.marks[0].y = intensity
        elif plotter == 'ipympl':
            ax.plot(*profiler.get_profile(image))
            if comparisons:
                ax.plot(*profiler.get_profile(image), label='Reference')
                for label, image_ in comparisons.items():
                    ax.plot(*profiler.get_profile(image_), label=label)
                ax.legend()
            else:
                ax.plot(*profiler.get_profile(image))

            ax.set_xlabel('Distance')
            ax.set_ylabel('Intensity')
            fig.canvas.draw()
            fig.canvas.flush_events()

    def update_profile(change):
        if plotter == 'plotly':
            update_plot()
        elif plotter == 'bqplot':
            update_plot()
        elif plotter == 'ipympl':
            is_interactive = matplotlib.is_interactive()
            matplotlib.interactive(False)
            ax.clear()
            update_plot()
            matplotlib.interactive(is_interactive)

    if plotter == 'plotly':
        distance, intensity = profiler.get_profile(image)
        trace = go.Scattergl(x=distance, y=intensity, name='Reference')
        fig.add_trace(trace)
        if comparisons:
            for label, image_ in comparisons.items():
                distance, intensity = profiler.get_profile(image_)
                trace = go.Scattergl(x=distance, y=intensity, name=label)
                fig.add_trace(trace)
        widget = widgets.VBox([profiler, fig])
    elif plotter == 'bqplot':
        update_plot()
        widget = widgets.VBox([profiler, fig])
    elif plotter == 'ipympl':
        update_plot()
        widget = widgets.VBox([profiler, fig.canvas])

    profiler.observe(update_profile, names=['point1', 'point2'])

    return widget
Exemple #9
0
shift_label2.layout.width = label_width
shift_label3.layout.width = label_width
shift_label4.layout.width = label_width

shift_label = widgets.HTML(value='Shift:&nbsp;')
shift_text_width = '50px'
shift_label.layout.width = shift_text_width

units = widgets.Label(value='nm')
unit_text_width = '30px'
units.layout.width = unit_text_width

## Create spectral lines display for both figures
# Axes for spectra
# Set the scales
x_sc_fig1 = bq.LinearScale(min=float(min(wavelengths)), max=float(max(wavelengths)))
y_sc_fig1 = bq.LinearScale()
x_sc = bq.LinearScale(min=float(min(wavelengths)), max=float(max(wavelengths)))
y_sc = bq.LinearScale()

# Define the axes
ax_x_fig1 = bq.Axis(label='Wavelength (nm)', scale=x_sc_fig1, grid_lines='none', num_ticks=12)
ax_y_fig1 = bq.Axis(scale=y_sc_fig1, orientation='vertical', grid_lines='none', num_ticks=0)
ax_x = bq.Axis(label='Wavelength (nm)', scale=x_sc, grid_lines='none', num_ticks=12)
ax_y = bq.Axis(scale=y_sc, orientation='vertical', grid_lines='none', num_ticks=0)

# Arrays for the spectral lines. one for the stars (0.5 to 1) and one for 
# laboratory measurements (0 to 0.5).
height1 = np.array([[0.0,0.5],[0.0,0.5],[0.0,0.5],[0.0,0.5],[0.0,0.5],[0.0,0.5],
                    [0.0,0.5],[0.0,0.5],[0.0,0.5],[0.0,0.5],[0.0,0.5],[0.0,0.5],[0.0,0.5],[0.0,0.5]])
height2 = np.array([[0.5,1.0],[0.5,1.0],[0.5,1.0],[0.5,1.0],[0.5,1.0],[0.5,1.0],
Exemple #10
0
    def __init__(self, session):
        super(BqplotBaseView, self).__init__(session)
        # session.hub.subscribe(self, SubsetCreateMessage,
        #                       handler=self.receive_message)
        self.state = self._state_cls()

        # if we allow padding, we sometimes get odd behaviour with the interacts
        self.scale_x = bqplot.LinearScale(min=0, max=1, allow_padding=False)
        self.scale_y = bqplot.LinearScale(min=0, max=1)
        self.scales = {'x': self.scale_x, 'y': self.scale_y}
        self.axis_x = bqplot.Axis(
            scale=self.scale_x, grid_lines='solid', label='x')
        self.axis_y = bqplot.Axis(scale=self.scale_y, orientation='vertical', tick_format='0.2f',
                                  grid_lines='solid', label='y')
        def update_axes(*ignore):
            self.axis_x.label = str(self.state.x_att)
            if self.is2d:
                self.axis_y.label = str(self.state.y_att)
        self.state.add_callback('x_att', update_axes)
        if self.is2d:
            self.state.add_callback('y_att', update_axes)
        self.figure = bqplot.Figure(scales=self.scales, animation_duration=0, axes=[
                                    self.axis_x, self.axis_y])
        self.figure.padding_y = 0
        
        actions = ['move']
        self.interact_map = {}
        self.panzoom = bqplot.PanZoom(scales={'x': [self.scale_x], 'y': [self.scale_y]})
        self.interact_map['move'] = self.panzoom

        if self.is2d:
            self.brush = bqplot.interacts.BrushSelector(x_scale=self.scale_x, y_scale=self.scale_y, color="green")
            self.interact_map['brush'] = self.brush
            self.brush.observe(self.update_brush, "brushing")
            actions.append('brush')

        self.brush_x = bqplot.interacts.BrushIntervalSelector(scale=self.scale_x, color="green" )
        self.interact_map['brush x'] = self.brush_x
        self.brush_x.observe(self.update_brush_x, "brushing")
        actions.append('brush x')

        if self.is2d:
            self.brush_y = bqplot.interacts.BrushIntervalSelector(scale=self.scale_y, color="green", orientation='vertical')
            self.interact_map['brush y'] = self.brush_y
            self.brush_y.observe(self.update_brush_y, "brushing")
            actions.append('brush y')


        self.button_action = widgets.ToggleButtons(description='Mode: ', options=[(action, action) for action in actions],
                                                   icons=["arrows", "pencil-square-o"])
        self.button_action.observe(self.change_action, "value")
        self.change_action()  # 'fire' manually for intial value

#         self.state.add_callback('y_att', self._update_axes)
#         self.state.add_callback('x_log', self._update_axes)
#         self.state.add_callback('y_log', self._update_axes)

        self.state.add_callback('x_min', self.limits_to_scales)
        self.state.add_callback('x_max', self.limits_to_scales)
        self.state.add_callback('y_min', self.limits_to_scales)
        self.state.add_callback('y_max', self.limits_to_scales)

        self.create_tab()
        self.output_widget = widgets.Output()
        self.main_widget = widgets.VBox(
            children=[self.tab, self.figure, self.output_widget])
def run_zonal_computation(aoi, output):

    aoi_model = aoi.view.model

    list_zones = get_ecozones()

    #get the aoi name
    aoi_name = aoi_model.name

    #create the result folder
    resultDir = os.path.join(os.path.expanduser('~'), 'zonal_results',
                             aoi_name, '')
    os.makedirs(resultDir, exist_ok=True)

    #create the map
    Map = sm.SepalMap(['CartoDB.Positron'])

    ###################################
    ###      placer sur la map     ####
    ###################################
    output.add_live_msg('visualize data')

    aoi = aoi_model.feature_collection
    Map.addLayer(aoi, {}, 'aoi')
    Map.zoom_ee_object(aoi.geometry())

    #add dataset
    dataset = ee.ImageCollection('NASA/MEASURES/GFCC/TC/v3').filter(
        ee.Filter.date('2010-01-01', '2010-12-31'))
    treeCanopyCover = dataset.select('tree_canopy_cover')
    treeCanopyCoverVis = {
        'min': 0.0,
        'max': 100.0,
        'palette': ['ffffff', 'afce56', '5f9c00', '0e6a00', '003800'],
    }
    country_gfcc_2010 = treeCanopyCover.mean().clip(aoi)
    Map.addLayer(country_gfcc_2010, treeCanopyCoverVis, 'Tree Canopy Cover')

    #load the ecozones
    gez_2010 = ee.Image('users/bornToBeAlive/gez_2010_wgs84')
    country_gez_2010 = gez_2010.select('b1').clip(aoi)

    Map.addLayer(country_gez_2010.sldStyle(getSldStyle()), {},
                 'gez 2010 raster')

    #show the 0 values
    cdl = country_gez_2010.select('b1')
    masked = ee.Image(cdl).updateMask(cdl.eq(0))
    Map.addLayer(masked, {'palette': 'red'}, 'masked 0 data')

    #mask these values from the results
    country_gez_2010 = ee.Image(cdl).updateMask(cdl.neq(0))

    #Get the list of values from the mosaic image
    freqHist = country_gez_2010.reduceRegions(
        **{
            'reducer': ee.Reducer.frequencyHistogram(),
            'collection': aoi,
            'scale': 100,
        })

    #rewrite the dictionary
    values = ee.List(
        freqHist.map(getVal).aggregate_array('vals')).flatten().distinct()

    #Run reduceToVectors per class by masking all other classes.
    classes = country_gez_2010.reduceToVectors(
        **{
            'reducer': ee.Reducer.countEvery(),
            'geometry': aoi,
            'scale': 100,
            'maxPixels': 1e10
        })
    country_gez_2010_vector = ee.FeatureCollection(classes)

    #add the borders on the map
    empty = ee.Image().byte()
    outline = empty.paint(**{
        'featureCollection': country_gez_2010_vector,
        'color': 1,
        'width': 1
    })
    Map.addLayer(outline, {'palette': '000000'}, 'gez 2010 borders')

    #export raw data
    out_dir = os.path.join(os.path.expanduser('~'), 'downloads')
    raw_stats = os.path.join(resultDir, aoi_name + '_raw.csv')

    if not os.path.isfile(raw_stats):
        output.add_live_msg('compute the zonal analysis')
        #compute the zonal analysis
        computation = False
        cpt = 0
        while not computation:
            f = io.StringIO()
            with redirect_stdout(f):
                geemap.zonal_statistics(in_value_raster=country_gfcc_2010,
                                        in_zone_vector=country_gez_2010_vector,
                                        out_file_path=raw_stats,
                                        statistics_type='FIXED_HIST',
                                        scale=100,
                                        crs=getConformProj(),
                                        hist_min=0,
                                        hist_max=100,
                                        hist_steps=100,
                                        tile_scale=2**cpt)
            output.add_live_msg(f.getvalue())

            #check if the computation have finished on a file
            if not os.path.isfile(raw_stats):
                #cannot use tile_scale > 16
                if cpt == 3:
                    raise Exception("Aoi is to big")

                #change the tile_scale and relaunch the process
                output.add_live_msg(f.getvalue(), 'error')
                time.sleep(2)
                cpt += 1
                output.add_live_msg(f'Increasing tile_scale ({2**cpt})',
                                    'warning')
                time.sleep(2)
            else:
                computation = True

    #######################################
    ###  lire et concatener les données ###
    #######################################

    out_stats = os.path.join(resultDir, aoi_name + '_stats.csv')

    if not os.path.isfile(out_stats):
        output.add_live_msg('read and concatenate the data')
        data = pd.read_csv(raw_stats, sep=',')

        output.add_live_msg('remove no_data')
        #enlever les lignes Nan
        #(vérifier dans le tableau d'avant qu'elles ne sont pas trop grandes)
        data = data.dropna()

        #recuperer les valeurs de label
        ecozones = data.label.unique()

        output.add_live_msg('merge')
        #aggreger les lignes avec les même valeurs
        stats = pd.DataFrame([i for i in range(100)], columns=['treecover'])
        stats = stats.set_index('treecover')

        for _, ecozoneId in enumerate(ecozones):
            patches = data[data.label == ecozoneId]
            ecozone = get_ecozones()[ecozoneId]
            stats[ecozone] = 0

            for index, patch in patches.iterrows():
                tmp = pd.DataFrame(
                    [val[1] for val in json.loads(patch['histogram'])])
                stats[ecozone] = stats[ecozone] + tmp[0]

        stats = stats * 100 * 100 / 1e6

        #exporter
        stats.to_csv(out_stats, index=True)

    #############################
    ##    create the layout    ##
    #############################
    output.add_live_msg('create the layout')

    stats = pd.read_csv(out_stats, index_col='treecover')

    #recuperer les noms de label
    ecozones = stats.columns

    x_sc = bq.LinearScale()
    ax_x = bq.Axis(label='treecover (%)', scale=x_sc)

    x = []
    for i in range(100):
        x.append(i)

    figs = []
    zones_id = []
    for ecozone in ecozones:

        #get the # of the ecozone
        for index, value in get_ecozones().items():
            if value == ecozone:
                zone_index = index
                zones_id.append(index)
                break

        y_sc = bq.LinearScale(max=stats[ecozone].max())
        ax_y = bq.Axis(label='surface (Km\u00B2)',
                       scale=y_sc,
                       orientation='vertical')  #, tick_format='.2e')
        y = []
        for index, row in stats.iterrows():
            y.append(row[ecozone])

        mark = bq.Bars(x=x,
                       y=y,
                       scales={
                           'x': x_sc,
                           'y': y_sc
                       },
                       colors=[get_colors()[zone_index]],
                       stroke='black',
                       padding=0.1)

        fig_hist = bq.Figure(title=ecozone, marks=[mark], axes=[ax_x, ax_y])

        figs.append(fig_hist)

    #add a coherent legend
    legend_keys = [get_ecozones()[i] for i in zones_id]
    legend_colors = [get_colors()[i] for i in zones_id]
    Map.add_legend(legend_keys=legend_keys,
                   legend_colors=legend_colors,
                   position='topleft')

    #create the partial layout
    children = [
        v.Flex(xs12=True,
               class_='pa-0',
               children=[sw.DownloadBtn('Download .csv', path=out_stats)]),
        v.Flex(xs12=True, class_='pa-0', children=[Map]),
        v.Flex(xs12=True, class_='pa-0', children=figs)
    ]

    output.add_live_msg('complete', 'success')

    return children
Exemple #12
0
        return "Tuples are not the same length!"
    else:
        summation = 0.0
        for i in range(n):
            summation += (x[i] - y[i])**2
        return np.sqrt(summation)


a = np.random.rand(100, 2)
b = np.random.rand(100, 2)

a = a * 700

b = b * 30

x_sc = bqplot.LinearScale()
y_sc = bqplot.LinearScale()

ax_x = bqplot.Axis(scale=x_sc, label="X-value")
ax_y = bqplot.Axis(scale=y_sc, label="Y-value", orientation='vertical')

scatters1 = bqplot.Scatter(x=a[:, 0],
                           y=a[:, 1],
                           scales={
                               'x': x_sc,
                               'y': y_sc
                           },
                           default_size=5,
                           colors=["navy"],
                           sizes=[10])
scatters2 = bqplot.Scatter(x=b[:, 0],
Exemple #13
0
    def __init__(self):
        self.episode_play = widgets.Play(
            value=0,
            min=0,
            max=100,
            step=1,
            description="Press play",
            disabled=False,
            interval=10,
        )
        self.update_interval_slider = widgets.IntSlider(
            min=1,
            max=300,
            value=30,
        )
        self.update_interval_box = widgets.HBox([
            widgets.Label("Animation update interval:"),
            self.update_interval_slider
        ])
        self.episode_slider = widgets.IntSlider()
        self.episode_hbox = widgets.HBox(
            [self.episode_play, self.episode_slider])
        self.episode_vbox = widgets.VBox(
            [self.episode_hbox, self.update_interval_box])

        widgets.jslink((self.episode_play, 'value'),
                       (self.episode_slider, 'value'))
        widgets.jslink((self.episode_play, 'max'),
                       (self.episode_slider, 'max'))
        widgets.jslink((self.episode_play, 'min'),
                       (self.episode_slider, 'min'))

        widgets.jslink((self.update_interval_slider, 'value'),
                       (self.episode_play, 'interval'))

        self.episode_gradient_image = widgets.Image(
            format='png',
            width=120,
            height=600,
        )
        self.episode_image = widgets.Image(
            format='png',
            width=120,
            height=600,
        )

        x_sc = bq.LinearScale()
        # x_sc.max = size * 1.3
        y_sc = bq.LinearScale()
        y_sc.max = 1
        y_sc2 = bq.LinearScale()

        self.x_sc = x_sc

        ax_x = bq.Axis(label='step', scale=x_sc, grid_lines='none')
        ax_x.min = 0
        ax_x.max = 100
        ax_y = bq.Axis(label='costs',
                       scale=y_sc,
                       orientation='vertical',
                       grid_lines='solid')
        ax_y2 = bq.Axis(label='speed',
                        scale=y_sc2,
                        orientation='vertical',
                        side='right',
                        grid_lines='none')

        self.costs_plot_lines_costs = Lines(scales={
            'x': x_sc,
            'y': y_sc
        },
                                            display_legend=True,
                                            stroke_width=1)
        self.costs_plot_lines_speed = Lines(scales={
            'x': x_sc,
            'y': y_sc2
        },
                                            colors=['red'],
                                            display_legend=True,
                                            stroke_width=1)
        self.costs_plot_progress = Lines(scales={'x': x_sc, 'y': y_sc})

        pan_zoom = bq.interacts.PanZoom(scales={'x': [x_sc], 'y': []})

        self.costs_plot_figure = Figure(marks=[
            self.costs_plot_lines_costs, self.costs_plot_lines_speed,
            self.costs_plot_progress
        ],
                                        axes=[ax_x, ax_y, ax_y2],
                                        title='Costs and speed',
                                        legend_location='top-left',
                                        interaction=pan_zoom,
                                        layout=widgets.Layout(width='100%',
                                                              height='100%'))

        self.follow_present = widgets.ToggleButton(
            value=False,
            description='Follow present',
            disabled=False,
            button_style='',  # 'success', 'info', 'warning', 'danger' or ''
        )

        self.reset_scale = widgets.Button(
            description='Reset scale',
            disabled=False,
            button_style='',  # 'success', 'info', 'warning', 'danger' or ''
        )

        def reset_scale_callback(b):
            self.reset_costs_plot_scale()

        self.reset_scale.on_click(reset_scale_callback)

        self.plot_controls_hbox = widgets.HBox(
            [self.follow_present, self.reset_scale],
            layout=widgets.Layout(width='100%', justify_content='center'))

        self.costs_plot_box = widgets.VBox(
            [self.costs_plot_figure, self.plot_controls_hbox],
            layout=widgets.Layout(width='100%'))

        self.images_hbox = widgets.HBox([
            self.episode_gradient_image, self.episode_image,
            self.costs_plot_box
        ],
                                        layout=widgets.Layout(width='100%'))

        def episode_slider_callback(change):
            if change.name == 'value' and change.new is not None:
                gradient_shift = max(
                    0,
                    len(self.images) - len(self.gradient_images))
                if change.new >= gradient_shift:
                    self.episode_gradient_image.value = self.gradient_images[
                        change.new - gradient_shift]
                self.episode_image.value = self.images[change.new]
                self.update_timestamp_line(change.new)

        self.episode_slider.observe(episode_slider_callback, type='change')

        super(EpisodeReview,
              self).__init__([self.episode_vbox, self.images_hbox])
Exemple #14
0
    def __init__(self):
        self.episode_play = widgets.Play(
            value=0,
            min=0,
            max=100,
            step=1,
            description="Press play",
            disabled=False,
            interval=10,
        )
        self.update_interval_slider = widgets.IntSlider(
            min=1, max=300, value=30,
        )
        self.update_interval_box = widgets.HBox(
            [
                widgets.Label("Animation update interval:"),
                self.update_interval_slider,
            ]
        )
        self.episode_slider = widgets.IntSlider()
        self.episode_hbox = widgets.HBox(
            [self.episode_play, self.episode_slider]
        )
        self.episode_vbox = widgets.VBox(
            [self.episode_hbox, self.update_interval_box]
        )

        widgets.jslink(
            (self.episode_play, "value"), (self.episode_slider, "value")
        )
        widgets.jslink(
            (self.episode_play, "max"), (self.episode_slider, "max")
        )
        widgets.jslink(
            (self.episode_play, "min"), (self.episode_slider, "min")
        )

        widgets.jslink(
            (self.update_interval_slider, "value"),
            (self.episode_play, "interval"),
        )

        self.episode_gradient_image = widgets.Image(
            format="png", width=120, height=600,
        )
        self.episode_image = widgets.Image(
            format="png", width=120, height=600,
        )

        x_sc = bq.LinearScale()
        # x_sc.max = size * 1.3
        y_sc = bq.LinearScale()
        y_sc.max = 1
        y_sc2 = bq.LinearScale()

        self.x_sc = x_sc

        ax_x = bq.Axis(label="step", scale=x_sc, grid_lines="none")
        ax_x.min = 0
        ax_x.max = 100
        ax_y = bq.Axis(
            label="costs",
            scale=y_sc,
            orientation="vertical",
            grid_lines="solid",
        )
        ax_y2 = bq.Axis(
            label="speed",
            scale=y_sc2,
            orientation="vertical",
            side="right",
            grid_lines="none",
        )

        self.costs_plot_lines_costs = Lines(
            scales={"x": x_sc, "y": y_sc}, display_legend=True, stroke_width=1
        )
        self.costs_plot_lines_speed = Lines(
            scales={"x": x_sc, "y": y_sc2},
            colors=["red"],
            display_legend=True,
            stroke_width=1,
        )
        self.costs_plot_progress = Lines(scales={"x": x_sc, "y": y_sc})

        pan_zoom = bq.interacts.PanZoom(scales={"x": [x_sc], "y": []})

        self.costs_plot_figure = Figure(
            marks=[
                self.costs_plot_lines_costs,
                self.costs_plot_lines_speed,
                self.costs_plot_progress,
            ],
            axes=[ax_x, ax_y, ax_y2],
            title="Costs and speed",
            legend_location="top-left",
            interaction=pan_zoom,
            layout=widgets.Layout(width="100%", height="100%"),
        )

        self.follow_present = widgets.ToggleButton(
            value=False,
            description="Follow present",
            disabled=False,
            button_style="",  # 'success', 'info', 'warning', 'danger' or ''
        )

        self.reset_scale = widgets.Button(
            description="Reset scale",
            disabled=False,
            button_style="",  # 'success', 'info', 'warning', 'danger' or ''
        )

        def reset_scale_callback(b):
            self.reset_costs_plot_scale()

        self.reset_scale.on_click(reset_scale_callback)

        self.plot_controls_hbox = widgets.HBox(
            [self.follow_present, self.reset_scale],
            layout=widgets.Layout(width="100%", justify_content="center"),
        )

        self.costs_plot_box = widgets.VBox(
            [self.costs_plot_figure, self.plot_controls_hbox],
            layout=widgets.Layout(width="100%"),
        )

        self.images_hbox = widgets.HBox(
            [
                self.episode_gradient_image,
                self.episode_image,
                self.costs_plot_box,
            ],
            layout=widgets.Layout(width="100%"),
        )

        def episode_slider_callback(change):
            if change.name == "value" and change.new is not None:
                gradient_shift = max(
                    0, len(self.images) - len(self.gradient_images)
                )
                if change.new >= gradient_shift:
                    if len(self.gradient_images) > change.new - gradient_shift:
                        self.episode_gradient_image.value = self.gradient_images[
                            change.new - gradient_shift
                        ]
                self.episode_image.value = self.images[change.new]
                self.update_timestamp_line(change.new)

        self.episode_slider.observe(episode_slider_callback, type="change")

        super(EpisodeReview, self).__init__(
            [self.episode_vbox, self.images_hbox]
        )
Exemple #15
0
 def get_scales(self):
     return {"x": bq.DateScale(), "y": bq.LinearScale()}
    def __init__(self, video_stream=None, img_path=None, img=None, interactors=None, size=0.5, vision_system=None, frame=None, \
            filter_fn=None, apply_filter_to_vision_system_input=False, update_frame_cbs=None, display_colorspace=ColorSpaces.BGR.value, **kwargs):

        if not (video_stream is not None) ^ (img is not None) ^ (
                frame is not None) ^ (img_path is not None):
            raise Exception(
                "either path, img or frame must be defined, and not both")

        self.bq_img = None
        self.raw_frame = None
        self.size = size or 1
        self.video_stream = video_stream
        self.togglebutton_group = []
        self.interactors = interactors or []
        self.vision_system = vision_system
        self.filter_fn = filter_fn
        self.apply_filter_to_vision_system_input = apply_filter_to_vision_system_input
        self.image_plot_scales = {'x': bq.LinearScale(), 'y': bq.LinearScale()}
        self.hidden = False

        if display_colorspace in ColorSpaces:
            self.display_colorspace = display_colorspace.value
        else:
            self.display_colorspace = display_colorspace

        self.update_frame_cbs = update_frame_cbs or []

        # read the data from a file to display
        if img is None:
            if frame is not None and type(frame is Frame):
                self.raw_frame = frame
            elif img_path is not None:
                self.raw_frame = Frame(cv2.imread(img_path))
            else:
                self.raw_frame = next(self.video_stream)
        else:
            self.raw_frame = Frame(img)

        self.filtered_frame = Frame.copy_of(self.raw_frame)
        self.labelled_frame = Frame.copy_of(self.filtered_frame)

        self.update_data_and_display()

        # link all required interactors
        for interactor in self.interactors:
            interactor.link_with(self)

        self.image_plot = self.make_image_plot()

        interactors_with_controls = [
            interactor for interactor in self.interactors
            if interactor.ipy_controls is not None
        ]
        panel_controls = [
            interactor.ipy_controls for interactor in interactors_with_controls
            if interactor.is_panel
        ]
        toolbar_controls = [
            interactor.ipy_controls for interactor in interactors_with_controls
            if not interactor.is_panel
        ]

        display_pane = ipy.VBox(
            [self.image_plot,
             self.make_image_tools(self.image_plot)] + toolbar_controls)
        display_pane.layout.width = str(
            self.size * self.FULL_EXTERNAL_WIDTH) + 'px'

        # fill accross 1/size times before filling downwards
        hbox_list = [display_pane]
        vbox_list = []

        for controls in (c for c in panel_controls if c is not None):
            hbox_list.append(controls)

            if len(hbox_list) == int(1 / size):
                vbox_list.append(ipy.HBox(hbox_list))
                hbox_list = []

        # add the remainder
        vbox_list += hbox_list

        super().__init__(vbox_list, **kwargs)
Exemple #17
0
                                     search_min_time, search_max_time)
dataset_id = datasets[0]
feature_layer = ipyl.GeoJSON(data=features)
feature_layer.on_click(map_click_handler)
map.layers = [map.layers[0], feature_layer]

# In[32]:

widget_dsnames = ipyw.Dropdown(options=datasets, value=dataset_id)

# This defines the intitial `bqplot` time series plot

# In[33]:

dt_x = bq.DateScale()
sc_y = bq.LinearScale()

constraints = {'time>=': search_min_time, 'time<=': search_max_time}

df, var = get_data(dataset=dataset_id,
                   standard_name=standard_name,
                   constraints=constraints)
def_tt = bq.Tooltip(fields=['y'], formats=['.2f'], labels=['value'])
time_series = bq.Lines(x=df.index,
                       y=df[var],
                       scales={
                           'x': dt_x,
                           'y': sc_y
                       },
                       tooltip=def_tt)
ax_x = bq.Axis(scale=dt_x, label='Time')
Exemple #18
0
    def create_widget(self, output, plot, dataset, limits):
        self.plot = plot
        self.output = output
        self.dataset = dataset
        self.limits = np.array(limits).tolist()

        def fix(v):
            # bqplot is picky about float and numpy scalars
            if hasattr(v, 'item'):
                return v.item()
            else:
                return v

        self.scale_x = bqplot.LinearScale(min=fix(limits[0][0]),
                                          max=fix(limits[0][1]),
                                          allow_padding=False)
        self.scale_y = bqplot.LinearScale(min=fix(limits[1][0]),
                                          max=fix(limits[1][1]),
                                          allow_padding=False)
        self.scales = {'x': self.scale_x, 'y': self.scale_y}

        self.figure = plt.figure(self.figure_key,
                                 fig=self.figure,
                                 scales=self.scales)
        self.figure.layout.width = 'calc(100% - 400px)'
        self.figure.layout.min_height = '800px'
        plt.figure(fig=self.figure)
        #self.figure.padding_y = 0
        x = np.arange(0, 10)
        y = x**2
        self.core_image = widgets.Image(format='png')
        self.core_image_fix = widgets.Image(format='png')

        self.image = bqplot.Image(scales=self.scales, image=self.core_image)
        # triggered by regular mouse click not a brush selector
        self.image.on_element_click(self.click_to_zoom)

        self.figure.marks = self.figure.marks + [self.image]
        self.scatter = s = plt.scatter(x,
                                       y,
                                       visible=False,
                                       rotation=x,
                                       scales=self.scales,
                                       size=x,
                                       marker="arrow")
        self.panzoom = bqplot.PanZoom(scales={
            'x': [self.scale_x],
            'y': [self.scale_y]
        })
        self.figure.interaction = self.panzoom
        for axes in self.figure.axes:
            axes.grid_lines = 'none'
            axes.color = axes.grid_color = axes.label_color = blackish
        self.figure.axes[0].label = plot.x_label
        self.figure.axes[1].label = plot.y_label
        self.figure.axes[1].scale = bqplot.LinearScale(min=0,
                                                       max=self.scale_y.max -
                                                       self.scale_y.min,
                                                       allow_padding=False)
        self.stuck_ctr = 0

        self.base_address = widgets.Label(
            value=f"Base address: 0x{int(self.limits[1][0]):X}")
        self.zoom_args = widgets.Text(description="Zoom Args",
                                      value=self.zoom_args_string(),
                                      disabled=True,
                                      style={'description_width': 'initial'},
                                      layout=widgets.Layout(width='50%'))

        self.curr_action = Action.other
        self.undo_actions = list()
        self.redo_actions = list()
        self.counter = 2
        self.scale_x.observe(self._update_limits)
        self.scale_y.observe(self._update_limits)
        self.widget = widgets.VBox(
            [self.figure, self.base_address, self.zoom_args])
        self.create_tools()
Exemple #19
0
def test_astro_image():
    data = np.zeros((2, 3))
    color_scale = bqplot.ColorScale()
    scale_x = bqplot.LinearScale()
    scale_y = bqplot.LinearScale()
    ImageGL(image=data, scales={'image': color_scale, 'x': scale_x, 'y': scale_y})
Exemple #20
0
    def __init__(self, view, viewer_state, layer_state=None, layer=None):

        super(BqplotScatterLayerArtist, self).__init__(viewer_state,
                                                       layer_state=layer_state,
                                                       layer=layer)

        self.view = view

        self.scale_size = bqplot.LinearScale()
        self.scale_color = bqplot.ColorScale()
        self.scale_size_quiver = bqplot.LinearScale(min=0, max=1)
        self.scale_rotation = bqplot.LinearScale(min=0, max=1)
        self.scales = dict(self.view.scales,
                           size=self.scale_size,
                           rotation=self.scale_rotation,
                           color=self.scale_color)
        self.scale_image = bqplot.ColorScale()
        self.scales_quiver = dict(self.view.scales,
                                  size=self.scale_size_quiver,
                                  rotation=self.scale_rotation)
        self.scales_image = dict(self.view.scales, image=self.scale_image)
        self.scatter = bqplot.ScatterGL(scales=self.scales, x=[0, 1], y=[0, 1])
        self.quiver = bqplot.ScatterGL(scales=self.scales_quiver,
                                       x=[0, 1],
                                       y=[0, 1],
                                       visible=False,
                                       marker='arrow')

        self.counts = None
        self.image = AstroImage(scales=self.scales_image)
        on_change([(self.state, 'density_map')])(self._on_change_density_map)
        on_change([(self.state, 'bins')])(self._update_scatter)
        self._viewer_state.add_global_callback(self._update_scatter)

        self.view.figure.marks = list(
            self.view.figure.marks) + [self.image, self.scatter, self.quiver]
        dlink((self.state, 'color'), (self.scatter, 'colors'), lambda x: [x])
        dlink((self.state, 'color'), (self.quiver, 'colors'), lambda x: [x])
        self.scatter.observe(self._workaround_unselected_style, 'colors')
        self.quiver.observe(self._workaround_unselected_style, 'colors')

        on_change([(self.state, 'cmap_mode', 'cmap_att')
                   ])(self._on_change_cmap_mode_or_att)
        on_change([(self.state, 'cmap')])(self._on_change_cmap)
        dlink((self.state, 'cmap_vmin'), (self.scale_color, 'min'),
              float_or_none)
        dlink((self.state, 'cmap_vmax'), (self.scale_color, 'max'),
              float_or_none)

        on_change([(self.state, 'size', 'size_scaling', 'size_mode',
                    'size_vmin', 'size_vmax')])(self._update_size)

        viewer_state.add_callback('x_att', self._update_xy_att)
        viewer_state.add_callback('y_att', self._update_xy_att)
        self._update_size()
        # set initial values for the colormap
        self._on_change_cmap()

        self.state.add_callback('visible', self._update_visibility)
        self.state.add_callback('vector_visible', self._update_visibility)
        self.state.add_callback('density_map', self._update_visibility)

        dlink((self.state, 'visible'), (self.scatter, 'visible'))
        dlink((self.state, 'visible'), (self.image, 'visible'))

        dlink((self.state, 'alpha'), (self.scatter, 'default_opacities'),
              lambda x: [x])
        dlink((self.state, 'alpha'), (self.quiver, 'default_opacities'),
              lambda x: [x])
        dlink((self.state, 'alpha'), (self.image, 'opacity'))

        on_change([(self.state, 'vector_visible', 'vx_att', 'vy_att')
                   ])(self._update_quiver)
        dlink((self.state, 'vector_visible'), (self.quiver, 'visible'))
Exemple #21
0
    def __init__(self,
                 y,
                 x=None,
                 xlim=None,
                 ylim=None,
                 xlabel=None,
                 ylabel=None,
                 title=None):
        super().__init__()

        self.x = x
        self.xlim = xlim or [0, 1]
        self.ylim = ylim or [0, 1]
        self.xlabel = xlabel or 'x'
        self.ylabel = ylabel or 'y'
        self.title = title or "{} vs {}".format(self.ylabel, self.xlabel)

        if isinstance(y, list):
            self.y = y
        else:
            self.y = [y]

        self.colors = [
            'blue', 'red', 'green', 'orange', 'black', 'purple', 'gray'
        ]
        self.xscale = bq.LinearScale(min=self.xlim[0], max=self.xlim[1])
        self.yscale = bq.LinearScale(min=self.ylim[0], max=self.ylim[1])

        if isinstance(self.ylabel, list):
            ylabel = ''
        else:
            ylabel = self.ylabel

        self.xax = bq.Axis(
            scale=self.xscale,
            label=self.xlabel,
            grid_lines='none',
        )
        self.yax = bq.Axis(
            scale=self.yscale,
            label=ylabel,
            orientation='vertical',
            grid_lines='none',
        )
        self.num_lines = 0
        self.lines = []
        self.scatters = []
        self.labels = []

        if isinstance(self.y, list):
            for y in self.y:
                self.create_line(y, display_legend=True)
        else:
            self.create_line(self.y)

        self.fig = bq.Figure(marks=self.lines + self.scatters,
                             axes=[self.xax, self.yax],
                             layout=ipw.Layout(height='550px', width='100%'),
                             title=self.title)
        self.debug = ipw.Output(
            layout=ipw.Layout(height='100px', overflow_y='scroll'))
        self.children = [self.fig]
            ])
        ]), header_rel_html
    ]  #, VBox(list_relative_controls), VBox([floattext_confidence])]


def updatecontrolinui():
    UI_model.children[0].children = UI_viewcontrol


updateviewcontrol()

# END OF ************************************************************************************* GUI portion of the code that contains various labels,checkboxes etc.  ***********************
# START OF ************************************************************************************************************** Build bar charts (use of bqp)  ***********************

x_ord = bqp.OrdinalScale()
y_sc = bqp.LinearScale()
y_sc.max = 1

#Plot #1 i.e. Creation of the bar plot

bar = bqp.Bars(
    x=[],
    y=[],
    scales={
        'x': x_ord,
        'y': y_sc
    },
    orientation="horizontal",
    display_legend=True,
    labels=[
        'Initial Weights', 'Mkt Efficient Portfolio (Max Sharpe)',
Exemple #23
0
def metrics(id,
            y_true,
            y_pred,
            protected,
            desc,
            x_ticks,
            pname,
            backend="bqplot"):
    """Plot a stack of figures for the three metrics."""
    figure_name = f"Fairness_{id}_{desc}"

    dis = protected.astype(bool)
    tn_adv, fp_adv, fn_adv, tp_adv = confusion_matrix(y_true[~dis],
                                                      y_pred[~dis]).ravel()

    tn_dis, fp_dis, fn_dis, tp_dis = confusion_matrix(y_true[dis],
                                                      y_pred[dis]).ravel()

    # Also just count all positives and negatives
    pos_adv = tp_adv + fp_adv  # all positives
    pos_dis = tp_dis + fp_dis
    neg_adv = tn_adv + fn_adv  # all negatives
    neg_dis = tn_dis + fn_dis

    # Demographic Parity
    dem_par = (pos_dis / (pos_dis + neg_dis)) / (pos_adv / (pos_adv + neg_adv))

    # Equal Opportunity
    eq_opp = (tp_dis / (tp_dis + fn_dis)) / (tp_adv / (tp_adv + fn_adv))

    acc_adv = np.mean(y_true[~dis] == y_pred[~dis])
    acc_dis = np.mean(y_true[dis] == y_pred[dis])
    print("Acc adv: {}".format(acc_adv))
    print("Acc dis: {}".format(acc_dis))

    def normalise(nd):
        numer, denom = nd
        numer = np.asarray(numer, float)
        denom = np.asarray(denom, float)
        total = numer + denom
        total /= 100.  # make percentage
        numer = numer / total
        denom = denom / total
        return [numer, denom]

    def describe(rate):
        rate = np.round(rate, 2)  # make a percent

        if rate < 0.5:
            return f"{rate:.0%} as often"
        elif rate == 0.5:
            return "half as often"
        elif rate < 1.:
            return f"{1. - rate:.0%} less often"
        elif rate == 1.:
            return "equally often"
        elif rate < 2.:
            return f"{rate - 1.:.0%} more often"
        elif rate == 2.:
            return "twice as often"
        elif rate < 4:
            return f"{rate:.1f}x as often"
        elif rate < 10:
            return f"{rate:.0f}x as often"
        else:
            return "significantly more often"
        # if eo > 0.995:
        #     stake = f"Suitable {groups[0].capitalize()} and {groups[1]} are selected at equal rates."

    def bqbars(values, colors, title, stake):
        fig = plt.figure()
        eo_bars = plt.bar(x_ticks, values, colors=colors)
        plt.ylabel("Fraction (%)")
        plt.xlabel(stake)
        fig.title = title
        return fig

    def matplotbars(ax, values, colors, title, stake):
        b0 = ax.bar(x_ticks, values[0], width=0.5, color=colors[0])
        b1 = ax.bar(x_ticks,
                    values[1],
                    bottom=values[0],
                    width=0.5,
                    color=colors[1])
        ax.set_ylabel("Fraction (%)")
        ax.set_xlabel(stake)
        ax.set_title(title)
        return b0, b1

    eov = normalise([[tp_adv, tp_dis], [fn_adv, fn_dis]])
    eov_title = "Opportunity{}".format(desc)
    eov_stake = f"Suitable {pname} are selected {describe(eq_opp)}."
    eov_colors = ["green", "lightgreen"]

    dpv = normalise([[pos_adv, pos_dis], [neg_adv, neg_dis]])
    dpv_title = "Selection Rates{}".format(desc)
    dpv_stake = f"{pname} are selected {describe(dem_par)}."
    dpv_colors = ["black", "gray"]

    ppv = normalise([[tp_adv, tp_dis], [fp_adv, fp_dis]])
    # error rates (false Discovery Rate parity)
    prec_par = (tp_dis / pos_dis) / (tp_adv / pos_adv)
    ppv_title = "Precision{}".format(desc)
    #ppv_stake = f"{pname.capitalize()} are selected {describe(prec_par)}.".replace("often", "precisely")
    ppv_stake = f"Selected {pname} are profitable {describe(prec_par)}."
    ppv_colors = ["green", "red"]

    print("Selection: {}".format(dpv))
    print("Opportunity: {}".format(eov))
    print("Precision: {}".format(ppv))
    if backend == "bqplot":
        scales = {
            "x": bq.OrdinalScale(),
            "y": bq.LinearScale(),
        }

        eo_fig = bqbars(eov, eov_colors, eov_title, eov_stake)
        dp_fig = bqbars(dpv, dpv_colors, dpv_title, dpv_stake)
        pp_fig = bqbars(ppv, ppv_colors, ppv_title, ppv_stake)

        fairbox = widgets.HBox((
            dp_fig,
            eo_fig,
            pp_fig,
        ))
        fairbox.layout.width = "99%"
        display(fairbox)
        eo_fig.axes[0].color = eo_fig.axes[1].color = "Black"
        dp_fig.axes[0].color = dp_fig.axes[1].color = "Black"
        pp_fig.axes[0].color = pp_fig.axes[1].color = "Black"

    else:
        fig, ax = mplt.subplots(1, 3, figsize=(9, 3), dpi=DPI)

        def clean(strv):
            # We have a bit more room for better sentences
            val = strv.replace("SE Asian",
                               "SE Asians").replace("x ", " times ")
            # split the line
            s = len(val) // 2
            a = val[s:].find(' ')
            b = val[:s][::-1].find(' ')
            split = s + a
            if (b >= 0 and b < a) or (a < 0):
                split = s - b - 1
            val = val[:split] + "\n" + val[split + 1:]
            return val

        sa, na = matplotbars(ax[0], dpv, dpv_colors, dpv_title,
                             clean(dpv_stake))
        es, en = matplotbars(ax[1], eov, eov_colors, eov_title,
                             clean(eov_stake))
        _, ii = matplotbars(ax[2], ppv, ppv_colors, ppv_title,
                            clean(ppv_stake))
        mplt.legend(
            (es, en, sa, na, ii),
            (
                "Profitable (Selected)",
                "Profitable (Rejected)",
                "Selected (All)",
                "Rejected (All)",
                "Not Profitable (Selected)",
            ),
            bbox_to_anchor=(1.25, 0.75),
        )
        mplt.tight_layout()
        fig.savefig("images/" + figure_name + ".png", bbox_inches="tight")
        


##
## Set up counts versus time plot
##

# Set up Species to be Generic
init_species_ind = 0  

# Set up initial time
init_time = 0
init_time_idx = 0

# Set up axes
x_time = bq.LinearScale(min = 0, max=max_half_lifes)
y_number = bq.LinearScale(min = 0, max=N_parent)
y_fraction = bq.LinearScale(min = 0, max=1)

# Labels and scales for Axes
ax_x_time = bq.Axis(label=species['timeunits'][init_species_ind], scale=x_time, num_ticks = time_ticks,
                    tick_values = half_life_ticks )
ax_y_number = bq.Axis(label='Number of atoms', scale=y_number, orientation='vertical')
ax_y_fraction = bq.Axis(label='Fraction of atoms', scale=y_fraction, orientation='vertical')

# Define tooltip (Bug: doesn't allow relabeling Tooltips, also needs to apply to Scatter, not Lines)
#def_tt_parent = bq.Tooltip(fields=['x', 'y'], formats=['.2f', '3.0f'], labels=['time', species['parent_short'][init_species_ind]])
#def_tt_daughter = bq.Tooltip(fields=['x', 'y'], formats=['.2f', '3.0f'], labels=['time', species['daughter_short'][init_species_ind]])
def_tt_parent = bq.Tooltip(fields=['x', 'y'], formats=['.2f', '.3f'], labels=['time', 'amount of parent isotope'])
def_tt_daughter = bq.Tooltip(fields=['x', 'y'], formats=['.2f', '.3f'], labels=['time', 'amount of daughter isotope'])
Exemple #25
0
def interactive_body(body,
                     angles,
                     bodies,
                     r_arm=True,
                     l_arm=True,
                     r_leg=True,
                     l_leg=True,
                     neck_p=True):
    """Plot an interactive body with which we can play. Only call this function on a complete body (for now)"""
    def refresh(_):
        i = 0
        to_update = []
        if l_arm:
            left_arm.x, left_arm.y = [[p[2][0], scat.x[i], scat.x[i + 1]],
                                      [p[2][1], scat.y[i], scat.y[i + 1]]]
            to_update.append(3)
            to_update.append(4)
            i += 2
        if r_arm:
            right_arm.x, right_arm.y = [[p[6][0], scat.x[i], scat.x[i + 1]],
                                        [p[6][1], scat.y[i], scat.y[i + 1]]]
            to_update.append(7)
            to_update.append(8)
            i += 2
        if l_leg:
            left_leg.x, left_leg.y = [[p[9][0], scat.x[i], scat.x[i + 1]],
                                      [p[9][1], scat.y[i], scat.y[i + 1]]]
            to_update.append(10)
            to_update.append(11)
            i += 2
        if r_leg:
            right_leg.x, right_leg.y = [[p[13][0], scat.x[i], scat.x[i + 1]],
                                        [p[13][1], scat.y[i], scat.y[i + 1]]]
            to_update.append(14)
            to_update.append(15)
            i += 2
        if neck_p:
            to_update.append(0)
            neck.x, neck.y = [[p[1][0], scat.x[i]], [p[1][1], scat.y[i]]]

        head.x = np.cos(np.linspace(0, 2 * np.pi, 100)) * 60 + neck.x[1]
        head.y = np.sin(np.linspace(0, 2 * np.pi, 100)) * 65 + neck.y[1]

        #update body
        for i in range(len(to_update)):
            body[0][to_update[i]] = [scat.x[i], scat.y[i]]

        body_angles = []
        b_angles = member_relative_angles(body)

        if l_arm:
            body_angles.append(b_angles[0])
            body_angles.append(b_angles[1])
        if r_arm:
            body_angles.append(b_angles[2])
            body_angles.append(b_angles[3])
        if l_leg:
            body_angles.append(b_angles[4])
            body_angles.append(b_angles[5])
        if r_leg:
            body_angles.append(b_angles[6])
            body_angles.append(b_angles[7])
        if neck_p:
            body_angles.append(b_angles[8])

        b = get_nearest_neighbor(np.transpose(all_angles),
                                 np.transpose(body_angles), bodies)[0]
        p2 = b[0]
        nchest.x, nchest.y = np.transpose(
            [p2[1], p2[2], p2[9], p2[13], p2[6], p2[1], p2[0]])

        nhead.x = np.cos(np.linspace(0, 2 * np.pi, 100)) * 60 + p2[0][0]
        nhead.y = np.sin(np.linspace(0, 2 * np.pi, 100)) * 65 + p2[0][1]

        nleft_arm.x, nleft_arm.y = [[p2[2][0], p2[3][0], p2[4][0]],
                                    [p2[2][1], p2[3][1], p2[4][1]]]
        nright_arm.x, nright_arm.y = [[p2[6][0], p2[7][0], p2[8][0]],
                                      [p2[6][1], p2[7][1], p2[8][1]]]
        nleft_leg.x, nleft_leg.y = [[p2[9][0], p2[10][0], p2[11][0]],
                                    [p2[9][1], p2[10][1], p2[11][1]]]
        nright_leg.x, nright_leg.y = [[p2[13][0], p2[14][0], p2[15][0]],
                                      [p2[13][1], p2[14][1], p2[15][1]]]

    scales = {
        'x': bqp.LinearScale(min=0, max=1000),
        'y': bqp.LinearScale(min=1000, max=0)
    }

    #initialization of the nearest neighbor of the interactive body.
    body_angles = []
    all_angles = []
    b_angles = member_relative_angles(body)
    a = np.transpose(angles)

    if l_arm:
        body_angles.append(b_angles[0])
        body_angles.append(b_angles[1])
        all_angles.append(a[0])
        all_angles.append(a[1])
    if r_arm:
        body_angles.append(b_angles[2])
        body_angles.append(b_angles[3])
        all_angles.append(a[2])
        all_angles.append(a[3])
    if l_leg:
        body_angles.append(b_angles[4])
        body_angles.append(b_angles[5])
        all_angles.append(a[4])
        all_angles.append(a[5])
    if r_leg:
        body_angles.append(b_angles[6])
        body_angles.append(b_angles[7])
        all_angles.append(a[6])
        all_angles.append(a[7])
    if neck_p:
        body_angles.append(b_angles[8])
        all_angles.append(a[8])

    nbody = get_nearest_neighbor(np.transpose(all_angles),
                                 np.transpose(body_angles), bodies)[0]

    #points of the interactive and neighbor bodies
    p = body[0]
    p2 = nbody[0]

    marks = []
    #Constructions of the two bodies: the interactive one and its nearest neighbor. body part beginnig with n... are part of the neighbor.
    #draw the chest
    chest = bqp.Lines(scales=scales)
    chest.x, chest.y = np.transpose([p[1], p[2], p[9], p[13], p[6], p[1]])
    marks.append(chest)

    nchest = bqp.Lines(scales=scales, colors=['red'])
    nchest.x, nchest.y = np.transpose(
        [p2[1], p2[2], p2[9], p2[13], p2[6], p2[1], p2[0]])
    marks.append(nchest)

    #draw the head
    head = bqp.Lines(scales=scales)
    head.x = np.cos(np.linspace(0, 2 * np.pi, 100)) * 60 + p[0][0]
    head.y = np.sin(np.linspace(0, 2 * np.pi, 100)) * 65 + p[0][1]
    marks.append(head)

    nhead_x = np.cos(np.linspace(0, 2 * np.pi, 100)) * 60 + p2[0][0]
    nhead_y = np.sin(np.linspace(0, 2 * np.pi, 100)) * 65 + p2[0][1]
    nhead = bqp.Lines(x=nhead_x, y=nhead_y, scales=scales, colors=['red'])
    marks.append(nhead)

    to_keep = list()
    if l_arm:
        to_keep.append(p[3])
        to_keep.append(p[4])
    if r_arm:
        to_keep.append(p[7])
        to_keep.append(p[8])
    if l_leg:
        to_keep.append(p[10])
        to_keep.append(p[11])
    if r_leg:
        to_keep.append(p[14])
        to_keep.append(p[15])
    if neck_p:
        to_keep.append(p[0])
    #movable points: arms first, left side first
    scat = bqp.Scatter(scales=scales,
                       enable_move=True,
                       update_on_move=True,
                       stroke_width=7)
    scat.x, scat.y = np.transpose(to_keep)
    marks.append(scat)

    i = 0
    left_arm = bqp.Lines(scales=scales)
    if l_arm:
        left_arm.x, left_arm.y = [[p[2][0], scat.x[i], scat.x[i + 1]],
                                  [p[2][1], scat.y[i], scat.y[i + 1]]]
        i += 2
    else:
        left_arm.x, left_arm.y = [[p[2][0], p[3][0], p[4][0]],
                                  [p[2][1], p[3][1], p[4][1]]]
    marks.append(left_arm)

    nleft_arm = bqp.Lines(scales=scales, colors=['red'])
    nleft_arm.x, nleft_arm.y = [[p2[2][0], p2[3][0], p2[4][0]],
                                [p2[2][1], p2[3][1], p2[4][1]]]
    marks.append(nleft_arm)

    right_arm = bqp.Lines(scales=scales)
    if r_arm:
        right_arm.x, right_arm.y = [[p[6][0], scat.x[i], scat.x[i + 1]],
                                    [p[6][1], scat.y[i], scat.y[i + 1]]]
        i += 2
    else:
        right_arm.x, right_arm.y = [[p[6][0], p[7][0], p[8][0]],
                                    [p[6][1], p[7][1], p[8][1]]]

    marks.append(right_arm)

    nright_arm = bqp.Lines(scales=scales, colors=['red'])
    nright_arm.x, nright_arm.y = [[p2[6][0], p2[7][0], p2[8][0]],
                                  [p2[6][1], p2[7][1], p2[8][1]]]
    marks.append(nright_arm)

    left_leg = bqp.Lines(scales=scales)
    if l_leg:
        left_leg.x, left_leg.y = [[p[9][0], scat.x[i], scat.x[i + 1]],
                                  [p[9][1], scat.y[i], scat.y[i + 1]]]
        i += 2
    else:
        left_leg.x, left_leg.y = [[p[9][0], p[10][0], p[11][0]],
                                  [p[9][1], p[10][1], p[11][1]]]
    marks.append(left_leg)

    nleft_leg = bqp.Lines(scales=scales, colors=['red'])
    nleft_leg.x, nleft_leg.y = [[p2[9][0], p2[10][0], p2[11][0]],
                                [p2[9][1], p2[10][1], p2[11][1]]]
    marks.append(nleft_leg)

    right_leg = bqp.Lines(scales=scales)
    if r_leg:
        right_leg.x, right_leg.y = [[p[13][0], scat.x[i], scat.x[i + 1]],
                                    [p[13][1], scat.y[i], scat.y[i + 1]]]
        i += 2
    else:
        right_leg.x, right_leg.y = [[p[13][0], p[14][0], p[15][0]],
                                    [p[13][1], p[14][1], p[15][1]]]
    marks.append(right_leg)

    nright_leg = bqp.Lines(scales=scales, colors=['red'])
    nright_leg.x, nright_leg.y = [[p2[13][0], p2[14][0], p2[15][0]],
                                  [p2[13][1], p2[14][1], p2[15][1]]]
    marks.append(nright_leg)

    neck = bqp.Lines(scales=scales)
    if neck_p:
        neck.x, neck.y = [[p[1][0], scat.x[i]], [p[1][1], scat.y[i]]]
        i += 1
    else:
        neck.x, neck.y = [[p[1][0], p[0][0]], [p[1][1], p[0][1]]]
    marks.append(neck)
    scat.observe(refresh, names=['x', 'y'])

    return bqp.Figure(marks=marks, padding_y=0., min_height=750, min_width=750)
Exemple #26
0
    def _init_grid(self):

        # Set up scales for the spatial x, spatial y, spectral, and flux
        self.scale_x = bqplot.LinearScale(min=0, max=1)
        self.scale_y = bqplot.LinearScale(min=0, max=1)
        self.scale_spec = bqplot.LinearScale(min=0, max=1)
        self.scale_flux = bqplot.LinearScale(min=0, max=1)

        # Set up colorscale
        self.scale_cutout_image = ColorScale(colors=['black', 'white'])
        self.scale_spec2d_image = ColorScale(colors=['black', 'white'])

        # Set up axes
        self.axis_x = bqplot.Axis(scale=self.scale_x,
                                  grid_lines='solid',
                                  label='x')
        self.axis_y = bqplot.Axis(scale=self.scale_y,
                                  grid_lines='solid',
                                  label='y',
                                  orientation='vertical')

        self.axis_spec = bqplot.Axis(scale=self.scale_spec,
                                     grid_lines='solid',
                                     label='spec')
        self.axis_flux = bqplot.Axis(scale=self.scale_flux,
                                     grid_lines='solid',
                                     label='flux',
                                     orientation='vertical')

        # Set up bqplot viewers
        # =====================

        # Cutout
        # ------
        self.fig_cutout = bqplot.Figure(scales={
            'x': self.scale_x,
            'y': self.scale_y
        },
                                        axes=[self.axis_x, self.axis_y],
                                        layout={
                                            'width': '500px',
                                            'height': '400px'
                                        })
        self.fig_cutout.interaction = PanZoom(scales={
            'x': [self.scale_x],
            'y': [self.scale_y]
        })

        # Spec 2d
        # -------
        self.fig_spec2d = bqplot.Figure(scales={
            'x': self.scale_spec,
            'y': self.scale_y
        },
                                        axes=[self.axis_spec, self.axis_y],
                                        layout={
                                            'width': '500px',
                                            'height': '400px'
                                        })

        self.fig_spec2d.interaction = PanZoom(scales={
            'x': [self.scale_spec],
            'y': [self.scale_y]
        })

        # Spec 1d
        # -------
        self.fig_spec1d = bqplot.Figure(scales={
            'x': self.scale_spec,
            'y': self.scale_flux
        },
                                        axes=[self.axis_spec, self.axis_flux],
                                        layout={
                                            'width': '500px',
                                            'height': '400px'
                                        })

        self.fig_spec1d.interaction = PanZoom(scales={
            'x': [self.scale_spec],
            'y': [self.scale_flux]
        })

        # info box
        # --------
        self.info_box = Textarea(value='Hello World')
        self.info_box.layout.height = '100%'
        self.info_box.layout.width = '100%'
        self.info_box.layout.align_self = 'flex-end'

        # Set up content of figures
        # =========================

        self.cutout_mark = AstroImage(scales={
            'x': self.scale_x,
            'y': self.scale_y,
            'image': self.scale_cutout_image
        })
        self.fig_cutout.marks = [self.cutout_mark]

        self.spec2d_mark = AstroImage(
            scales={
                'x': self.scale_spec,
                'y': self.scale_y,
                'image': self.scale_spec2d_image
            })
        self.fig_spec2d.marks = [self.spec2d_mark]

        self.spec1d_mark = bqplot.Lines(scales={
            'x': self.scale_spec,
            'y': self.scale_flux
        },
                                        x=[],
                                        y=[])
        self.fig_spec1d.marks = [self.spec1d_mark]

        GridBox.__init__(self, [
            self.fig_cutout,
            HTML(), self.fig_spec2d,
            HTML(),
            HTML(),
            HTML(), self.info_box,
            HTML(), self.fig_spec1d
        ],
                         layout=Layout(width='100%',
                                       grid_template_columns='35% 5% 35%',
                                       grid_template_rows='30% 5% 30%',
                                       grid_gap='30px 30px'))

        self.layout.justify_content = "center"
        self.layout.align_items = "center"
Exemple #27
0
def square_orbit(x0, y0):
    sc_x = bqplot.LinearScale(min=-1.2, max=1.2)
    sc_y = bqplot.LinearScale(min=-1.2, max=1.2)

    z0 = x0 + y0 * 1j

    z_point = bqplot.Scatter(x=[z0.real],
                             y=[z0.imag],
                             scales={
                                 'x': sc_x,
                                 'y': sc_y
                             },
                             colors=['green'],
                             enable_move=True,
                             default_size=200)
    z_point.update_on_move = True

    z_label = bqplot.Label(x=[z0.real + .05],
                           y=[z0.imag + .05],
                           scales={
                               'x': sc_x,
                               'y': sc_y
                           },
                           colors=['green'],
                           text=['z0'],
                           default_size=26,
                           font_weight='bolder')

    scatt = bqplot.Scatter(x=[],
                           y=[],
                           scales={
                               'x': sc_x,
                               'y': sc_y
                           },
                           colors=['black'],
                           default_size=20)

    theta = np.linspace(0, 2. * np.pi, 1000)
    x = np.cos(theta)
    y = np.sin(theta)
    #circle = bqplot.Lines(x=x, y=y, scales={'x': sc_x, 'y': sc_y}, colors=['black'])
    lin = bqplot.Lines(x=[],
                       y=[],
                       scales={
                           'x': sc_x,
                           'y': sc_y
                       },
                       colors=['black'],
                       stroke_width=1)

    def update_line(change=None):
        out = orbit(z_point.x + 1j * z_point.y)
        z_label.x = z_point.x + 0.05
        z_label.y = z_point.y + 0.05
        lin.x = out.real
        lin.y = out.imag
        scatt.x = out.real.flatten()
        scatt.y = out.imag.flatten()

    update_line()
    # update line on change of x or y of scatter
    z_point.observe(update_line, names=['x'])
    z_point.observe(update_line, names=['y'])
    ax_x = bqplot.Axis(scale=sc_x, offset=dict(value=0.5), grid_lines='none')
    ax_y = bqplot.Axis(scale=sc_y,
                       orientation='vertical',
                       offset=dict(value=0.5),
                       grid_lines='none')

    #fig = bqplot.Figure(marks=[scatt, lin, circle, z_point, z_label], axes=[ax_x, ax_y],
    #min_aspect_ratio=1, max_aspect_ratio=1)
    fig = bqplot.Figure(marks=[scatt, lin, z_point, z_label],
                        axes=[ax_x, ax_y],
                        min_aspect_ratio=1,
                        max_aspect_ratio=1)
    fig.layout.height = '800px'
    return fig
Exemple #28
0
    def __init__(self, rope):

        super().__init__()

        title = ipw.HTML("<h3>Rope Parameters</h3>")

        self.rope = rope
        grid = rope.grid

        z_quants = ['water_speeds', 'water_angles', 'frond_lengths', 'frond_stds', 'num_fronds']
        z_scale = bq.LinearScale(min=grid.z.minval, max=grid.z.maxval)
        z_ax = bq.Axis(scale=z_scale, label='Depth (z)', grid_lines='none')

        mins = {
            'water_speeds': 0,
            'water_angles': 0,
            'frond_lengths': 0,
            'frond_stds': 0,
            'num_fronds': 0,
        }

        maxs = {
            'water_speeds': 50,
            'water_angles': 2*np.pi,
            'frond_lengths': 3,
            'frond_stds': .5,
            'num_fronds': 5000
        }

        colors = {
            'water_speeds': 'red',
            'water_angles': 'green',
            'frond_lengths': 'blue',
            'frond_stds': 'yellow',
            'num_fronds': 'purple'
        }

        ylabels = {
            'water_speeds': 'Water current velocity',
            'water_angles': 'Water current angle',
            'frond_lengths': 'Frond length mean',
            'frond_stds': 'Frond length std. dev.',
            'num_fronds': 'Number of fronds'
        }


        values = {}
        figs = {}

        for quant in z_quants:
            values[quant] = getattr(rope, quant)
            ylim = np.array([mins[quant], maxs[quant]], dtype=float)
            labels = {'xlabel': 'z', 'ylabel': '', 'title': ylabels[quant]}
            figs[quant] = HandDrawFigure(rope, quant, grid.z, ylim, labels, color=colors[quant])

        self.children = [
            title,
            ipw.HBox([
                figs['water_speeds'],
                figs['water_angles'],
            ]),
            ipw.HBox([
                figs['frond_lengths'],
                figs['frond_stds']
            ]),
            ipw.HBox([
                figs['num_fronds']
            ])
        ]
Exemple #29
0
    def __init__(self, session, state=None):

        # if we allow padding, we sometimes get odd behaviour with the interacts
        self.scale_x = bqplot.LinearScale(min=0, max=1, allow_padding=False)
        self.scale_y = bqplot.LinearScale(min=0, max=1)

        self.scales = {'x': self.scale_x, 'y': self.scale_y}
        self.axis_x = bqplot.Axis(scale=self.scale_x,
                                  grid_lines='none',
                                  label='x')
        self.axis_y = bqplot.Axis(scale=self.scale_y,
                                  orientation='vertical',
                                  tick_format='0.2f',
                                  grid_lines='none',
                                  label='y')

        self.figure = bqplot.Figure(scales=self.scales,
                                    animation_duration=0,
                                    axes=[self.axis_x, self.axis_y],
                                    fig_margin={
                                        'left': 60,
                                        'bottom': 60,
                                        'top': 10,
                                        'right': 10
                                    })
        self.figure.padding_y = 0
        self._fig_margin_default = self.figure.fig_margin
        self._fig_margin_zero = dict(self.figure.fig_margin)
        self._fig_margin_zero['left'] = 0
        self._fig_margin_zero['bottom'] = 0

        super(BqplotBaseView, self).__init__(session, state=state)

        # Remove the following two lines once glue v0.16 is required - see
        # https://github.com/glue-viz/glue/pull/2099/files for more information.
        self.state.remove_callback('layers', self._sync_layer_artist_container)
        self.state.add_callback('layers',
                                self._sync_layer_artist_container,
                                priority=10000)

        def update_axes(*ignore):
            try:
                # Extract units from data
                x_unit = self.state.reference_data.get_component(
                    self.state.x_att_world).units
            except AttributeError:
                # If no data loaded yet, ignore units
                x_unit = ""
            finally:
                # Append units to axis label
                self.axis_x.label = str(self.state.x_att) + " " + str(x_unit)
            if self.is2d:
                self.axis_y.label = str(self.state.y_att)
                try:
                    y_unit = self.state.reference_data.get_component(
                        self.state.y_att_world).units
                except AttributeError:
                    y_unit = ""
                finally:
                    self.axis_y.label = str(
                        self.state.y_att) + " " + str(y_unit)

        self.state.add_callback('x_att', update_axes)
        if self.is2d:
            self.state.add_callback('y_att', update_axes)

        self.scale_x.observe(self.update_glue_scales, names=['min', 'max'])
        self.scale_y.observe(self.update_glue_scales, names=['min', 'max'])

        dlink((self.state, 'x_min'), (self.scale_x, 'min'), float_or_none)
        dlink((self.state, 'x_max'), (self.scale_x, 'max'), float_or_none)
        dlink((self.state, 'y_min'), (self.scale_y, 'min'), float_or_none)
        dlink((self.state, 'y_max'), (self.scale_y, 'max'), float_or_none)

        on_change([(self.state, 'show_axes')])(self._sync_show_axes)

        self.create_layout()
Exemple #30
0
    def _gen_series_tab(self):
        """Creates widgets for selecting timeseries data.
          - Dropdown to select column from dataframe.
          - Interactive bqplot with BrushIntervalSelector.
    
        Returns
        -------
        widget : tab content widget
        """
        scales = {
            'x': bqp.DateScale(),
            'y': bqp.LinearScale(),
            'color': bqp.ColorScale(scheme='oranges')
        }

        axes = [
            bqp.Axis(scale=scales['x'],
                     label='Date',
                     num_ticks=int(len(self._data.index) / 2)),
            bqp.Axis(scale=scales['y'], label='Value', orientation='vertical')
        ]

        mark = bqp.Lines(scales=scales)

        feature_selector = widgets.Dropdown(options=self._data.columns,
                                            description='Feature:')

        def feature_selection_callback(change):
            """Callback to update graph when new data has been selected
    
            Parameters
            ----------
            change : dict
                Widget and changed values.
            """
            self._selected_feature = change.new
            fig.title = f'Feature: {change.new}'
            series = self._data[change.new]
            mark.x = series.index
            mark.y = series.values

        # register callback for dropdown value-change events
        feature_selector.observe(feature_selection_callback, ['value'])

        def brush_sel_dt_callback(change):
            """Callback to update graph when new data has been selected
    
            Parameters
            ----------
            change : dict
                Widget and changed values.
            """
            tstamps = selector.selected
            if isinstance(tstamps, np.ndarray):
                tstamps = tstamps.tolist()
            if not selector.brushing and tstamps:
                # extract year and month from timestamp string: e.g. 2000-03-11T06:51:50.089000 -> 2000-03
                dates = [
                    pd.to_datetime(re.split(r'-\d\dT', str(ts))[0])
                    for ts in tstamps
                ]
                self._selected_dates = dates

        selector = bqpi.BrushIntervalSelector(scale=scales['x'], color='blue')
        # register callback for BrushIntervalSelector brushing events
        selector.observe(brush_sel_dt_callback, ['brushing'])

        fig = bqp.Figure(marks=[mark],
                         axes=axes,
                         interaction=selector,
                         layout=widgets.Layout(width='880px', height='380px'),
                         animation_duration=1000)

        def reset_btn_click(b):
            """Reset selector and update instance properties accordingly.
    
            Parameters
            ----------
            b : Button
                Button instance.
            """
            selector.reset()
            self.reset()

        reset_btn = widgets.Button(description='reset')
        reset_btn.on_click(reset_btn_click)

        return widgets.VBox(children=[
            widgets.HBox(children=[feature_selector, reset_btn]), fig
        ])