Repository: spacetx/starfish

Issue:
ExpressionMatrix methods
After target assignment I created an expression matrix (a 2-dimensional cells x genes tensor). 

<img width="589" alt="Screenshot 2019-06-18 at 19 23 41" src="https://user-images.githubusercontent.com/50377927/59705626-0e0e0a80-91ff-11e9-83cd-04fe9d0f25d2.png">

I need to save it for the following usage with single-cell analysis software packages but ExpressionMatrix methods return these kinds of errors.

matrix.save('expression_matrix')

DataArray.name or Dataset key must be either a string or None for serialization to netCDF files

matrix.save_loom('expression_matrix1')

invalid indexer array, does not have integer dtype: array(<Axes.X: 'x'>, dtype=object)

matrix.save_anndata('expression_matrix2')

invalid indexer array, does not have integer dtype: array(<Axes.X: 'x'>, dtype=object)

Discussion:

Patch:
diff --git a/starfish/core/intensity_table/intensity_table.py b/starfish/core/intensity_table/intensity_table.py
index 8f6ab949..01427ba8 100644
--- a/starfish/core/intensity_table/intensity_table.py
+++ b/starfish/core/intensity_table/intensity_table.py
@@ -522,10 +522,11 @@ class IntensityTable(xr.DataArray):
         counts = grouped.count().iloc[:, 0].unstack().fillna(0)
         if self.has_physical_coords:
             grouped = self.to_features_dataframe().groupby([Features.CELL_ID])[[
-                Axes.X, Axes.Y, Axes.ZPLANE, Coordinates.X, Coordinates.Y, Coordinates.Z]]
+                Axes.X.value, Axes.Y.value, Axes.ZPLANE.value, Coordinates.X.value,
+                Coordinates.Y.value, Coordinates.Z.value]]
         else:
             grouped = self.to_features_dataframe().groupby([Features.CELL_ID])[[
-                Axes.X, Axes.Y, Axes.ZPLANE]]
+                Axes.X.value, Axes.Y.value, Axes.ZPLANE.value]]
         min_ = grouped.min()
         max_ = grouped.max()
         coordinate_df = min_ + (max_ - min_) / 2


Test patch:
diff --git a/starfish/core/expression_matrix/test/test_serialization.py b/starfish/core/expression_matrix/test/test_serialization.py
index e69de29b..4f712444 100644
--- a/starfish/core/expression_matrix/test/test_serialization.py
+++ b/starfish/core/expression_matrix/test/test_serialization.py
@@ -0,0 +1,28 @@
+import random
+
+from starfish import IntensityTable
+from starfish.core.codebook.test.factories import codebook_array_factory
+from starfish.core.types import Features
+
+NUMBER_SPOTS = 10
+
+
+def test_save_expression_matrix():
+
+    codebook = codebook_array_factory()
+
+    intensities = IntensityTable.synthetic_intensities(
+        codebook,
+        num_z=3,
+        height=100,
+        width=100,
+        n_spots=10
+    )
+    # mock out come cell_ids
+    cell_ids = random.sample(range(1, 20), NUMBER_SPOTS)
+    intensities[Features.CELL_ID] = (Features.AXIS, cell_ids)
+
+    expression_matrix = intensities.to_expression_matrix()
+
+    # test all saving methods
+    expression_matrix.save("expression")