class MatrixDisplayFilter(object):
    def __init__(self, view_box, data_fields):
        self.output = None
        self._signalHandler = SignalHandler()
        self.sigOutputChanged = self._signalHandler.sigOutputChanged
        self.view_box = view_box
        self.legend = None
        self.colorMap = ColorMapParameter()
        self.data_fields = data_fields
        self.field_names = [field[0] for field in self.data_fields]

        self.colorMap.setFields(self.data_fields)

        self.params = Parameter.create(name='Matrix Display', type='group', children=[
            self.colorMap,
            {'name': 'Text format', 'type': 'str'},
            {'name': 'Show Confidence', 'type': 'list', 'values': [field[0] for field in self.data_fields]},
            {'name': 'log_scale', 'type': 'bool'},
        ])
    
        self.params.sigTreeStateChanged.connect(self.invalidate_output)

    def element_display_output(self, result, default_bgcolor):
        colormap = self.colorMap
        show_confidence = self.params['Show Confidence']
        text_format = self.params['Text format']
        self.output = {}

        # if show_confidence != 'None':
        #     default_bgcolor = np.array([128., 128., 128., 255.])
        # else:
        #     default_bgcolor = np.array([220., 220., 220.])
        
        # if result['no_data'] is False:
        #     self.output['bgcolor'] = tuple(default_bgcolor)
        #     self.output['fgcolor'] = 0.6
        #     self.output['text'] = ''
        # else:
        result_vals = result.loc[:, 'metric_summary']
        mappable_result = {k:v for k,v in result_vals.iteritems() if np.isscalar(v)}

        color = colormap.map(mappable_result)[0]
    
        # desaturate low confidence cells
        if show_confidence != 'None':
            lower, upper = result[show_confidence, 'metric_conf']
            confidence = (1.0 - (upper - lower)) ** 2
            color = color * confidence + default_bgcolor * (1.0 - confidence)
        # invert text color for dark background
        self.output['fgcolor'] = 'w' if sum(color[:3]) < 384 else 'k'
        text_result = {k:FormattableNumber(v) if isinstance(v, float) else v for k, v in result_vals.iteritems()}
        self.output['text'] = text_format.format(**text_result)
        self.output['bgcolor'] = tuple(color)

        return self.output
    
    def colormap_legend(self):
        if self.legend is not None:
            self.view_box.removeItem(self.legend)
        cmap_item = [cmap for cmap in self.colorMap.children() if cmap['Enabled'] is True][0]
        log_scale = self.params.child('log_scale').value()
        colors = cmap_item.value().color
        x_min = cmap_item['Min']
        x_max = cmap_item['Max']
        x = np.linspace(x_min, x_max, len(colors))
        name = cmap_item.name()
        # units = self.colorMap.fields[name].get('units', None)
        scale, prefix = pg.siScale(x_min)
        # if units is not None:
        #     units = scale + units
        # else:
        #     units = ''
        self.legend = pg.GradientLegend([25, 300], [-20, -30])
        if log_scale is True:
            cmap2 = pg.ColorMap(x, colors)
            self.legend.setGradient(cmap2.getGradient())
            self.legend.setLabels({'%0.02f' % (a*scale):b for a,b in zip(cmap_item.value().pos, x)})
        else:
            self.legend.setGradient(cmap_item.value().getGradient())
            self.legend.setLabels({'%0.02f' % (a*scale):b for a,b in zip(x, cmap_item.value().pos)})
        self.view_box.addItem(self.legend)

    def get_colormap_field(self):
        color_map_fields = self.colorMap.children()
        if len(color_map_fields) > 1:
            field_name = [field.name() for field in color_map_fields if field['Enabled'] is True][0]
        else:
            field_name = color_map_fields[0].name()

        return field_name

    def invalidate_output(self):
        self.output = None
