STOmics / Stereopy

A toolkit of spatial transcriptomic analysis.
MIT License
193 stars 64 forks source link

ValueError: All arrays must be of the same length #202

Closed Biomamba closed 10 months ago

Biomamba commented 11 months ago

Dear author: When I study the chapter Interactive Cluster, I selected the area by data.plt.interact_spatial_scatter and got back a stereo.core.stereo_exp_data.StereoExpData object successfully. However, when I run selected_data.plt.spatial_scatter(dot_size=30),I met the following error. My stereopy version is 0.14.0b1.

`--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[35], line 2 1 # 用代码查看一下新对象 ----> 2 selected_data.plt.spatial_scatter(dot_size=30)

File ~/miniconda3/envs/st14/lib/python3.8/site-packages/stereopy-0.14.0b1-py3.8.egg/stereo/plots/decorator.py:46, in download..wrapped(*args, *kwargs) 44 dpi = kwargs['out_dpi'] 45 del kwargs['out_dpi'] ---> 46 fig: Figure = func(args, **kwargs) 47 if type(fig) is not Figure: 48 return fig

File ~/miniconda3/envs/st14/lib/python3.8/site-packages/stereopy-0.14.0b1-py3.8.egg/stereo/plots/decorator.py:32, in plot_scale..wrapped(*args, *kwargs) 30 kwargs.setdefault('data_resolution', data_resolution) 31 kwargs.setdefault('data_bin_offset', data_bin_offset) ---> 32 return func(args, **kwargs)

File ~/miniconda3/envs/st14/lib/python3.8/site-packages/stereopy-0.14.0b1-py3.8.egg/stereo/plots/decorator.py:114, in reorganize_coordinate..wrapped(*args, *kwargs) 108 if reorganize_coordinate: 109 data.position, data.position_offset = \ 110 reorganize_data_coordinates( 111 data.cells.batch, data.position, data.position_offset, 112 reorganize_coordinate, horizontal_offset_additional, vertical_offset_additional 113 ) --> 114 res = func(args, **kwargs) 115 data.reset_position() 116 return res

File ~/miniconda3/envs/st14/lib/python3.8/site-packages/stereopy-0.14.0b1-py3.8.egg/stereo/plots/plot_collection.py:353, in PlotCollection.spatial_scatter(self, cells_key, ncols, dot_size, palette, width, height, x_label, ylabel, title, **kwargs) 351 if title is None: 352 title = [' '.join(i.split('')) for i in cells_key] --> 353 fig = multi_scatter( 354 x=self.data.position[:, 0], 355 y=self.data.position[:, 1], 356 hue=[self.data.cells.get_property(key) for key in cells_key], 357 x_label=x_label, 358 y_label=y_label, 359 title=title, 360 ncols=ncols, 361 dot_size=dot_size, 362 palette=palette, 363 color_bar=True, 364 width=width, 365 height=height, 366 **kwargs 367 ) 368 return fig

File ~/miniconda3/envs/st14/lib/python3.8/site-packages/stereopy-0.14.0b1-py3.8.egg/stereo/plots/scatter.py:324, in multi_scatter(x, y, hue, ncols, title, x_label, y_label, color_bar, color_bar_reverse, bad_color, dot_size, palette, vmin, vmax, width, height, show_plotting_scale, data_resolution, kwargs) 322 cv = cv.toarray()[0] 323 ax: Axes = fig.add_subplot(axs[i]) # ax = plt.subplot(axs[i]) || ax = fig.add_subplot(axs[1, 1])) --> 324 base_scatter(x, y, cv, 325 ax=ax, 326 title=title[i] if title is not None and title != '' else None, 327 x_label=x_label[i] if x_label is not None and x_label != '' else None, 328 y_label=y_label[i] if y_label is not None and y_label != '' else None, 329 color_bar=color_bar, 330 color_bar_reverse=color_bar_reverse, 331 bad_color=bad_color, 332 dot_size=dot_size, 333 palette=palette, 334 vmin=vmin, 335 vmax=vmax, 336 show_plotting_scale=show_plotting_scale, 337 data_resolution=data_resolution, 338 kwargs 339 ) 340 return fig

File ~/miniconda3/envs/st14/lib/python3.8/site-packages/stereopy-0.14.0b1-py3.8.egg/stereo/plots/scatter.py:99, in base_scatter(x, y, hue, ax, title, x_label, y_label, color_bar, color_bar_reverse, bad_color, dot_size, marker, palette, invert_y, legend_ncol, show_legend, show_ticks, vmin, vmax, SegmentedColormap, hue_order, width, height, show_plotting_scale, plotting_scale_width, data_resolution, data_bin_offset) 96 cmap = ListedColormap(colors) 97 cmap.set_bad(bad_color) ---> 99 sns.scatterplot(x=x, y=y, hue=hue, ax=ax, palette=cmap, size=hue, linewidth=0, marker=marker, 100 sizes=(dot_size, dot_size), vmin=vmin, vmax=vmax) 101 if vmin is None and vmax is None: 102 norm = plt.Normalize(hue.min(), hue.max())

File ~/miniconda3/envs/st13/lib/python3.8/site-packages/seaborn/_decorators.py:46, in _deprecate_positional_args..inner_f(*args, kwargs) 36 warnings.warn( 37 "Pass the following variable{} as {}keyword arg{}: {}. " 38 "From version 0.12, the only valid positional argument " (...) 43 FutureWarning 44 ) 45 kwargs.update({k: arg for k, arg in zip(sig.parameters, args)}) ---> 46 return f(kwargs)

File ~/miniconda3/envs/st13/lib/python3.8/site-packages/seaborn/relational.py:808, in scatterplot(x, y, hue, style, size, data, palette, hue_order, hue_norm, sizes, size_order, size_norm, markers, style_order, x_bins, y_bins, units, estimator, ci, n_boot, alpha, x_jitter, y_jitter, legend, ax, *kwargs) 793 @_deprecate_positional_args 794 def scatterplot( 795 , (...) 804 legend="auto", ax=None, **kwargs 805 ): 807 variables = _ScatterPlotter.get_semantics(locals()) --> 808 p = _ScatterPlotter( 809 data=data, variables=variables, 810 x_bins=x_bins, y_bins=y_bins, 811 estimator=estimator, ci=ci, n_boot=n_boot, 812 alpha=alpha, x_jitter=x_jitter, y_jitter=y_jitter, legend=legend, 813 ) 815 p.map_hue(palette=palette, order=hue_order, norm=hue_norm) 816 p.map_size(sizes=sizes, order=size_order, norm=size_norm)

File ~/miniconda3/envs/st13/lib/python3.8/site-packages/seaborn/relational.py:587, in _ScatterPlotter.init(self, data, variables, x_bins, y_bins, estimator, ci, n_boot, alpha, x_jitter, y_jitter, legend) 571 def init( 572 self, , 573 data=None, variables={}, (...) 581 # the kind of plot to draw, but for the time being we need to set 582 # this information so the SizeMapping can use it 583 self._default_sizerange = ( 584 np.r[.5, 2] np.square(mpl.rcParams["lines.markersize"]) 585 ) --> 587 super().init(data=data, variables=variables) 589 self.alpha = alpha 590 self.legend = legend

File ~/miniconda3/envs/st13/lib/python3.8/site-packages/seaborn/_core.py:605, in VectorPlotter.init(self, data, variables) 603 def init(self, data=None, variables={}): --> 605 self.assign_variables(data, variables) 607 for var, cls in self._semantic_mappings.items(): 608 609 # Create the mapping function 610 map_func = partial(cls.map, plotter=self)

File ~/miniconda3/envs/st13/lib/python3.8/site-packages/seaborn/_core.py:668, in VectorPlotter.assign_variables(self, data, variables) 666 else: 667 self.input_format = "long" --> 668 plot_data, variables = self._assign_variables_longform( 669 data, **variables, 670 ) 672 self.plot_data = plot_data 673 self.variables = variables

File ~/miniconda3/envs/st13/lib/python3.8/site-packages/seaborn/_core.py:927, in VectorPlotter._assign_variables_longform(self, data, **kwargs) 923 variables[key] = getattr(val, "name", None) 925 # Construct a tidy plot DataFrame. This will convert a number of 926 # types automatically, aligning on index in case of pandas objects --> 927 plot_data = pd.DataFrame(plot_data) 929 # Reduce the variables dictionary to fields with valid data 930 variables = { 931 var: name 932 for var, name in variables.items() 933 if plot_data[var].notnull().any() 934 }

File ~/miniconda3/envs/st13/lib/python3.8/site-packages/pandas/core/frame.py:664, in DataFrame.init(self, data, index, columns, dtype, copy) 658 mgr = self._init_mgr( 659 data, axes={"index": index, "columns": columns}, dtype=dtype, copy=copy 660 ) 662 elif isinstance(data, dict): 663 # GH#38939 de facto copy defaults to False only in non-dict cases --> 664 mgr = dict_to_mgr(data, index, columns, dtype=dtype, copy=copy, typ=manager) 665 elif isinstance(data, ma.MaskedArray): 666 import numpy.ma.mrecords as mrecords

File ~/miniconda3/envs/st13/lib/python3.8/site-packages/pandas/core/internals/construction.py:493, in dict_to_mgr(data, index, columns, dtype, typ, copy) 489 else: 490 # dtype check to exclude e.g. range objects, scalars 491 arrays = [x.copy() if hasattr(x, "dtype") else x for x in arrays] --> 493 return arrays_to_mgr(arrays, columns, index, dtype=dtype, typ=typ, consolidate=copy)

File ~/miniconda3/envs/st13/lib/python3.8/site-packages/pandas/core/internals/construction.py:118, in arrays_to_mgr(arrays, columns, index, dtype, verify_integrity, typ, consolidate) 115 if verify_integrity: 116 # figure out the index, if necessary 117 if index is None: --> 118 index = _extract_index(arrays) 119 else: 120 index = ensure_index(index)

File ~/miniconda3/envs/st13/lib/python3.8/site-packages/pandas/core/internals/construction.py:666, in _extract_index(data) 664 lengths = list(set(raw_lengths)) 665 if len(lengths) > 1: --> 666 raise ValueError("All arrays must be of the same length") 668 if have_dicts: 669 raise ValueError( 670 "Mixing dicts with non-Series may lead to ambiguous ordering." 671 )

ValueError: All arrays must be of the same length`

tanliwei-coder commented 11 months ago

Sorry! this is a bug, we will fix it at next version.

Currently, in order to avoid this error, you can save the selected_data to a new gef and read it to run the following steps.

You can refer to this tutorial.