ImSoErgodic / py-upset

A pure-python implementation of the UpSet suite of visualisation methods by Lex, Gehlenborg et al.
313 stars 57 forks source link

Issue with newer version of matplotlib #22

Open JenniferShelton opened 6 years ago

JenniferShelton commented 6 years ago

Thanks for the package it is very useful. I do get an index error unless I use matplotlib 1.4.3. The new version (2.2.0) fails with the following trace back

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/nethome/jshelton/miniconda2/envs/pipenv3/lib/python3.6/site-packages/pyupset/visualisation.py", line 63, in plot
    upset = UpSetPlot(len(ordered_dfs), len(ordered_in_sets), additional_plots, query)
  File "/nethome/jshelton/miniconda2/envs/pipenv3/lib/python3.6/site-packages/pyupset/visualisation.py", line 127, in __init__
    self.ax_setsize, self.ax_tablenames, self.additional_plots_axes = self._prepare_figure(additional_plots)
  File "/nethome/jshelton/miniconda2/envs/pipenv3/lib/python3.6/site-packages/pyupset/visualisation.py", line 180, in _prepare_figure
    ax_setsize = plt.subplot(gs_top[-1:-setsize_h, 0:setsize_w])
  File "/nethome/jshelton/miniconda2/envs/pipenv3/lib/python3.6/site-packages/matplotlib/gridspec.py", line 161, in __getitem__
    [_normalize(k1, nrows), _normalize(k2, ncols)], (nrows, ncols))
  File "/nethome/jshelton/miniconda2/envs/pipenv3/lib/python3.6/site-packages/matplotlib/gridspec.py", line 153, in _normalize
    raise IndexError("invalid index")
IndexError: invalid index

You can recreate the error running your test dataset with updated matplotlib.

Best, Jennifer

sert23 commented 6 years ago

Same problem here, did you manage to fix this?

ColeBoudreau commented 6 years ago

I'm having the same issue, is there still no fix?

(edit) Okay so this definitely isn't the best fix, but I noticed in _prepare_figure, line 179-182, the gs_top was being indexed like gs_top[-1:-setsize_h, ...] but setsize_h is always greater than -1, causing that to return a null list. I think this is where the Index Error stems from. I simple changed the order (gs_top[-setsize_h:-1, ...]) so it could at least get past those lines correctly, and this lets me at least display the figure. This may ruin some functionality for additional plots, but those were unneeded for my purpose anyway. Hope this helps!

mproffitt commented 6 years ago

This isn't strictly a py-upset issue although it will most likely need to be resolved here.

In matplotlib > 2.2.0, the getitem method was re-written to include a new "normalize" definition. This normalize validates slices to ensure that the end key is greater than the start key, effectively blocking negative indexing.

I'm not sure this is a problem in matplotlib rather than desired behaviour but there are two ways of fixing it:

  1. patch matplotlib.gridspec:138-154

    def __getitem__(self, key):
        """Create and return a SuplotSpec instance.
        """
        nrows, ncols = self.get_geometry()
    
        def _normalize(key, size):  # Includes last index.
            if isinstance(key, slice):
                start, stop, _ = key.indices(size)
                if stop > start:
                    return start, stop - 1
    +              return stop - 1, start
            else:
                if key < 0:
                    key += size
                if 0 <= key < size:
                    return key, key
            raise IndexError("invalid index")
  2. More involved but friendlier and more compatible, patch py-upset to no longer use negative indexing on the grid.
waqarali141 commented 6 years ago

I'm Still having the same issue !

Any Fix around >?

Gustawsson-J commented 6 years ago

Also still having the same issue despite mproffitt's suggestion. The error's just moved to line 154.. Due to other dependencies, I have no means to downgrade matplotlib to 1.4.3 or alike.

Any ideas?

burkesquires commented 5 years ago

Same here...invalid index when I try to demo the code


IndexError Traceback (most recent call last)

in 5 data_dict = load(f) 6 ----> 7 pyu.plot(data_dict) ~/anaconda3/lib/python3.6/site-packages/pyupset/visualisation.py in plot(data_dict, unique_keys, sort_by, inters_size_bounds, inters_degree_bounds, additional_plots, query) 61 ordered_dfs, ordered_df_names = plot_data.ordered_dfs, plot_data.ordered_df_names 62 ---> 63 upset = UpSetPlot(len(ordered_dfs), len(ordered_in_sets), additional_plots, query) 64 fig_dict = upset.main_plot(ordered_dfs, ordered_df_names, ordered_in_sets, ordered_out_sets, 65 ordered_inters_sizes) ~/anaconda3/lib/python3.6/site-packages/pyupset/visualisation.py in __init__(self, rows, cols, additional_plots, query) 125 self.x_values, self.y_values = self._create_coordinates(rows, cols) 126 self.fig, self.ax_intbars, self.ax_intmatrix, \ --> 127 self.ax_setsize, self.ax_tablenames, self.additional_plots_axes = self._prepare_figure(additional_plots) 128 129 self.standard_graph_settings = { ~/anaconda3/lib/python3.6/site-packages/pyupset/visualisation.py in _prepare_figure(self, additional_plots) 178 intmatrix_w, intmatrix_h = tablesize_w + self.cols, self.rows 179 intbars_w, intbars_h = tablesize_w + self.cols, self.rows * 4 --> 180 ax_setsize = plt.subplot(gs_top[-1:-setsize_h, 0:setsize_w]) 181 ax_tablenames = plt.subplot(gs_top[-1:-tablesize_h, setsize_w:tablesize_w]) 182 ax_intmatrix = plt.subplot(gs_top[-1:-intmatrix_h, tablesize_w:intmatrix_w]) ~/anaconda3/lib/python3.6/site-packages/matplotlib/gridspec.py in __getitem__(self, key) 159 raise ValueError("unrecognized subplot spec") 160 num1, num2 = np.ravel_multi_index( --> 161 [_normalize(k1, nrows), _normalize(k2, ncols)], (nrows, ncols)) 162 else: # Single key 163 num1, num2 = _normalize(key, nrows * ncols) ~/anaconda3/lib/python3.6/site-packages/matplotlib/gridspec.py in _normalize(key, size) 151 if 0 <= key < size: 152 return key, key --> 153 raise IndexError("invalid index") 154 155 if isinstance(key, tuple): IndexError: invalid index
pedromiravaz commented 5 years ago

Fork using Cole Boudreau's solution above:

pedromiravaz/py-upset

(my first try at forking and changing someone else's code, so if I did something reproachable please let me know!)

keoughkath commented 5 years ago

Due to this issue I've switched to this version of UpSet implementation: https://github.com/jnothman/UpSetPlot and it seems to be working fine.