Пример #2
0
class MatrixDisplayFilter(object):
    def __init__(self, main_window, data_fields, text_fields):
        self.output = None
        self._signalHandler = SignalHandler()
        self.sigOutputChanged = self._signalHandler.sigOutputChanged
        self.view_box = main_window.matrix_widget.view_box
        self.main_window = main_window
        self.legend = None
        self.colorMap = ColorMapParameter()
        self.data_fields = data_fields
        self.text_fields = text_fields
        self.field_names = [field[0] for field in self.data_fields]

        self.colorMap.setFields(self.data_fields)

        self.params = Parameter.create(
            name='Matrix Display',
            type='group',
            children=[
                self.colorMap,
                {
                    'name': 'Text format',
                    'type': 'str'
                },
                {
                    'name': 'Show Confidence',
                    'type': 'list',
                    'values': [field[0] for field in self.data_fields],
                    'value': 'None'
                },
                # {'name': 'log_scale', 'type': 'bool'},
            ])

        self.params.sigTreeStateChanged.connect(self.invalidate_output)
        self.colorMap.sigColorMapChanged.connect(self.set_default_text)

    def set_default_text(self):
        map_field = self.get_colormap_field()
        default_text = self.text_fields.get(map_field, '')
        self.params.child('Text format').setValue(default_text)

    def element_display_output(self, result, default_bgcolor):
        colormap = self.colorMap
        show_confidence = self.params['Show Confidence']
        text_format = self.params['Text format']
        self.output = {}

        # if show_confidence != 'None':
        #     default_bgcolor = np.array([128., 128., 128., 255.])
        # else:
        #     default_bgcolor = np.array([220., 220., 220.])

        # if result['no_data'] is False:
        #     self.output['bgcolor'] = tuple(default_bgcolor)
        #     self.output['fgcolor'] = 0.6
        #     self.output['text'] = ''
        # else:
        result_vals = result.loc[:, 'metric_summary']
        result_vals.replace(np.nan, '')
        mappable_result = {
            k: v
            for k, v in result_vals.iteritems() if np.isscalar(v)
        }

        color = colormap.map(mappable_result)[0]

        # desaturate low confidence cells
        if show_confidence != 'None':
            lower, upper = result[show_confidence, 'metric_conf']
            confidence = (1.0 - (upper - lower))**2
            color = color * confidence + default_bgcolor * (1.0 - confidence)
        # invert text color for dark background
        self.output['fgcolor'] = 'w' if sum(color[:3]) < 384 else 'k'
        text_result = {
            k: FormattableNumber(v) if isinstance(v, float) else v
            for k, v in result_vals.iteritems()
        }
        self.output['text'] = text_format.format(**text_result)
        if self.output['text'] == 'nan':
            self.output['text'] = ''
        self.output['bgcolor'] = tuple(color)

        return self.output

    def colormap_legend(self):
        if self.legend is not None:
            self.view_box.removeItem(self.legend)
        if len(self.colorMap.children()) == 0:
            pg.QtGui.QMessageBox.information(
                self.main_window, '',
                "No Analysis ColorMap is selected, please add one and Update Results",
                pg.QtGui.QMessageBox.Ok)
            raise Exception("No color maps are selected.")
        cmap_item = [
            cmap for cmap in self.colorMap.children()
            if cmap['Enabled'] is True
        ][0]
        # log_scale = self.params.child('log_scale').value()
        colors = cmap_item.value().color
        x_min = cmap_item['Min']
        x_max = cmap_item['Max']
        x = np.linspace(x_min, x_max, len(colors))
        name = cmap_item.name()
        if name.endswith('Probability'):
            log_scale = True
        else:
            log_scale = False
        # units = self.colorMap.fields[name].get('units', None)
        min_scale, _ = pg.siScale(x_min)
        max_scale, _ = pg.siScale(x_max)
        scale = min_scale if (1 / min_scale) < (1 / max_scale) else max_scale
        # if units is not None:
        #     units = scale + units
        # else:
        #     units = ''
        self.legend = pg.GradientLegend([25, 300], [-20, -30])
        if log_scale is True:
            cmap2 = pg.ColorMap(x, colors)
            self.legend.setGradient(cmap2.getGradient())
            self.legend.setLabels({
                '%0.02f' % (a * scale): b
                for a, b in zip(cmap_item.value().pos, x)
            })
        else:
            self.legend.setGradient(cmap_item.value().getGradient())
            self.legend.setLabels({
                '%0.02f' % (a * scale): b
                for a, b in zip(x,
                                cmap_item.value().pos)
            })
        self.view_box.addItem(self.legend)

    def get_colormap_field(self):
        color_map_fields = self.colorMap.children()
        if len(color_map_fields) == 0:
            field_name = ''
        elif len(color_map_fields) > 1:
            field_name = [
                field.name() for field in color_map_fields
                if field['Enabled'] is True
            ][0]
        else:
            field_name = color_map_fields[0].name()

        return field_name

    def invalidate_output(self):
        self.output = None
