broadinstitute / wot

A software package for analyzing snapshots of developmental processes
https://broadinstitute.github.io/wot/
BSD 3-Clause "New" or "Revised" License
140 stars 34 forks source link

Error in visualizing trajectory #106

Open hanjun98 opened 1 year ago

hanjun98 commented 1 year ago

Hi, When I visualize trajectory using this code I have a error like that! so how can I solve this problem? do you have something to know about that? This data is tutorial data!

`# Visualize trajectories trajectory_dropdown = widgets.Dropdown( options=trajectory_ds.var.index, description='Trajectory:' )

def update_trajectory_vis(name): figure = plt.figure(figsize=(10, 10)) plt.axis('off') plt.tight_layout() plt.title(name) plt.scatter(coord_df['x'], coord_df['y'], c='#f0f0f0', s=4, marker=',', edgecolors='none', alpha=0.8) binned_df = trajectory_ds.obs.copy() binned_df['values'] = trajectory_ds[:, name].X binned_df = binned_df.groupby(['x', 'y'], as_index=False).sum() plt.scatter(binned_df['x'], binned_df['y'], c=binned_df['values'], s=6, marker=',', edgecolors='none', vmax=binned_df['values'].quantile(0.975)) plt.colorbar().ax.set_title('Trajectory')

widgets.interact(update_trajectory_vis, name=trajectory_dropdown)`


ValueError Traceback (most recent call last) File ~/mambaforge/envs/pegasus/lib/python3.9/site-packages/ipywidgets/widgets/interaction.py:240, in interactive.update(self, *args) 238 value = widget.get_interact_value() 239 self.kwargs[widget._kwarg] = value --> 240 self.result = self.f(**self.kwargs) 241 show_inline_matplotlib_plots() 242 if self.auto_display and self.result is not None:

Cell In[7], line 15, in update_trajectory_vis(name) 12 plt.scatter(coord_df['x'], coord_df['y'], c='#f0f0f0', 13 s=4, marker=',', edgecolors='none', alpha=0.8) 14 binned_df = trajectory_ds.obs.copy() ---> 15 binned_df['values'] = trajectory_ds[:, name].X 16 binned_df = binned_df.groupby(['x', 'y'], as_index=False).sum() 17 plt.scatter(binned_df['x'], binned_df['y'], c=binned_df['values'], 18 s=6, marker=',', edgecolors='none', vmax=binned_df['values'].quantile(0.975))

File ~/mambaforge/envs/pegasus/lib/python3.9/site-packages/pandas/core/frame.py:4091, in DataFrame.setitem(self, key, value) 4088 self._setitem_array([key], value) 4089 else: 4090 # set column -> 4091 self._set_item(key, value)

File ~/mambaforge/envs/pegasus/lib/python3.9/site-packages/pandas/core/frame.py:4300, in DataFrame._set_item(self, key, value) 4290 def _set_item(self, key, value) -> None: 4291 """ 4292 Add series to DataFrame in specified column. 4293 (...) 4298 ensure homogeneity. 4299 """ -> 4300 value, refs = self._sanitize_column(value) 4302 if ( 4303 key in self.columns 4304 and value.ndim == 1 4305 and not isinstance(value.dtype, ExtensionDtype) 4306 ): 4307 # broadcast across multiple columns if necessary 4308 if not self.columns.is_unique or isinstance(self.columns, MultiIndex):

File ~/mambaforge/envs/pegasus/lib/python3.9/site-packages/pandas/core/frame.py:5035, in DataFrame._sanitize_column(self, value) 5033 if is_dict_like(value): 5034 if not isinstance(value, Series): -> 5035 value = Series(value) 5036 return _reindex_for_setitem(value, self.index) 5038 if is_list_like(value):

File ~/mambaforge/envs/pegasus/lib/python3.9/site-packages/pandas/core/series.py:512, in Series.init(self, data, index, dtype, name, copy, fastpath) 510 data = data.copy() 511 else: --> 512 data = sanitize_array(data, index, dtype, copy) 514 manager = get_option("mode.data_manager") 515 if manager == "block":

