New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Upload example of transforming raw MERFISH -> spaceTx formatted data #478
Merged
Merged
Changes from 3 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
import argparse | ||
import io | ||
import os | ||
from typing import IO, Tuple | ||
|
||
from skimage.io import imread, imsave | ||
from slicedimage import ImageFormat | ||
|
||
from starfish.experiment.builder import FetchedTile, TileFetcher, write_experiment_json | ||
from starfish.types import Indices | ||
from starfish.util.argparse import FsExistsType | ||
|
||
SHAPE = 2048, 2048 | ||
|
||
|
||
class MERFISHTile(FetchedTile): | ||
def __init__(self, file_path, hyb, ch): | ||
self.file_path = file_path | ||
# how to index tiles from indices into multi-page tiff | ||
# key is a tuple of round, chan. val is the index | ||
self.map = {(0, 0): 0, | ||
(0, 1): 1, | ||
(1, 0): 3, | ||
(1, 1): 2, | ||
(2, 0): 4, | ||
(2, 1): 5, | ||
(3, 0): 6, | ||
(3, 1): 7, | ||
(4, 0): 9, | ||
(4, 1): 8, | ||
(5, 0): 10, | ||
(5, 1): 11, | ||
(6, 0): 12, | ||
(6, 1): 13, | ||
(7, 0): 15, | ||
(7, 1): 14} | ||
self.hyb = hyb | ||
self.ch = ch | ||
|
||
@property | ||
def shape(self) -> Tuple[int, ...]: | ||
return SHAPE | ||
|
||
@property | ||
def format(self) -> ImageFormat: | ||
return ImageFormat.TIFF | ||
|
||
def tile_data_handle(self) -> IO: | ||
im = imread(self.file_path) | ||
im = im[self.map[(self.hyb, self.ch)], :, :] | ||
fh = io.BytesIO() | ||
imsave(fh, im, plugin='tifffile') | ||
fh.seek(0) | ||
return fh | ||
|
||
|
||
class MERFISHAuxTile(FetchedTile): | ||
def __init__(self, file_path): | ||
self.file_path = file_path | ||
self.dapi_index = 17 | ||
|
||
@property | ||
def shape(self) -> Tuple[int, ...]: | ||
return SHAPE | ||
|
||
@property | ||
def format(self) -> ImageFormat: | ||
return ImageFormat.TIFF | ||
|
||
def tile_data_handle(self) -> IO: | ||
im = imread(self.file_path)[self.dapi_index, :, :] | ||
fh = io.BytesIO() | ||
imsave(fh, im, plugin='tifffile') | ||
fh.seek(0) | ||
return fh | ||
|
||
|
||
class MERFISHTileFetcher(TileFetcher): | ||
def __init__(self, input_dir, is_dapi): | ||
self.input_dir = input_dir | ||
self.is_dapi = is_dapi | ||
|
||
def get_tile(self, fov: int, hyb: int, ch: int, z: int) -> FetchedTile: | ||
filename = os.path.join(self.input_dir, 'fov_{}.tif'.format(fov)) | ||
file_path = os.path.join(self.input_dir, filename) | ||
if self.is_dapi: | ||
return MERFISHAuxTile(file_path) | ||
else: | ||
return MERFISHTile(file_path, hyb, ch) | ||
|
||
|
||
def format_data(input_dir, output_dir): | ||
def add_codebook(experiment_json_doc): | ||
experiment_json_doc['codebook'] = "codebook.json" | ||
return experiment_json_doc | ||
|
||
num_fovs = 1 | ||
|
||
hyb_dimensions = { | ||
Indices.ROUND: 8, | ||
Indices.CH: 2, | ||
Indices.Z: 1, | ||
} | ||
|
||
aux_name_to_dimensions = { | ||
'nuclei': { | ||
Indices.ROUND: 1, | ||
Indices.CH: 1, | ||
Indices.Z: 1 | ||
} | ||
} | ||
|
||
write_experiment_json(output_dir, | ||
num_fovs, | ||
hyb_dimensions, | ||
aux_name_to_dimensions, | ||
primary_tile_fetcher=MERFISHTileFetcher(input_dir, is_dapi=False), | ||
aux_tile_fetcher={ | ||
'nuclei': MERFISHTileFetcher(input_dir, is_dapi=True), | ||
}, | ||
postprocess_func=add_codebook, | ||
default_shape=SHAPE | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
s3_bucket = "s3://czi.starfish.data.public/browse/raw/20180820/merfish_u2os/" | ||
input_help_msg = "Path to raw data. Raw data can be downloaded from: {}".format(s3_bucket) | ||
output_help_msg = "Path to output experment.json and all formatted images it references" | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("input_dir", type=FsExistsType(), help=input_help_msg) | ||
parser.add_argument("output_dir", type=FsExistsType(), help=output_help_msg) | ||
|
||
args = parser.parse_args() | ||
format_data(args.input_dir, args.output_dir) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to make sure I'm clear on what this means:
# maps information on the acquisition channel and round to an index that identifies the correct page in the original multi-page tiff file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so each fov_00x.tiff loads into numpy as a 18X2048X2048 array. that first axis corresponds to some round and color channel. the map dictates how to index into that first axis based on the round/channel you want.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
16 rounds + dapi + ___ ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
failed attempt at a membrane stain
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks.