vitessce / vitessce-python

Python API and Jupyter widget for Vitessce
https://python-docs.vitessce.io
MIT License
40 stars 7 forks source link

Minimal example for obsSegmentations.json for visualizing polygons in Spatial View #310

Open J-Yash opened 10 months ago

J-Yash commented 10 months ago

Hi, I'm trying to visualize polygons (given in a JSON file similar to the example at http://vitessce.io/docs/data-file-types/#obssegmentationsjson) in the Spatial view but am unable to do so. I get a blank screen in the widget (in Jupyter notebook) and get an error message in the status window ("Error while validating undefined.")

Here is the code I use to create this:

vc = VitessceConfig(schema_version="1.0.15", name='Segmentations test config', description='Test config')

dataset = vc.add_dataset(name='Segmentation-json-example').add_file(
                                                                url="path-to-file-on-aws-s3/obsSegmentations-example.json",
                                                                data_type=DataType.OBS_SEGMENTATIONS,
                                                                file_type=ft.OBS_SEGMENTATIONS_JSON,

)

spatial = vc.add_view(cm.SPATIAL, dataset=dataset)
status = vc.add_view(cm.STATUS, dataset=dataset)
lc = vc.add_view(cm.LAYER_CONTROLLER, dataset=dataset)
vc.layout(spatial | (lc / status));
vc.display()

The obsSegmentations-example.json file has cell polygons defined based on the example in the documentation (the first link above):

{
    "cell_1": [
      [6668, 26182],
      [6668, 26296],
      [6873, 26501],
      [6932, 26501],
      [6955, 26478],
      [6955, 26260],
      [6838, 26143],
      [6707, 26143]
    ],
    "cell_2": [
      [5047, 44428],
      [5047, 44553],
      [5065, 44571],
      [5125, 44571],
      [5284, 44412],
      [5284, 44368],
      [5239, 44323],
      [5152, 44323]
    ]
  }

Could you please provide a minimal example of how to use a JSON file to visualize the polygons?

keller-mark commented 10 months ago

Hi @J-Yash Can you try removing the data_type parameter in the add_file?

This is a minimal example of how the config would look as JSON

{
  "name": "Codeluppi et al., Nature Methods 2018",
  "description": "Spatial organization of the somatosensory cortex revealed by osmFISH",
  "version": "1.0.15",
  "initStrategy": "auto",
  "datasets": [
    {
      "uid": "codeluppi",
      "name": "Codeluppi",
      "files": [
        {
          "fileType": "obsSegmentations.json",
          "url": "https://s3.amazonaws.com/vitessce-data/0.0.33/main/codeluppi-2018/codeluppi_2018_nature_methods.cells.segmentations.json"
        }
      ]
    }
  ],
  "coordinationSpace": {
    "spatialSegmentationLayer": {
      "A": {
        "opacity": 1,
        "radius": 0,
        "visible": true,
        "stroked": false
      }
    }
  },
  "layout": [
    {
      "component": "layerController",
      "coordinationScopes": {
        "spatialSegmentationLayer": "A"
      },
      "x": 0,
      "y": 1,
      "w": 2,
      "h": 4
    },
    {
      "component": "spatial",
      "coordinationScopes": {
        "spatialSegmentationLayer": "A"
      },
      "x": 2,
      "y": 0,
      "w": 4,
      "h": 4
    }
  ]
}

You may need to add the line

vc.link_views([spatial, lc], ["spatialSegmentationLayer"], [{ "opacity": 1, "radius": 0, "visible": True, "stroked": False }]
J-Yash commented 10 months ago

Awesome! That worked. Thanks for the quick response! On a separate note, is it also possible to set the color of the polygons to a specific color?