def transfer_physical_coords_from_imagestack_to_intensity_table(image_stack: ImageStack, intensity_table: IntensityTable ) -> IntensityTable: """ Transfers physical coordinates from an Imagestack's coordinates xarray to an intensity table 1. Creates three new coords on the intensity table (xc, yc, zc) 2. For every spot: - Get pixel x,y values - Calculate the physical x,y values - Assign those values to the coords arrays for this spot """ # Add three new coords to xarray (xc, yc, zc) num_features = intensity_table.sizes[Features.AXIS] intensity_table[Coordinates.X.value] = xr.DataArray(np.zeros(num_features, np.float32), dims='features') intensity_table[Coordinates.Y.value] = xr.DataArray(np.zeros(num_features, np.float32), dims='features') intensity_table[Coordinates.Z.value] = xr.DataArray(np.zeros(num_features, np.float32), dims='features') for ind, spot in intensity_table.groupby(Features.AXIS): # Iterate through r, ch per spot for ch, _round in np.ndindex(spot.data.shape): # if non zero value set coords if spot[ch][_round].data == 0: continue # get pixel coords of this tile pixel_x = spot.coords[Axes.X].data pixel_y = spot.coords[Axes.Y].data pixel_z = spot.coords[Axes.ZPLANE].data tile_indices = { Axes.ROUND.value: _round, Axes.CH.value: ch, Axes.ZPLANE.value: pixel_z, } # Get corresponding physical coordinates physical_coords = get_physical_coordinates_of_spot( image_stack._coordinates, tile_indices, pixel_x, pixel_y, image_stack._tile_shape) # Assign to coordinates arrays intensity_table[Coordinates.X.value][ind] = physical_coords[0] intensity_table[Coordinates.Y.value][ind] = physical_coords[1] intensity_table[Coordinates.Z.value][ind] = physical_coords[2] break return intensity_table
def _calculate_mean_pixel_traces( label_image: np.ndarray, intensities: IntensityTable, ) -> IntensityTable: """ For all pixels that contribute to a connected component, calculate the mean value for each (ch, round), producing an average "trace" of a feature across the imaging experiment Parameters ---------- label_image : np.ndarray An image where all pixels of a connected component share the same integer ID intensities : IntensityTable decoded intensities Returns ------- IntensityTable : an IntensityTable where the number of features equals the number of connected components and the intensities of each each feature is its mean trace. """ pixel_labels = label_image.reshape(-1) intensities['spot_id'] = (Features.AXIS, pixel_labels) mean_pixel_traces = intensities.groupby('spot_id').mean(Features.AXIS) mean_distances = intensities[Features.DISTANCE].groupby('spot_id').mean(Features.AXIS) mean_pixel_traces[Features.DISTANCE] = ( 'spot_id', np.ravel(mean_distances) ) # the 0th pixel trace corresponds to background. If present, drop it. try: mean_pixel_traces = mean_pixel_traces.drop(0, dim='spot_id') except KeyError: pass return mean_pixel_traces