Пример #1
0
    def _create_tabs(self):
        "Creates a tab for each variable"
        self.lis = self.classl if self.is_ordered else list(
            permutations(self.classl, 2))
        if self._is_tab:
            self._boxes = len(self.df_list)
            self._cols = math.ceil(math.sqrt(self._boxes))
            self._rows = math.ceil(self._boxes / self._cols)
            self.tbnames = list(
                self.df_list[0].columns
            )[:-1] if self.varlist is None else self.varlist
        else:
            vals = self.interp.most_confused()
            self._ranges = []
            self.tbnames = []
            self._boxes = int(
                input(
                    'Please enter a value for `k`, or the top images you will see: '
                ))
            for x in iter(vals):
                for y in range(len(self.lis)):
                    if x[0:2] == self.lis[y]:
                        self._ranges.append(x[2])
                        self.tbnames.append(str(x[0] + ' | ' + x[1]))

        self.tb = widgets.TabBar(self.tbnames)
        self._populate_tabs()
Пример #2
0
 def __init__(self,
              learn,
              dl=None,
              cut_off=100,
              is_ordered=False,
              classlist=[],
              varlist=None,
              figsize=(12, 12),
              **kwargs):
     dl = learn.dls[1] if dl is None else dl
     interp = ClassificationInterpretation.from_learner(learn, dl=dl)
     combs = classlist if is_ordered else list(
         itertools.permutations(classlist, 2))
     figsize = figsize
     cut_off = cut_off
     vocab = interp.vocab
     _, tl_idx = interp.top_losses(len(interp.losses))
     idxs = dl.get_losses(tl_idx, interp.preds, combs)
     mc = interp.most_confused()
     tbnames, boxes, cols, rows, ranges = self._get_names(x=dl,
                                                          idxs=idxs,
                                                          mc=mc,
                                                          varlist=varlist,
                                                          li=combs)
     tb = widgets.TabBar(tbnames)
     self._create_tabs(tb, tbnames, dl, interp, idxs, combs, boxes, cols,
                       rows, ranges, figsize, cut_off)
Пример #3
0
def plotting_region_consumption(_data_mapping, _rolling, _figsize):
    _d = list(_data_mapping.keys())

    tb = widgets.TabBar(_d, location='start')

    for i in _d:
        with tb.output_to(i):
            _asu, _LOX, _LOX_ALSF, test_plot, test_stats = _data_mapping[i]

            d_ = [
                'enlèvements', 'covid 19', 'lits', 'réanimations',
                'market share'
            ]
            tb_ = widgets.TabBar(d_, location='top')

            for j in d_:
                with tb_.output_to(j):
                    if j == 'enlèvements':
                        try:
                            LOX_rolling_plot = _LOX[
                                'consommation totale'].rolling(_rolling).sum()
                            LOX_ALSF_rolling_plot = _LOX_ALSF[
                                'consommation totale'].rolling(_rolling).sum()
                            asu_rolling_plot = _asu.rolling(_rolling).sum()
                            asu_rolling_plot_ = list(asu_rolling_plot.columns)
                            asu_rolling_plot_ = [
                                'client LOX médical externes',
                                'client LOX médical internes'
                            ] + asu_rolling_plot_
                            asu_rolling_plot[
                                'client LOX médical externes'] = LOX_rolling_plot
                            asu_rolling_plot[
                                'client LOX médical internes'] = LOX_ALSF_rolling_plot
                            asu_rolling_plot = asu_rolling_plot[
                                asu_rolling_plot_]
                            asu_rolling_plot[asu_rolling_plot.index.month ==
                                             3].plot(figsize=_figsize)
                        except IndexError:
                            pass
                    elif j == 'covid 19':
                        test_plot.plot(figsize=_figsize)
                    else:
                        print(test_stats)
