Skip to content

Default value for img_data should contain expected column names #45

Description

@jkanche

Currently the img_data slot creates an empty dataframe when no such information is provided, ref. This creates issues when an image is added to the object.

MRE on v0.0.7

nrows = 200
ncols = 500
counts = np.random.rand(nrows, ncols)
tspe = SpatialExperiment(assays={"spots": counts})

tspe.add_img(
    image_source="spatialexperiment/tests/images/sample_image4.png",
    scale_factor=1,
    sample_id="sample_2",
    image_id="unsplash",
)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[6], line 6
      3 counts = np.random.rand(nrows, ncols)
      4 tspe = SpatialExperiment(assays={"spots": counts})
----> 6 tspe.add_img(
      7     image_source="spatialexperiment[/tests/images/sample_image4.png](http://localhost:8888/tests/images/sample_image4.png)",
      8     scale_factor=1,
      9     sample_id="sample_2",
     10     image_id="unsplash",
     11 )

File [/opt/homebrew/Caskroom/miniforge/base/envs/biocpy/lib/python3.12/site-packages/spatialexperiment/SpatialExperiment.py:868](http://localhost:8888/opt/homebrew/Caskroom/miniforge/base/envs/biocpy/lib/python3.12/site-packages/spatialexperiment/SpatialExperiment.py#line=867), in SpatialExperiment.add_img(self, image_source, scale_factor, sample_id, image_id, load, in_place)
    858     spi = construct_spatial_image_class(image_source, is_url=False)
    860 new_row = BiocFrame(
    861     {
    862         "sample_id": [sample_id],
   (...)
    866     }
    867 )
--> 868 new_img_data = self._img_data.combine_rows(new_row)
    870 output = self._define_output(in_place)
    871 output._img_data = new_img_data

File [/opt/homebrew/Caskroom/miniforge/base/envs/biocpy/lib/python3.12/site-packages/biocframe/BiocFrame.py:1406](http://localhost:8888/opt/homebrew/Caskroom/miniforge/base/envs/biocpy/lib/python3.12/site-packages/biocframe/BiocFrame.py#line=1405), in BiocFrame.combine_rows(self, *other)
   1404 def combine_rows(self, *other):
   1405     """Wrapper around :py:func:`~biocutils.combine_rows`."""
-> 1406     return _combine_rows_bframes(self, *other)

File [/opt/homebrew/Caskroom/miniforge/base/envs/biocpy/lib/python3.12/site-packages/biocframe/BiocFrame.py:1445](http://localhost:8888/opt/homebrew/Caskroom/miniforge/base/envs/biocpy/lib/python3.12/site-packages/biocframe/BiocFrame.py#line=1444), in _combine_rows_bframes(*x)
   1443         has_rownames = True
   1444     if df.shape[1] != first_nc:
-> 1445         raise ValueError(
   1446             "All objects to combine must have the same number of columns (expected "
   1447             + str(first_nc)
   1448             + ", got "
   1449             + str(df.shape[1])
   1450             + ")."
   1451         )
   1453 new_data = {}
   1454 for i, col in enumerate(x[0]._column_names):

ValueError: All objects to combine must have the same number of columns (expected 0, got 4).

a test run from SFE: https://github.com/BiocPy/SpatialFeatureExperiment/actions/runs/14633318187/job/41059515714?pr=5

Fix: I forced it to set defaults and it works:

from biocframe import BiocFrame
tspe._img_data = BiocFrame(data={"sample_id":[], "image_id":[], "data":[], "scale_factor":[]}, number_of_rows=0, column_names=["sample_id", "image_id", "data", "scale_factor"])
tspe.add_img(
    image_source="spatialexperiment/tests/images/sample_image4.png",
    scale_factor=1,
    sample_id="sample_2",
    image_id="unsplash",
)
print(tspe)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions