Skip to content

Commit a77581b

Browse files
committed
Update kpmp-2023 nb
1 parent 6cbdedf commit a77581b

File tree

2 files changed

+105
-52
lines changed

2 files changed

+105
-52
lines changed

docs/notebooks/spatial_data_kpmp.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,37 @@ def clean_adata(adata):
5959
shapes_key = "shapes" + labels_key[labels_key.index("_"):]
6060
sdata.shapes[shapes_key] = to_polygons(sdata.labels[labels_key])
6161

62+
# Convert boolean columns to set names
63+
ptc_obs = sdata.tables['table_peritubular_capillaries'].obs
64+
ptc_obs['PTC_in_Cortex'] = sdata.tables['table_peritubular_capillaries'].X[:,2]
65+
ptc_obs['PTC_in_IFTA'] = sdata.tables['table_peritubular_capillaries'].X[:,3]
66+
67+
def row_to_cortex_name(row):
68+
if row['PTC_in_Cortex'] == 1.0:
69+
return "Inside Cortex"
70+
if row['PTC_in_Cortex'] == 0.0:
71+
return "Outside Cortex"
72+
73+
def row_to_ifta_name(row):
74+
if row['PTC_in_IFTA'] == 1.0:
75+
return "IFTA"
76+
if row['PTC_in_IFTA'] == 0.0:
77+
return "non-IFTA"
78+
79+
def row_to_cortical_ifta_name(row):
80+
if row['PTC_in_Cortex'] == 1.0 and row['PTC_in_IFTA'] == 1.0:
81+
return "Cortical IFTA"
82+
if row['PTC_in_Cortex'] == 1.0 and row['PTC_in_IFTA'] == 0.0:
83+
return "Cortical non-IFTA"
84+
if row['PTC_in_Cortex'] == 0.0:
85+
return "Outside Cortex"
86+
87+
ptc_obs['cortex_set'] = ptc_obs.apply(row_to_cortex_name, axis='columns')
88+
ptc_obs['ifta_set'] = ptc_obs.apply(row_to_ifta_name, axis='columns')
89+
ptc_obs['cortex_ifta_set'] = ptc_obs.apply(row_to_cortical_ifta_name, axis='columns')
90+
91+
sdata.tables['table_peritubular_capillaries'].obs = ptc_obs
92+
6293
sdata.write(join(base_dir, "sdata.zarr"), overwrite=True)
6394
print(join(base_dir, "S-1905-017737.sdata.zarr"))
6495
print("Done")

docs/notebooks/spatial_data_kpmp_vis.ipynb

