Skip to content
Snippets Groups Projects
Commit 881f1fde authored by Christopher Randolph Rhodes's avatar Christopher Randolph Rhodes
Browse files

Added and covered PatchStack dataframe with summary stats

parent 0f3f110c
No related branches found
No related tags found
No related merge requests found
......@@ -492,16 +492,22 @@ class PatchStack(InMemoryDataAccessor):
def shape_dict(self):
return dict(zip(('P', 'Y', 'X', 'C', 'Z'), self.data.shape))
@property
def df(self):
def get_object_df(self, mask) -> pd.DataFrame:
"""
Given a mask patch stack of the same size, return a DataFrame summarizing the area and intensity of objects,
assuming the each patch in the patch stack represents a single object.
:param mask of the same dimensions
"""
if self.shape != mask.shape or not mask.is_mask():
raise DataShapeError(f'Patch stack object dataframe expects a mask of the same dimensions')
df = pd.DataFrame([
{
'label': i,
'area': (self.iat(i).data > 0).sum(),
'sum': self.iat(i).data.sum()
'area': (mask.iat(i).data > 0).sum(),
'intensity_sum': (self.iat(i).data * (mask.iat(i).data > 0)).sum()
} for i in range(0, self.count)
])
df['intensity_mean'] = df['sum'] / df['area']
df['intensity_mean'] = df['intensity_sum'] / df['area']
return df
......
......@@ -303,8 +303,21 @@ class TestPatchStackAccessor(unittest.TestCase):
self.assertEqual(acc.get_mono(channel=0).data_yxz.shape, (n, h, w, nz))
self.assertEqual(acc.get_mono(channel=0, mip=True).data_yx.shape, (n, h, w))
def test_object_df(self):
w = 30
h = 20
n = 2
nz = 3
nc = 3
acc = PatchStack(_random_int(n, h, w, nc, nz))
mask_data= np.zeros((n, h, w, nc, nz), dtype='uint8')
mask_data[0, 0:5, 0:5, :, :] = 255
mask_data[1, 0:10, 0:10, :, :] = 255
mask = PatchStack(mask_data)
df = acc.get_object_df(mask)
# intensity means are centered around half of full range
self.assertTrue(np.all(((acc.df['intensity_mean'] / acc.dtype_max) - 0.5)**2 < 1e-3))
self.assertTrue(np.all(((df['intensity_mean'] / acc.dtype_max) - 0.5)**2 < 1e-3))
self.assertTrue(df['area'][1] / df['area'][0] == 4.0)
return acc
def test_get_one_channel(self):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment