def show(self, figsize=(20, 20)): plt.figure(figsize=figsize) ax = plt.gca() image(self.blobs, ax=ax) blobs_log = self.spots_df_viz.loc[:, ['x', 'y', 'r']].values for blob in blobs_log: x, y, r = blob c = plt.Circle((y, x), r, color='r', linewidth=2, fill=False) ax.add_patch(c) plt.title('Num blobs: {}'.format(len(blobs_log))) plt.show()
def show(self, figsize=(10, 10)): plt.figure(figsize=figsize) plt.subplot(121) image(self.blobs_binary, size=10, ax=plt.gca()) plt.subplot(122) regions = self.regions image(regions.mask(background=[0.9, 0.9, 0.9], dims=self.labels.shape, stroke=None, cmap='rainbow'), size=10, ax=plt.gca())
def animate(mv, name): clim = 5*percentile(mv, 90) img = mv[mv.shape[0]/2,:,:] nframes = mv.shape[0]-dt fig = plt.figure(figsize=[12, 12.0*img.shape[0]/img.shape[1]]) fig.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=None, hspace=None) ax = plt.gca() im = image(img, clim=(0, clim), ax=ax) if meta is not None: time = ax.text(.97*img.shape[1], .04*img.shape[0], '%.1f s' % 0, color='white', fontsize=22, ha='right', fontdict={'family': 'monospace'}); ax.plot([.04*img.shape[1], .04*img.shape[1]+scale*ppum], [.94*img.shape[0], .94*img.shape[0]], 'w', lw=2); sclae = ax.text(.04*img.shape[1]+scale*ppum/2, .97*img.shape[0], '%d um' % scale, color='white', fontsize=22, ha='center', fontdict={'family': 'monospace'}); plt.xlim([0, img.shape[1]]); plt.ylim([img.shape[0], 0]); def update(f): im.set_array(mv[dt/2+f]) if meta is not None: time.set_text('%.1f s' % ((dt/2+f)/rate)) ani = animation.FuncAnimation(fig, update, nframes, blit=False) ani.save(join(output, name), writer=writer)
def test_image(): im = random.randn(25, 25) image(im)
# at least we can do it with get_slice and set_slice right now. for indices in s.image._iter_indices(): data = s.image.get_slice(indices)[0] scaled = data / scale_factors[indices[Indices.HYB], indices[Indices.CH]] s.image.set_slice(indices, scaled) # EPY: END code # EPY: START code from scipy.stats import scoreatpercentile # EPY: END code # EPY: START code mp = s.image.max_proj(Indices.HYB, Indices.CH, Indices.Z) clim = scoreatpercentile(mp, [0.5, 99.5]) image(mp, clim=clim) # EPY: END code # EPY: START markdown # ## Use spot-detector to create 'encoder' table for standardized input to decoder # # Each pipeline exposes a spot detector, and this spot detector translates the filtered image into an encoded table by detecting spots. The table contains the spot_id, the corresponding intensity (val) and the channel (ch), hybridization round (hyb), and bit position (bit) of each spot. # # The MERFISH pipeline merges these two steps together by finding pixel-based features, and then later collapsing these into spots and filtering out undesirable (non-spot) features. # # Therefore, no encoder table is generated, but a robust SpotAttribute and DecodedTable are both produced: # EPY: END markdown # EPY: START markdown # ## Decode #
def image_lims(im, num_std, bar=True, size=20): stats = im_describe(im) lim = stats['mean'] + num_std * stats['std'] image(im, bar=bar, size=size, clim=[0, lim])
# EPY: END markdown # EPY: START code from starfish.io import Stack # replace <output directory> with where you saved the formatted data to with the above script in_json = '<output directory>/org.json' s = Stack() s.read(in_json) tile(s.squeeze(), size=10) # EPY: END code # EPY: START code image(s.aux_dict['dots'], size=10) # EPY: END code # EPY: START markdown # ## Register # EPY: END markdown # EPY: START code from starfish.registration._fourier_shift import compute_shift, shift_im upsample = 1000 mp = s.max_proj('ch') res = np.zeros(s.image.shape) for h in range(s.image.num_hybs):
# EPY: END code # EPY: START code bandpass = Filter.Bandpass(lshort=0.5, llong=7, threshold=None, truncate=4) bandpass.run(primary_image, verbose=True, in_place=True) # EPY: END code # EPY: START markdown #To see the image, one needs to make the image quite big, but there is definitely signal present. You might need to clean your monitor to differentiate the spots from dust specs. # EPY: END markdown # EPY: START code from showit import image image(primary_image.get_slice({ Indices.CH.value: 0, Indices.Z.value: 17 })[0][0, :, :], size=20, clim=(0, 0.004)) # EPY: END code # EPY: START markdown #For bandpass, there's a point where things get weird, at `c == 0; z <= 14`. In that range the images look mostly like noise. However, _above_ that, they look great + background subtracted! The later stages of the pipeline appear robust to this, though, as no spots are called for the noisy sections. # EPY: END markdown # EPY: START code # I wasn't sure if this clipping was supposed to be by volume or tile. I've done tile here, but it can be switched clip = Filter.Clip(p_min=10, p_max=100, is_volume=False) clip.run(primary_image, verbose=True, in_place=True) # EPY: END code # EPY: START code
# EPY: END markdown # EPY: START code from starfish.io import Stack # replace <output directory> with where you saved the formatted data to with the above script in_json = '<output directory>/org.json' s = Stack() s.read(in_json) tile(s.image.squeeze(), size=10); # EPY: END code # EPY: START code image(s.auxiliary_images['dots'], size=10) # EPY: END code # EPY: START markdown # ## Register # EPY: END markdown # EPY: START code from starfish.pipeline.registration import Registration registration = Registration.fourier_shift(upsampling=1000) registration.register(s) tile(s.image.squeeze(), size=10); # EPY: END code
def show(self, figsize=(10, 10)): import matplotlib.pyplot as plt plt.figure(figsize=figsize) plt.subplot(321) image(self.dapi, ax=plt.gca(), size=20, bar=True) plt.title('DAPI') plt.subplot(322) image(self.stain, ax=plt.gca(), size=20, bar=True) plt.title('Stain') plt.subplot(323) image(self.dapi_thresholded, bar=False, ax=plt.gca()) plt.title('DAPI Thresholded') plt.subplot(324) image(self.mask, bar=False, ax=plt.gca()) plt.title('Watershed Mask') plt.subplot(325) marker_regions = label_to_regions(self.markers) im = marker_regions.mask(background=[0.9, 0.9, 0.9], dims=self.markers.shape, stroke=None, cmap='rainbow') image(im, size=20, ax=plt.gca()) plt.title('Found: {} cells'.format(self.num_cells)) plt.subplot(326) segmented_regions = label_to_regions(self.segmented) im = segmented_regions.mask(background=[0.9, 0.9, 0.9], dims=self.segmented.shape, stroke=None, cmap='rainbow') image(im, size=20, ax=plt.gca()) plt.title('Segmented Cells') return plt.gca()
def show(self, figsize=(10, 10)): import matplotlib.pyplot as plt plt.figure(figsize=figsize) plt.subplot(321) image(self.nuclei, ax=plt.gca(), size=20, bar=True) plt.title('Nuclei') plt.subplot(322) image(self.stain, ax=plt.gca(), size=20, bar=True) plt.title('Stain') plt.subplot(323) image(self.nuclei_thresholded, bar=False, ax=plt.gca()) plt.title('Nuclei Thresholded') plt.subplot(324) image(self.mask, bar=False, ax=plt.gca()) plt.title('Watershed Mask') plt.subplot(325) image(self.markers, size=20, cmap=plt.cm.nipy_spectral, ax=plt.gca()) plt.title('Found: {} cells'.format(self.num_cells)) plt.subplot(326) image(self.segmented, size=20, cmap=plt.cm.nipy_spectral, ax=plt.gca()) plt.title('Segmented Cells') return plt.gca()
import numpy as np from fakearray import calcium_imaging from neurocatcher import data_train data,series,truth=calcium_imaging(shape=(100,100), n=12, t=10, withparams=True) data=data*255/data.max() data=np.transpose(data,(1,2,0)) data=[data] batch_data,batch_truth=data_train(data,[truth],10,20,20-6) for i,pic in enumerate(batch_data): image(np.mean(pic,axis=2)) plot.show() image(batch_truth[i,:,:,0]) plot.show() ##train a network and check how it performs import neurocatcher as nc import fakearray as fa from showit import image import numpy as np import matplotlib.pyplot as plot # generate some faux calcium imaging data
# round, channel, x, y, z primary_image.xarray.shape # EPY: END code # EPY: START markdown ### Show auxiliary images captured during the experiment # EPY: END markdown # EPY: START markdown #'dots' is a general stain for all possible transcripts. This image should correspond to the maximum projcection of all color channels within a single imaging round. This auxiliary image is useful for registering images from multiple imaging rounds to this reference image. We'll see an example of this further on in the notebook # EPY: END markdown # EPY: START code dots_mp = dots.max_proj(Axes.ROUND, Axes.CH, Axes.ZPLANE) dots_mp_numpy = dots._squeezed_numpy(Axes.ROUND, Axes.CH, Axes.ZPLANE) image(dots_mp_numpy) # EPY: END code # EPY: START markdown #Below is a DAPI auxiliary image, which specifically marks nuclei. This is useful cell segmentation later on in the processing. # EPY: END markdown # EPY: START code nuclei_mp = nuclei.max_proj(Axes.ROUND, Axes.CH, Axes.ZPLANE) nuclei_mp_numpy = nuclei_mp._squeezed_numpy(Axes.ROUND, Axes.CH, Axes.ZPLANE) image(nuclei_mp_numpy) # EPY: END code # EPY: START markdown ### Examine the codebook # EPY: END markdown
import thunder as td from regional import one, many import numpy as np import matplotlib.pyplot as plot from showit import image from fakearray import calcium_imaging from cnmf import CNMF data, series, truth = calcium_imaging(n=5, t=10, seed=42, noise=0.5, withparams=True) base = data.mean(0) image(base, size=10) plot.show() algorithm = CNMF(k=5, gSig=[4, 4], merge_thresh=0.8) model, temporaldata = algorithm.fit(data) def convert(array): r, c = np.where(array > 0.0) return one(zip(r, c)) regions = many([convert(model[:, :, i]) for i in range(model.shape[2])]) #show true solution
from neurocatcher import data_train data, series, truth = calcium_imaging(shape=(100, 100), n=12, t=10, withparams=True) data = data * 255 / data.max() data = np.transpose(data, (1, 2, 0)) data = [data] batch_data, batch_truth = data_train(data, [truth], 10, 20, 20 - 6) for i, pic in enumerate(batch_data): image(np.mean(pic, axis=2)) plot.show() image(batch_truth[i, :, :, 0]) plot.show() ##train a network and check how it performs import neurocatcher as nc import fakearray as fa from showit import image import numpy as np import matplotlib.pyplot as plot # generate some faux calcium imaging data data, series, truth = fa.calcium_imaging(withparams=True)
# EPY: START code # round, channel, x, y, z primary_image.xarray.shape # EPY: END code # EPY: START markdown ### Show auxiliary images captured during the experiment # EPY: END markdown # EPY: START markdown #'dots' is a general stain for all possible transcripts. This image should correspond to the maximum projcection of all color channels within a single imaging round. This auxiliary image is useful for registering images from multiple imaging rounds to this reference image. We'll see an example of this further on in the notebook # EPY: END markdown # EPY: START code image(dots.max_proj(Indices.ROUND, Indices.CH, Indices.Z)) # EPY: END code # EPY: START markdown #Below is a DAPI auxiliary image, which specifically marks nuclei. This is useful cell segmentation later on in the processing. # EPY: END markdown # EPY: START code image(nuclei.max_proj(Indices.ROUND, Indices.CH, Indices.Z)) # EPY: END code # EPY: START markdown ### Examine the codebook # EPY: END markdown # EPY: START markdown
## fit a model import thunder as td from regional import one, many import numpy as np import matplotlib.pyplot as plot from showit import image from fakearray import calcium_imaging from cnmf import CNMF data, series, truth = calcium_imaging(n=5, t=10, seed=42, noise=0.5, withparams=True) base = data.mean(0) image(base, size=10); plot.show() algorithm = CNMF( k=5, gSig=[4,4], merge_thresh=0.8) model,temporaldata = algorithm.fit(data) def convert(array): r,c = np.where(array > 0.0) return one(zip(r,c)) regions = many([convert(model[:,:,i]) for i in range(model.shape[2])]) #show true solution image(many(truth).mask(dims=data.shape[1:], cmap='rainbow', stroke='black', base=base)); plot.show()
segmenter = Segment.WatershedSegment(connectivity=np.ones((1, 3, 3), dtype=np.bool)) masks = segmenter.run( stain, watershed_markers, watershed_mask, ) import matplotlib.pyplot as plt from showit import image plt.figure(figsize=(10, 10)) plt.subplot(321) nuclei_numpy = nuclei_mp_scaled._squeezed_numpy(Axes.ROUND, Axes.CH, Axes.ZPLANE) image(nuclei_numpy, ax=plt.gca(), size=20, bar=True) plt.title('Nuclei') plt.subplot(322) image( stain._squeezed_numpy(Axes.ROUND, Axes.CH, Axes.ZPLANE), ax=plt.gca(), size=20, bar=True) plt.title('Stain') plt.subplot(323) image( binarized_nuclei.uncropped_mask(0).squeeze(Axes.ZPLANE.value).values, bar=False, ax=plt.gca(), ) plt.title('Nuclei Thresholded')
# EPY: START code # hyb, channel, x, y, z s.image.numpy_array.shape # EPY: END code # EPY: START markdown # ## Show auxiliary images captured during the experiment # EPY: END markdown # EPY: START markdown # 'dots' is a general stain for all possible transcripts. This image should correspond to the maximum projcection of all color channels within a single hybridization round. This auxiliary image is useful for registering images from multiple hybridization rounds to this reference image. We'll see an example of this further on in the notebook # EPY: END markdown # EPY: START code image(s.auxiliary_images['dots'].max_proj(Indices.HYB, Indices.CH, Indices.Z)) # EPY: END code # EPY: START markdown # Below is a DAPI auxiliary image, which specifically marks nuclei. This is useful cell segmentation later on in the processing. # EPY: END markdown # EPY: START code image(s.auxiliary_images['nuclei'].max_proj(Indices.HYB, Indices.CH, Indices.Z)) # EPY: END code # EPY: START markdown # ## Examine the codebook # EPY: END markdown
results = locate(ch1, diameter=3, minmass=250, maxsize=3, separation=5, preprocess=False, percentile=10) results.columns = [ 'y', 'x', 'intensity', 'r', 'eccentricity', 'signal', 'raw_mass', 'ep' ] # EPY: END code # EPY: START code # plot the z-projection image(ch1, size=20, clim=(15, 52)) # draw called spots on top as red circles # scale radius plots the red circle at scale_radius * spot radius s.image._show_spots(results, ax=plt.gca(), scale_radius=7) # EPY: END code # EPY: START markdown # Below spot finding is on the _volumes_ for each channel. This will take about `11m30s` # EPY: END markdown # EPY: START code from starfish.pipeline.features.spots.detector import SpotFinder # I've guessed at these parameters from the allen_smFISH code, but you might want to tweak these a bit. # as you can see, this function takes a while. It will be great to parallelize this. That's also coming,
def show(self, figsize=(10, 10)): import matplotlib.pyplot as plt plt.figure(figsize=figsize) plt.subplot(321) nuclei_numpy = self.nuclei_mp_scaled._squeezed_numpy( Axes.ROUND, Axes.CH, Axes.ZPLANE) image(nuclei_numpy, ax=plt.gca(), size=20, bar=True) plt.title('Nuclei') plt.subplot(322) image(self.stain._squeezed_numpy(Axes.ROUND, Axes.CH, Axes.ZPLANE), ax=plt.gca(), size=20, bar=True) plt.title('Stain') plt.subplot(323) image( self.binarized_nuclei.uncropped_mask(0).squeeze( Axes.ZPLANE.value).values, bar=False, ax=plt.gca(), ) plt.title('Nuclei Thresholded') plt.subplot(324) image( self.mask.to_label_image().xarray.squeeze( Axes.ZPLANE.value).values, bar=False, ax=plt.gca(), ) plt.title('Watershed Mask') plt.subplot(325) image( self.markers.to_label_image().xarray.squeeze( Axes.ZPLANE.value).values, size=20, cmap=plt.cm.nipy_spectral, ax=plt.gca(), ) plt.title('Found: {} cells'.format(self.num_cells)) plt.subplot(326) image( self.segmented.to_label_image().xarray.squeeze( Axes.ZPLANE.value).values, size=20, cmap=plt.cm.nipy_spectral, ax=plt.gca(), ) plt.title('Segmented Cells') return plt.gca()
# generate data from extraction.utils import make_gaussian data = make_gaussian(noise=0.5) # fit a model from extraction import NMF model = NMF().fit(data, chunk_size=(100, 200)) # show estimated sources import matplotlib.pyplot as plt from showit import image image( model.regions.mask((100, 200), fill=None, stroke='deeppink', base=data.mean().toarray() / 2)) plt.show()