Пример #3
0
class MatrixDisplayFilter(object):
    def __init__(self, view_box, data_fields):
        self.output = None
        self._signalHandler = SignalHandler()
        self.sigOutputChanged = self._signalHandler.sigOutputChanged
        self.view_box = view_box
        self.legend = None
        self.colorMap = ColorMapParameter()
        self.data_fields = data_fields
        self.field_names = [field[0] for field in self.data_fields]

        self.colorMap.setFields(self.data_fields)

        self.params = Parameter.create(
            name='Matrix Display',
            type='group',
            children=[
                self.colorMap,
                {
                    'name': 'Text format',
                    'type': 'str'
                },
                {
                    'name': 'Show Confidence',
                    'type': 'list',
                    'values': [field[0] for field in self.data_fields]
                },
                {
                    'name': 'log_scale',
                    'type': 'bool'
                },
            ])

        self.params.sigTreeStateChanged.connect(self.invalidate_output)

    def element_display_output(self, result, default_bgcolor):
        colormap = self.colorMap
        show_confidence = self.params['Show Confidence']
        text_format = self.params['Text format']
        self.output = {}

        # if show_confidence != 'None':
        #     default_bgcolor = np.array([128., 128., 128., 255.])
        # else:
        #     default_bgcolor = np.array([220., 220., 220.])

        # if result['no_data'] is False:
        #     self.output['bgcolor'] = tuple(default_bgcolor)
        #     self.output['fgcolor'] = 0.6
        #     self.output['text'] = ''
        # else:
        result_vals = result.loc[:, 'metric_summary']
        mappable_result = {
            k: v
            for k, v in result_vals.iteritems() if np.isscalar(v)
        }

        color = colormap.map(mappable_result)[0]

        # desaturate low confidence cells
        if show_confidence != 'None':
            lower, upper = result[show_confidence, 'metric_conf']
            confidence = (1.0 - (upper - lower))**2
            color = color * confidence + default_bgcolor * (1.0 - confidence)
        # invert text color for dark background
        self.output['fgcolor'] = 'w' if sum(color[:3]) < 384 else 'k'
        text_result = {
            k: FormattableNumber(v) if isinstance(v, float) else v
            for k, v in result_vals.iteritems()
        }
        self.output['text'] = text_format.format(**text_result)
        self.output['bgcolor'] = tuple(color)

        return self.output

    def colormap_legend(self):
        if self.legend is not None:
            self.view_box.removeItem(self.legend)
        cmap_item = [
            cmap for cmap in self.colorMap.children()
            if cmap['Enabled'] is True
        ][0]
        log_scale = self.params.child('log_scale').value()
        colors = cmap_item.value().color
        x_min = cmap_item['Min']
        x_max = cmap_item['Max']
        x = np.linspace(x_min, x_max, len(colors))
        name = cmap_item.name()
        # units = self.colorMap.fields[name].get('units', None)
        scale, prefix = pg.siScale(x_min)
        # if units is not None:
        #     units = scale + units
        # else:
        #     units = ''
        self.legend = pg.GradientLegend([25, 300], [-20, -30])
        if log_scale is True:
            cmap2 = pg.ColorMap(x, colors)
            self.legend.setGradient(cmap2.getGradient())
            self.legend.setLabels({
                '%0.02f' % (a * scale): b
                for a, b in zip(cmap_item.value().pos, x)
            })
        else:
            self.legend.setGradient(cmap_item.value().getGradient())
            self.legend.setLabels({
                '%0.02f' % (a * scale): b
                for a, b in zip(x,
                                cmap_item.value().pos)
            })
        self.view_box.addItem(self.legend)

    def get_colormap_field(self):
        color_map_fields = self.colorMap.children()
        if len(color_map_fields) > 1:
            field_name = [
                field.name() for field in color_map_fields
                if field['Enabled'] is True
            ][0]
        else:
            field_name = color_map_fields[0].name()

        return field_name

    def invalidate_output(self):
        self.output = None