Пример #4
0
def displayTarBarImage(path_dir: Path,
                       first: int,
                       last: int,
                       start_last=False,
                       figsize: tuple = (3, 3)) -> None:
    _enum_items = list(islice(itemize(path_dir), first, last))
    tb_plt = widgets.TabBar([str(i) for i in range(first, last)],
                            location='start')

    for base_0 in range(0, last - first):
        with tb_plt.output_to(base_0, select=start_last):
            print(f'File name: {Path(_enum_items[base_0][1].name)}')
            print(
                f'Image Size: {imageSize(Path(_enum_items[base_0][1]))} (H x W)'
            )
            displayImage(Path(_enum_items[base_0][1]), figsize=figsize)
Пример #5
0
def TabBar(*args):
  """Returns a real TabBar or a mock. Useful for UIs that don't support JS."""
  if TABULATED_OUTPUT:
    return widgets.TabBar(*args)
  class MockTab:
      def __init__(self):
          pass
      def __enter__(self):
          pass
      def __exit__(self, *x):
          pass
  class MockTabBar:
      def __init__(self):
          pass
      def output_to(self, x):
          return MockTab()
  return MockTabBar()
Пример #6
0
def capa_vs_covid19_plotting(_data,
                             selection,
                             *,
                             figsize=(15, 15),
                             hspace=0.4,
                             wspace=0.1,
                             not_plotted=None):

    if not_plotted is None:
        not_plotted = list()

    regions_departements_mapping = pd.read_csv(
        'https://www.data.gouv.fr/en/datasets/r/987227fb-dcb2-429e-96af-8979f97c9c84'
    )
    regions_departements_mapping = regions_departements_mapping[[
        'dep_name', 'region_name'
    ]]
    gb_ = regions_departements_mapping.groupby('region_name')
    dep_name_series = gb_['dep_name'].apply(list)
    dep_name_series_mapping = dep_name_series.to_dict()

    dep_name_series_mapping_plot = {
        k: (v, max(math.ceil(len(v) / 2.0), 2))
        for k, v in dep_name_series_mapping.items() if k not in not_plotted
    }
    _d = list(dep_name_series_mapping_plot.keys())

    tb = widgets.TabBar(_d, location='start')

    for i in _d:
        with tb.output_to(i):
            dep_set, rows = dep_name_series_mapping_plot[i]
            try:
                plot_capacity_vs_covid19(_data,
                                         dep_set,
                                         rows,
                                         2,
                                         selection,
                                         figsize=figsize,
                                         hspace=hspace,
                                         wspace=wspace)
            except IndexError:
                pass

    return True