File ~/mambaforge/envs/pegasus/lib/python3.9/site-packages/pandas/core/construction.py:646, in sanitize_array(data, index, dtype, copy, allow_2d) 643 subarr = cast(np.ndarray, subarr) 644 subarr = maybe_infer_to_datetimelike(subarr) --> 646 subarr = _sanitize_ndim(subarr, data, dtype, index, allow_2d=allow_2d) 648 if isinstance(subarr, np.ndarray): 649 # at this point we should have dtype be None or subarr.dtype == dtype 650 dtype = cast(np.dtype, dtype)

File ~/mambaforge/envs/pegasus/lib/python3.9/site-packages/pandas/core/construction.py:705, in _sanitize_ndim(result, data, dtype, index, allow_2d) 703 if allow_2d: 704 return result --> 705 raise ValueError( 706 f"Data must be 1-dimensional, got ndarray of shape {data.shape} instead" 707 ) 708 if is_object_dtype(dtype) and isinstance(dtype, ExtensionDtype): 709 # i.e. NumpyEADtype("O") 711 result = com.asarray_tuplesafe(data, dtype=np.dtype("object"))

ValueError: Data must be 1-dimensional, got ndarray of shape (165892, 1) instead

nekonekko commented 7 months ago

I am getting the same error.

I am using the tutorial data and Notebook 4 as it is distributed.

The environment is as follows:

Apple M2 Chip
macOS Sonoma ver 14.4.1
Python 3.11.6
anndata            0.10.6
appnope            0.1.4
array_api_compat   1.5.1
asttokens          2.4.1
comm               0.2.2
contourpy          1.2.0
cycler             0.12.1
Cython             3.0.9
debugpy            1.8.1
decorator          5.1.1
executing          2.0.1
fonttools          4.50.0
h5py               3.10.0
ipykernel          6.29.4
ipython            8.22.2
ipywidgets         8.1.2
jedi               0.19.1
joblib             1.3.2
jupyter_client     8.6.1
jupyter_core       5.7.2
jupyterlab_widgets 3.0.10
kiwisolver         1.4.5
matplotlib         3.8.3
matplotlib-inline  0.1.6
natsort            8.4.0
nest-asyncio       1.6.0
numpy              1.26.4
packaging          24.0
pandas             2.2.1
parso              0.8.3
patsy              0.5.6
pexpect            4.9.0
pillow             10.2.0
pip                24.0
platformdirs       4.2.0
POT                0.9.3
prompt-toolkit     3.0.43
psutil             5.9.8
ptyprocess         0.7.0
pure-eval          0.2.2
Pygments           2.17.2
pyparsing          3.1.2
python-dateutil    2.9.0.post0
pytz               2024.1
pyzmq              25.1.2
scikit-learn       1.4.1.post1
scipy              1.12.0
setuptools         65.5.0
six                1.16.0
stack-data         0.6.3
statsmodels        0.14.1
threadpoolctl      3.4.0
tornado            6.4
traitlets          5.14.2
tzdata             2024.1
wcwidth            0.2.13
widgetsnbextension 4.0.10
wot                1.0.8.post2
nekonekko commented 7 months ago

I made the following changes based on the error message and it worked, but the output does not seem to be correct. The color of the plot is almost uniform. There may be a problem with the color bar settings.

binned_df['values'] = trajectory_ds[:, name].Xbinned_df['values'] = trajectory_ds[:, name].X.T[0]

image
nekonekko commented 7 months ago

It now works correctly by rewriting it as follows!

binned_df['values'] = trajectory_ds[:, name].Xbinned_df['values'] = trajectory_ds[:, name].X.T[0].tolist()

Since trajectory_ds[:, name].X is a two-dimensional array of the form (165892, 1), it should first be converted into a one-dimensional array using .T[0].

However, because trajectory_ds[:, name].X.T[0] is an ArrayView class, it also needs to be converted into a form that can be assigned to a DataFrame using .tolist().