Lines changed: 74 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,6 @@
9595
" table_path=\"tables/table_tubules\",\n",
9696
" labels_path=\"labels/labels_tubules\",\n",
9797
" obs_feature_matrix_path=\"tables/table_tubules/X\",\n",
98-
" #obs_feature_matrix_path=\"tables/table/X\",\n",
99-
" #obs_set_paths=[\"tables/table/obs/annotation\"],\n",
100-
" #obs_set_names=[\"Annotation\"],\n",
101-
" #region=\"tubules\",\n",
10298
" coordinate_system=\"global\",\n",
10399
" coordination_values={\n",
104100
" \"fileUid\": \"labels_tubules\",\n",
@@ -111,10 +107,6 @@
111107
" SpatialDataWrapper(\n",
112108
" sdata_url=sdata_url,\n",
113109
" labels_path=\"labels/labels_arteries_arterioles\",\n",
114-
" #obs_feature_matrix_path=\"tables/table/X\",\n",
115-
" #obs_set_paths=[\"tables/table/obs/annotation\"],\n",
116-
" #obs_set_names=[\"Annotation\"],\n",
117-
" #region=\"tubules\",\n",
118110
" coordinate_system=\"global\",\n",
119111
" coordination_values={\n",
120112
" \"fileUid\": \"labels_arteries_arterioles\",\n",
@@ -129,11 +121,6 @@
129121
" table_path=\"tables/table_cortical_interstitia\",\n",
130122
" labels_path=\"labels/labels_cortical_interstitia\",\n",
131123
" obs_feature_matrix_path=\"tables/table_cortical_interstitia/X\",\n",
132-
" \n",
133-
" #obs_feature_matrix_path=\"tables/table/X\",\n",
134-
" #obs_set_paths=[\"tables/table/obs/annotation\"],\n",
135-
" #obs_set_names=[\"Annotation\"],\n",
136-
" #region=\"tubules\",\n",
137124
" coordinate_system=\"global\",\n",
138125
" coordination_values={\n",
139126
" \"fileUid\": \"labels_cortical_interstitia\",\n",
@@ -148,10 +135,6 @@
148135
" table_path=\"tables/table_globally_sclerotic_glomeruli\",\n",
149136
" labels_path=\"labels/labels_globally_sclerotic_glomeruli\",\n",
150137
" obs_feature_matrix_path=\"tables/table_globally_sclerotic_glomeruli/X\",\n",
151-
" #obs_feature_matrix_path=\"tables/table/X\",\n",
152-
" #obs_set_paths=[\"tables/table/obs/annotation\"],\n",
153-
" #obs_set_names=[\"Annotation\"],\n",
154-
" #region=\"tubules\",\n",
155138
" coordinate_system=\"global\",\n",
156139
" coordination_values={\n",
157140
" \"fileUid\": \"labels_globally_sclerotic_glomeruli\",\n",
@@ -166,10 +149,6 @@
166149
" table_path=\"tables/table_non_globally_sclerotic_glomeruli\",\n",
167150
" labels_path=\"labels/labels_non_globally_sclerotic_glomeruli\",\n",
168151
" obs_feature_matrix_path=\"tables/table_non_globally_sclerotic_glomeruli/X\",\n",
169-
" #obs_feature_matrix_path=\"tables/table/X\",\n",
170-
" #obs_set_paths=[\"tables/table/obs/annotation\"],\n",
171-
" #obs_set_names=[\"Annotation\"],\n",
172-
" #region=\"tubules\",\n",
173152
" coordinate_system=\"global\",\n",
174153
" coordination_values={\n",
175154
" \"fileUid\": \"labels_non_globally_sclerotic_glomeruli\",\n",
@@ -183,10 +162,6 @@
183162
" sdata_url=sdata_url,\n",
184163
" table_path=\"tables/table_interstitialfibrosis_and_tubular_atrophy\",\n",
185164
" labels_path=\"labels/labels_interstitialfibrosis_and_tubular_atrophy\",\n",
186-
" #obs_feature_matrix_path=\"tables/table/X\",\n",
187-
" #obs_set_paths=[\"tables/table/obs/annotation\"],\n",
188-
" #obs_set_names=[\"Annotation\"],\n",
189-
" #region=\"tubules\",\n",
190165
" coordinate_system=\"global\",\n",
191166
" coordination_values={\n",
192167
" \"fileUid\": \"labels_interstitialfibrosis_and_tubular_atrophy\",\n",
@@ -201,10 +176,18 @@
201176
" table_path=\"tables/table_peritubular_capillaries\",\n",
202177
" labels_path=\"labels/labels_peritubular_capillaries\",\n",
203178
" obs_feature_matrix_path=\"tables/table_peritubular_capillaries/X\",\n",
204-
" #obs_feature_matrix_path=\"tables/table/X\",\n",
205-
" #obs_set_paths=[\"tables/table/obs/annotation\"],\n",
206-
" #obs_set_names=[\"Annotation\"],\n",
207-
" #region=\"tubules\",\n",
179+
" obs_set_paths=[\n",
180+
" \"tables/table_peritubular_capillaries/obs/cortex_ifta_set\",\n",
181+
" \"tables/table_peritubular_capillaries/obs/cortex_set\",\n",
182+
" \"tables/table_peritubular_capillaries/obs/ifta_set\",\n",
183+
" [\"tables/table_peritubular_capillaries/obs/cortex_set\", \"tables/table_peritubular_capillaries/obs/ifta_set\"],\n",
184+
" ],\n",
185+
" obs_set_names=[\n",
186+
" \"Cortex and IFTA membership\",\n",
187+
" \"Cortex membership\",\n",
188+
" \"IFTA membership\",\n",
189+
" \"Cortex and IFTA hierarchy\",\n",
190+
" ],\n",
208191
" coordinate_system=\"global\",\n",
209192
" coordination_values={\n",
210193
" \"fileUid\": \"labels_peritubular_capillaries\",\n",
@@ -231,6 +214,12 @@
231214
"ngsg_feature_list = vc.add_view(\"featureList\", dataset=dataset).set_props(title=\"Non-Globally Sclerotic Glomeruli\")\n",
232215
"ngsg_histogram = vc.add_view(\"featureValueHistogram\", dataset=dataset)\n",
233216
"\n",
217+
"# Add obsSets, obsSetSizes, and violin plot views for PTC sets+areas/aspectRatio\n",
218+
"pt_sets = vc.add_view(\"obsSets\", dataset=dataset)\n",
219+
"pt_bar_plot = vc.add_view(\"obsSetSizes\", dataset=dataset)\n",
220+
"pt_violin_plot = vc.add_view(\"obsSetFeatureValueDistribution\", dataset=dataset).set_props(jitter=True)\n",
221+
"\n",
222+
"\n",
234223
"# Coordination of views.\n",
235224
"[ft_scope, fvt_scope] = vc.add_coordination(\"featureType\", \"featureValueType\")\n",
236225
"ft_scope.set_value(\"feature\")\n",
@@ -242,7 +231,8 @@
242231
"\n",
243232
"[pt_ot_scope, pt_fs_scope, pt_oce_scope] = vc.add_coordination(\"obsType\", \"featureSelection\", \"obsColorEncoding\")\n",
244233
"pt_ot_scope.set_value(ptc_obstype)\n",
245-
"pt_oce_scope.set_value(\"spatialChannelColor\")\n",
234+
"pt_fs_scope.set_value([\"Area\"])\n",
235+
"pt_oce_scope.set_value(\"cellSetSelection\")\n",
246236
"\n",
247237
"\n",
248238
"[gsg_ot_scope, gsg_fs_scope, gsg_oce_scope, gsg_fvcr_scope] = vc.add_coordination(\"obsType\", \"featureSelection\", \"obsColorEncoding\", \"featureValueColormapRange\")\n",
@@ -257,6 +247,7 @@
257247
"\n",
258248
"vc.link_views_by_dict([spatial, layer_controller], {\n",
259249
" \"imageLayer\": CL([{\n",
250+
" \"spatialLayerOpacity\": 0.1,\n",
260251
" \"photometricInterpretation\": \"RGB\",\n",
261252
" }]),\n",
262253
"}, meta=True, scope_prefix=get_initial_coordination_scope_prefix(\"A\", \"image\"))\n",
@@ -271,12 +262,13 @@
271262
" \"featureType\": ft_scope,\n",
272263
" \"featureValueType\": fvt_scope,\n",
273264
" \"featureSelection\": t_fs_scope,\n",
274-
" \"spatialChannelVisible\": True,\n",
275-
" \"spatialChannelColor\": [255, 255, 255],\n",
265+
" \"spatialChannelVisible\": False,\n",
266+
" \"spatialChannelColor\": [73, 155, 119],\n",
276267
" \"spatialChannelOpacity\": 0.5,\n",
277268
" \"obsColorEncoding\": t_oce_scope,\n",
278269
" \"featureValueColormapRange\": [0, 1],\n",
279270
" \"featureAggregationStrategy\": \"first\",\n",
271+
" \"spatialSegmentationFilled\": True,\n",
280272
" \"obsHighlight\": None,\n",
281273
" }]),\n",
282274
" },\n",
@@ -287,12 +279,13 @@
287279
" \"obsType\": \"Artery\",\n",
288280
" \"featureType\": ft_scope,\n",
289281
" \"featureValueType\": fvt_scope,\n",
290-
" \"spatialChannelVisible\": True,\n",
291-
" \"spatialChannelColor\": [255, 255, 255],\n",
282+
" \"spatialChannelVisible\": False,\n",
283+
" \"spatialChannelColor\": [237, 226, 107],\n",
292284
" \"spatialChannelOpacity\": 0.5,\n",
293285
" \"obsColorEncoding\": \"spatialChannelColor\",\n",
294286
" \"featureValueColormapRange\": [0, 1],\n",
295287
" \"featureAggregationStrategy\": \"first\",\n",
288+
" \"spatialSegmentationFilled\": True,\n",
296289
" \"obsHighlight\": None,\n",
297290
" }]),\n",
298291
" },\n",
@@ -309,6 +302,7 @@
309302
" \"obsColorEncoding\": \"spatialChannelColor\",\n",
310303
" \"featureValueColormapRange\": [0, 1],\n",
311304
" \"featureAggregationStrategy\": \"first\",\n",
305+
" \"spatialSegmentationFilled\": True,\n",
312306
" \"obsHighlight\": None,\n",
313307
" }]),\n",
314308
" },\n",
@@ -321,11 +315,12 @@
321315
" \"featureValueType\": fvt_scope,\n",
322316
" \"featureSelection\": gsg_fs_scope,\n",
323317
" \"spatialChannelVisible\": False,\n",
324-
" \"spatialChannelColor\": [255, 255, 255],\n",
318+
" \"spatialChannelColor\": [52, 113, 171],\n",
325319
" \"spatialChannelOpacity\": 0.5,\n",
326320
" \"obsColorEncoding\": gsg_oce_scope,\n",
327321
" \"featureValueColormapRange\": gsg_fvcr_scope,\n",
328322
" \"featureAggregationStrategy\": \"first\",\n",
323+
" \"spatialSegmentationFilled\": True,\n",
329324
" \"obsHighlight\": None,\n",
330325
" }]),\n",
331326
" },\n",
@@ -338,11 +333,12 @@
338333
" \"featureValueType\": fvt_scope,\n",
339334
" \"featureSelection\": ngsg_fs_scope,\n",
340335
" \"spatialChannelVisible\": False,\n",
341-
" \"spatialChannelColor\": [255, 255, 255],\n",
336+
" \"spatialChannelColor\": [114, 179, 226],\n",
342337
" \"spatialChannelOpacity\": 0.5,\n",
343338
" \"obsColorEncoding\": ngsg_oce_scope,\n",
344339
" \"featureValueColormapRange\": ngsg_fvcr_scope,\n",
345340
" \"featureAggregationStrategy\": \"first\",\n",
341+
" \"spatialSegmentationFilled\": True,\n",
346342
" \"obsHighlight\": None,\n",
347343
" }]),\n",
348344
" },\n",
@@ -353,12 +349,13 @@
353349
" \"obsType\": \"Interstitial Fibrosis and Tubular Atrophy\",\n",
354350
" \"featureType\": ft_scope,\n",
355351
" \"featureValueType\": fvt_scope,\n",
356-
" \"spatialChannelVisible\": False,\n",
357-
" \"spatialChannelColor\": [255, 255, 255],\n",
358-
" \"spatialChannelOpacity\": 0.5,\n",
352+
" \"spatialChannelVisible\": True,\n",
353+
" \"spatialChannelColor\": [218, 161, 66],\n",
354+
" \"spatialChannelOpacity\": 1.0,\n",
359355
" \"obsColorEncoding\": \"spatialChannelColor\",\n",
360356
" \"featureValueColormapRange\": [0, 1],\n",
361357
" \"featureAggregationStrategy\": \"first\",\n",
358+
" \"spatialSegmentationFilled\": False,\n",
362359
" \"obsHighlight\": None,\n",
363360
" }]),\n",
364361
" },\n",
@@ -370,12 +367,13 @@
370367
" \"featureType\": ft_scope,\n",
371368
" \"featureValueType\": fvt_scope,\n",
372369
" \"featureSelection\": pt_fs_scope,\n",
373-
" \"spatialChannelVisible\": False,\n",
374-
" \"spatialChannelColor\": [255, 255, 255],\n",
375-
" \"spatialChannelOpacity\": 0.5,\n",
370+
" \"spatialChannelVisible\": True,\n",
371+
" \"spatialChannelColor\": [197, 101, 47],\n",
372+
" \"spatialChannelOpacity\": 1.0,\n",
376373
" \"obsColorEncoding\": pt_oce_scope,\n",
377-
" \"featureValueColormapRange\": [0, 1],\n",
374+
" \"featureValueColormapRange\": [0, 0.5],\n",
378375
" \"featureAggregationStrategy\": \"first\",\n",
376+
" \"spatialSegmentationFilled\": True,\n",
379377
" \"obsHighlight\": None,\n",
380378
" }]),\n",
381379
" }\n",
@@ -388,6 +386,11 @@
388386
"pt_feature_list.use_coordination(pt_ot_scope, ft_scope, fvt_scope, pt_fs_scope, pt_oce_scope)\n",
389387
"pt_histogram.use_coordination(pt_ot_scope, ft_scope, fvt_scope, pt_fs_scope, pt_oce_scope)\n",
390388
"\n",
389+
"pt_sets.use_coordination(pt_ot_scope, ft_scope, fvt_scope, pt_fs_scope, pt_oce_scope)\n",
390+
"pt_bar_plot.use_coordination(pt_ot_scope, ft_scope, fvt_scope, pt_fs_scope, pt_oce_scope)\n",
391+
"pt_violin_plot.use_coordination(pt_ot_scope, ft_scope, fvt_scope, pt_fs_scope, pt_oce_scope)\n",
392+
"\n",
393+
"\n",
391394
"gsg_feature_list.use_coordination(gsg_ot_scope, ft_scope, fvt_scope, gsg_fs_scope, gsg_oce_scope, gsg_fvcr_scope)\n",
392395
"gsg_histogram.use_coordination(gsg_ot_scope, ft_scope, fvt_scope, gsg_fs_scope, gsg_oce_scope, gsg_fvcr_scope)\n",
393396
"\n",
@@ -396,26 +399,45 @@
396399
"\n",
397400
"\n",
398401
"# Layout the views in a grid arrangement.\n",
399-
"vc.layout(vconcat(hconcat(spatial, layer_controller, split=[3, 1]), hconcat(\n",
400-
" (tubules_feature_list / tubules_histogram),\n",
401-
" (pt_feature_list / pt_histogram),\n",
402-
" (gsg_feature_list / gsg_histogram),\n",
403-
" (ngsg_feature_list / ngsg_histogram)\n",
404-
")));"
402+
"vc.layout(vconcat(\n",
403+
" hconcat(spatial, layer_controller, split=[3, 1]),\n",
404+
" hconcat(\n",
405+
" (tubules_feature_list / tubules_histogram),\n",
406+
" (pt_feature_list / pt_histogram),\n",
407+
" (gsg_feature_list / gsg_histogram),\n",
408+
" (ngsg_feature_list / ngsg_histogram)\n",
409+
" ),\n",
410+
" hconcat(pt_sets, pt_bar_plot, pt_violin_plot)\n",
411+
"));"
405412
]
406413
},
407414
{
408415
"cell_type": "code",
409416
"execution_count": null,
410-
"metadata": {
411-
"tags": []
412-
},
417+
"metadata": {},
413418
"outputs": [],
414419
"source": [
415420
"vw = vc.widget(height=1000)\n",
416421
"vw"
417422
]
418423
},
424+
{
425+
"cell_type": "code",
426+
"execution_count": null,
427+
"metadata": {},
428+
"outputs": [],
429+
"source": [
430+
"#import json\n",
431+
"#print(json.dumps(vc.to_dict(), indent=2))"
432+
]
433+
},
434+
{
435+
"cell_type": "code",
436+
"execution_count": null,
437+
"metadata": {},
438+
"outputs": [],
439+
"source": []
440+
},
419441
{
420442
"cell_type": "code",
421443
"execution_count": null,

0 commit comments

Comments
 (0)