Пример #7
0
def plotting_region_consumption_(_data_mapping, _data_dep, _rolling, _figsize):
    _d = list(_data_mapping.keys())

    tb = widgets.TabBar(_d, location='start')

    for i in _d:
        with tb.output_to(i):
            _asu, _LOX, _LOX_ALSF, test_plot, test_stats, dep_set, map_lits, map_covid_19 = _data_mapping[
                i]

            d_ = [
                'enlèvements', 'covid 19', 'lits', 'réanimations',
                'market share'
            ]
            tb_ = widgets.TabBar(d_, location='top')

            for j in d_:
                with tb_.output_to(j):
                    if j == 'enlèvements':
                        try:
                            LOX_rolling_plot = _LOX[
                                'consommation totale'].rolling(_rolling).sum()
                            LOX_ALSF_rolling_plot = _LOX_ALSF[
                                'consommation totale'].rolling(_rolling).sum()
                            asu_rolling_plot = _asu.rolling(_rolling).sum()
                            asu_rolling_plot_ = list(asu_rolling_plot.columns)
                            asu_rolling_plot_ = [
                                'client LOX médical externes',
                                'client LOX médical internes'
                            ] + asu_rolling_plot_
                            asu_rolling_plot[
                                'client LOX médical externes'] = LOX_rolling_plot
                            asu_rolling_plot[
                                'client LOX médical internes'] = LOX_ALSF_rolling_plot
                            asu_rolling_plot = asu_rolling_plot[
                                asu_rolling_plot_]
                            asu_rolling_plot[asu_rolling_plot.index.month ==
                                             3].plot(figsize=_figsize)
                        except IndexError:
                            pass
                    elif j == 'covid 19':
                        tb_dep = widgets.TabBar(dep_set, location='bottom')

                        data_plot_region = _data_dep[['capacité (2018)'
                                                      ]].copy()
                        data_plot_region = data_plot_region[
                            data_plot_region.index.get_level_values(0).isin(
                                dep_set)]
                        data_plot_region = data_plot_region.groupby(
                            level=[1]).sum()
                        test_plot['capacité (2018)'] = data_plot_region[
                            'capacité (2018)']
                        test_plot = test_plot[[
                            'en reanimation ou soins intensifs',
                            'capacité (2018)', 'hospitalises', 'deces'
                        ]]

                        for d in dep_set:
                            with tb_dep.output_to(d):

                                _fig, _axs = plt.subplots(nrows=1,
                                                          ncols=2,
                                                          figsize=_figsize)
                                plotting_figure(_axs[0], test_plot, title=i)
                                data_plot_ = _data_dep.loc[d, :][[
                                    'reanimation', 'capacité (2018)'
                                ]]
                                plotting_figure(_axs[1], data_plot_, title=d)
                                plt.show()

                    elif j == 'market share':
                        print(test_stats)

                    elif j == 'lits':
                        fig_dep_set, ax_dep_set = plt.subplots(1,
                                                               1,
                                                               figsize=(10,
                                                                        10))
                        divider = make_axes_locatable(ax_dep_set)
                        cax = divider.append_axes("right", size="5%", pad=0.1)
                        fig_dep_set_ = map_lits.plot(column="lits",
                                                     cmap="Reds",
                                                     ax=ax_dep_set,
                                                     legend=True,
                                                     cax=cax)
                        for p in ['top', 'left', 'bottom', 'right']:
                            fig_dep_set_.axes.spines[p].set_visible(False)
                        fig_dep_set_.axes.xaxis.set_visible(False)
                        fig_dep_set_.axes.yaxis.set_visible(False)

                    elif j == 'réanimations':
                        fig_dep_set, ax_dep_set = plt.subplots(1,
                                                               1,
                                                               figsize=(10,
                                                                        10))
                        divider = make_axes_locatable(ax_dep_set)
                        cax = divider.append_axes("right", size="5%", pad=0.1)
                        fig_dep_set_ = map_covid_19.plot(column="reanimation",
                                                         cmap="Reds",
                                                         ax=ax_dep_set,
                                                         legend=True,
                                                         cax=cax)
                        for p in ['top', 'left', 'bottom', 'right']:
                            fig_dep_set_.axes.spines[p].set_visible(False)
                        fig_dep_set_.axes.xaxis.set_visible(False)
                        fig_dep_set_.axes.yaxis.set_visible(False)
Пример #8
0
                                    num_epochs=1,
                                    shuffle=False))
predictions = []
for prediction_item, in zip(predictions_dict):
    predictions.append(prediction_item['class_ids'][0])
actuals = list(test_df.loc[test_df[CATEGORY] == SUBGROUP]
               ['income_bracket'].apply(lambda x: '>50K' in x).astype(int))
classes = ['Over $50K', 'Less than $50K']

# To stay consistent, we have to flip the confusion
# matrix around on both axes because sklearn's confusion matrix module by
# default is rotated.
rotated_confusion_matrix = np.fliplr(confusion_matrix(actuals, predictions))
rotated_confusion_matrix = np.flipud(rotated_confusion_matrix)

tb = widgets.TabBar(['Confusion Matrix', 'Evaluation Metrics'], location='top')

with tb.output_to('Confusion Matrix'):
    plot_confusion_matrix(rotated_confusion_matrix, classes)

with tb.output_to('Evaluation Metrics'):
    grid = widgets.Grid(2, 3)

    p, r, fpr = compute_eval_metrics(actuals, predictions)

    with grid.output_to(0, 0):
        print('Precision ')
    with grid.output_to(1, 0):
        print(' %.4f ' % p)

    with grid.output_to(0, 1):