Beispiel #1
0
    def process(self, inputs):
        """
        Plot the P & L graph from the `strategy_returns` column.
        `label` in the `conf` defines the stock symbol name

        Arguments
        -------
         inputs: list
            list of input dataframes.
        Returns
        -------
        Figure

        """
        input_df = inputs[0]
        if isinstance(input_df,  dask_cudf.DataFrame):
            input_df = input_df.compute()  # get the computed value
        label = 'stock'
        if 'label' in self.conf:
            label = self.conf['label']
        num_points = self.conf['points']
        stride = max(len(input_df) // num_points, 1)
        date_co = DateScale()
        linear_co = LinearScale()
        yax = Axis(label='Cumulative return', scale=linear_co,
                   orientation='vertical')
        xax = Axis(label='Time', scale=date_co, orientation='horizontal')
        panzoom_main = PanZoom(scales={'x': [date_co]})
        line = Lines(x=input_df['datetime'][::stride],
                     y=(input_df['strategy_returns'].cumsum())[::stride],
                     scales={'x': date_co, 'y': linear_co},
                     colors=['blue'], labels=[label], display_legend=True)
        new_fig = Figure(marks=[line], axes=[yax, xax], title='P & L',
                         interaction=panzoom_main)
        return new_fig
Beispiel #2
0
    def __init__(self, viewer, **kwargs):

        super().__init__(viewer, **kwargs)

        self.interact = PanZoom(scales={
            'x': [self.viewer.scale_x],
            'y': [self.viewer.scale_y]
        })
Beispiel #3
0
    def process(self, inputs):
        """
        Plot the lines from the input dataframe. The plotted lines are the
        columns in the input dataframe which are specified in the `lines` of
        node's `conf`
        The plot title is defined in the `title` of the node's `conf`

        Arguments
        -------
         inputs: list
            list of input dataframes.
        Returns
        -------
        Figure
        """

        input_df = inputs[self.INPUT_PORT_NAME]

        num_points = self.conf['points']
        stride = max(len(input_df) // num_points, 1)
        date_co = DateScale()
        linear_co = LinearScale()
        yax = Axis(label='', scale=linear_co, orientation='vertical')
        xax = Axis(label='Time', scale=date_co, orientation='horizontal')
        panzoom_main = PanZoom(scales={'x': [date_co]})
        lines = []
        for line in self.conf['lines']:
            col_name = line['column']
            label_name = line['label']
            color = line['color']
            if (isinstance(input_df, cudf.DataFrame)
                    or isinstance(input_df, dask_cudf.DataFrame)):
                line = Lines(x=input_df['datetime'][::stride].to_array(),
                             y=input_df[col_name][::stride].to_array(),
                             scales={
                                 'x': date_co,
                                 'y': linear_co
                             },
                             colors=[color],
                             labels=[label_name],
                             display_legend=True)
            else:
                line = Lines(x=input_df['datetime'][::stride],
                             y=input_df[col_name][::stride],
                             scales={
                                 'x': date_co,
                                 'y': linear_co
                             },
                             colors=[color],
                             labels=[label_name],
                             display_legend=True)

            lines.append(line)
        new_fig = Figure(marks=lines,
                         axes=[yax, xax],
                         title=self.conf['title'],
                         interaction=panzoom_main)
        return {self.OUTPUT_PORT_NAME: new_fig}
Beispiel #4
0
    def process(self, inputs):
        """
        Plot the ROC curve

        Arguments
        -------
         inputs: list
            list of input dataframes.
        Returns
        -------
        Figure

        """
        input_df = inputs[self.INPUT_PORT_NAME]
        if isinstance(input_df,  dask_cudf.DataFrame):
            input_df = input_df.compute()  # get the computed value

        label_col = input_df[self.conf['label']].values
        pred_col = input_df[self.conf['prediction']].values

        if isinstance(input_df, cudf.DataFrame):
            fpr, tpr, _ = metrics.roc_curve(label_col.get(),
                                            pred_col.get())
        else:
            fpr, tpr, _ = metrics.roc_curve(label_col,
                                            pred_col)
        auc_value = metrics.auc(fpr, tpr)
        out = {}

        if self.outport_connected(self.OUTPUT_PORT_NAME):
            linear_x = LinearScale()
            linear_y = LinearScale()
            yax = Axis(label='True Positive Rate', scale=linear_x,
                       orientation='vertical')
            xax = Axis(label='False Positive Rate', scale=linear_y,
                       orientation='horizontal')
            panzoom_main = PanZoom(scales={'x': [linear_x]})
            curve_label = 'ROC (area = {:.2f})'.format(auc_value)
            line = Lines(x=fpr, y=tpr,
                         scales={'x': linear_x, 'y': linear_y},
                         colors=['blue'], labels=[curve_label],
                         display_legend=True)
            new_fig = Figure(marks=[line], axes=[yax, xax], title='ROC Curve',
                             interaction=panzoom_main)
            out.update({self.OUTPUT_PORT_NAME: new_fig})
        if self.outport_connected(self.OUTPUT_VALUE_NAME):
            out.update({self.OUTPUT_VALUE_NAME: float(auc_value)})
        return out
Beispiel #5
0
    def __init__(self,
                 volume=None,
                 default_directory=os.getcwd(),
                 title='',
                 enhancement_steps=1000,
                 **kwargs):
        def on_chosen_path_change(old_path, new_path):
            self.dataset = FolderDataset(new_path)
            # TODO: If the path doesn't contain images, display a warning

        # A widget for changing the image folder
        self.pathchooser = PathChooser(
            chosen_path_desc='Image folder:',
            default_directory=default_directory,
            on_chosen_path_change=on_chosen_path_change,
        )
        self.pathchooser.layout.margin = '0 0 10px 0'

        # The number of increments of the min/max slider
        self.enhancement_steps = enhancement_steps

        self.scales = {
            'x': LinearScale(),
            'y': LinearScale(),
        }

        # The currently displayed image will be in bytes at `self.image_plot.image.value`
        self.image_plot = BQImage(
            image=IPyImage(),
            scales=self.scales,
        )

        self.figure = Figure(
            marks=[self.image_plot],
            padding_x=0,
            padding_y=0,
            animation_duration=1000,
            fig_margin={
                'top': 0,
                'right': 0,
                'bottom': 0,
                'left': 0,
            },
            layout=Layout(
                grid_area='figure',
                margin='0',
                width='320px',
                height='320px',
            ),
        )

        # Custom toolbar
        toolbar_width = '100%'
        toolbar_margin = '0px 0 2px 0'
        self.pan_zoom = PanZoom(scales={
            'x': [self.scales['x']],
            'y': [self.scales['y']],
        }, )

        self.save_button = Button(
            description='Save Image',
            tooltip='Save Image',
            icon='save',
            layout=Layout(
                width=toolbar_width,
                # flex='1 1 auto',
                margin=toolbar_margin,
            ),
        )
        self.save_button.on_click(self.save_current_image)

        self.hide_button = Button(
            description='Hide Image',
            tooltip='Hide Image',
            icon='eye-slash',
            layout=Layout(
                width=toolbar_width,
                # flex='1 1 auto',
                margin=toolbar_margin,
            ))
        self.hide_button.on_click(self.hide_current_image)

        self.pan_zoom_toggle_button = ToggleButton(
            description='Pan / Zoom',
            tooltip='Pan/Zoom',
            icon='arrows',
            layout=Layout(
                width=toolbar_width,
                # flex='1 1 auto',
                margin=toolbar_margin,
            ),
        )
        self.pan_zoom_toggle_button.observe(self.on_pan_zoom_toggle,
                                            names='value')

        self.reset_pan_zoom_button = Button(
            description='Undo Zoom',
            tooltip='Reset pan/zoom',
            icon='refresh',
            layout=Layout(
                width=toolbar_width,
                # flex='1 1 auto',
                margin=toolbar_margin,
            ),
        )
        self.reset_pan_zoom_button.on_click(self.reset_pan_zoom)

        self.reset_enhancements_button = Button(
            description='Un-Enhance',
            tooltip='Reset enhancements',
            icon='ban',
            layout=Layout(
                width=toolbar_width,
                # flex='1 1 auto',
                margin=toolbar_margin,
            ),
        )
        self.reset_enhancements_button.on_click(self.reset_enhancements)

        self.mini_map = IPyImage(layout=Layout(
            grid_area='mini-map',
            margin='0',
        ))
        self.mini_map.width = 180
        self.mini_map.height = 180
        # PERFORMANCE CONCERN
        # Ideally instead of four observations, this would observe 'scales' on `self.pan_zoom`
        # However, it doesn't fire updates
        # Ref: https://github.com/bloomberg/bqplot/issues/800
        self.image_plot.scales['x'].observe(self.on_pan_zoom_change('x_min'),
                                            names='min')
        self.image_plot.scales['x'].observe(self.on_pan_zoom_change('x_max'),
                                            names='max')
        self.image_plot.scales['y'].observe(self.on_pan_zoom_change('y_min'),
                                            names='min')
        self.image_plot.scales['y'].observe(self.on_pan_zoom_change('y_max'),
                                            names='max')

        self.plane_toggle = ToggleButtons(
            options=['yz', 'xz', 'xy'],
            description='',
            disabled=False,
            button_style='',
            tooltips=[
                'Step in x direction', 'Step in y direction',
                'Step in z direction'
            ],
            layout=Layout(
                width='200px',
                # flex='1 1 auto',
                margin='7px 0 auto auto',
            ),
        )
        self.plane_toggle.style.button_width = 'auto'
        self.plane_toggle.observe(self.on_plane_change, names='value')

        self.toolbar = VBox(
            children=[
                self.save_button,
                self.hide_button,
                self.pan_zoom_toggle_button,
                self.reset_pan_zoom_button,
                self.reset_enhancements_button,
            ],
            layout=Layout(
                grid_area='toolbar',
                margin='0',
            ),
        )

        # Image enhancements
        self.min_max_slider = FloatRangeSlider(
            value=[0, 255],
            min=0,
            max=255,
            step=255 / self.enhancement_steps,
            description='Min/Max:',
            orientation='horizontal',
            readout=True,
            readout_format='.1f',
            continuous_update=True,
            layout=Layout(
                grid_area='min-max-slider',
                margin='10px 0 10px -10px',
                width='100%',
            ),
        )
        self.min_max_slider.observe(self.on_min_max_change, names='value')

        self.index_slider = IntSlider(
            value=0,
            min=0,
            max=1,
            step=1,
            description='Index:',
            orientation='horizontal',
            readout=True,
            readout_format='d',
            continuous_update=True,
            layout=Layout(
                grid_area='index-slider',
                margin='8px -20px 10px -36px',
                width='100%',
            ),
        )
        self.index_slider.observe(self.on_image_index_change, names='value')

        # Animation
        self.play = Play(
            value=self.index_slider.value,
            min=self.index_slider.min,
            max=self.index_slider.max,
            step=self.index_slider.step,
        )
        jslink((self.play, 'value'), (self.index_slider, 'value'))
        # Keep 'max' in sync as well
        self.index_slider.observe(self.on_index_slider_max_change, names='max')

        self.bottom_bar = HBox(
            children=[
                self.play,
                self.index_slider,
                self.plane_toggle,
            ],
            layout=Layout(
                grid_area='bottom-bar',
                margin=f'10px -20px 0 0',
                # overflow='hidden',
            ))

        # Layout
        self.gridbox = GridBox(children=[
            self.figure,
            self.toolbar,
            self.mini_map,
            self.min_max_slider,
            self.bottom_bar,
        ], )
        # Initially hidden without data
        self.gridbox.layout.display = 'none'

        self._dataset = None
        if volume is not None:
            self.dataset = VolumeDataset(volume)
            # Hide pathchooser when using a volume
            self.pathchooser.layout.display = 'none'

        # Call VBox super class __init__
        super().__init__(
            children=[
                self.pathchooser,
                self.gridbox,
            ],
            layout=Layout(width='auto'),
            **kwargs,
        )