Skip to content

Commit

Permalink
TileFetcher formats complete 496 fov MERFISH dataset (#1341)
Browse files Browse the repository at this point in the history
  • Loading branch information
ambrosejcarr authored May 29, 2019
1 parent dd6811f commit bfc5f8d
Showing 1 changed file with 43 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@
"""
import argparse
import functools
import json
import os
from typing import IO, Mapping, Tuple, Union

import numpy as np
import pandas as pd
from skimage.io import imread
from slicedimage import ImageFormat

from starfish.core.util.argparse import FsExistsType
from starfish.experiment.builder import FetchedTile, TileFetcher, write_experiment_json
from starfish.types import Axes, Coordinates, Number
from starfish.util.argparse import FsExistsType

SHAPE = {Axes.Y: 2048, Axes.X: 2048}

Expand All @@ -29,7 +31,7 @@ def cached_read_fn(file_path):


class MERFISHTile(FetchedTile):
def __init__(self, file_path, r, ch):
def __init__(self, file_path, r, ch, coordinates):
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
Expand All @@ -51,41 +53,33 @@ def __init__(self, file_path, r, ch):
(7, 1): 14}
self.r = r
self.ch = ch
self._coordinates = coordinates

@property
def shape(self) -> Mapping[Axes, int]:
return SHAPE

@property
def coordinates(self) -> Mapping[Union[str, Coordinates], Union[Number, Tuple[Number, Number]]]:
# FIXME: (dganguli) please provide proper coordinates here.
return {
Coordinates.X: (0.0, 0.0001),
Coordinates.Y: (0.0, 0.0001),
Coordinates.Z: (0.0, 0.0001),
}
return self._coordinates

def tile_data(self) -> IO:
return cached_read_fn(self.file_path)[self.map[(self.r, self.ch)], :, :]


class MERFISHAuxTile(FetchedTile):
def __init__(self, file_path):
def __init__(self, file_path, coordinates):
self.file_path = file_path
self.dapi_index = 17
self._coordinates = coordinates

@property
def shape(self) -> Mapping[Axes, int]:
return SHAPE

@property
def coordinates(self) -> Mapping[Union[str, Coordinates], Union[Number, Tuple[Number, Number]]]:
# FIXME: (dganguli) please provide proper coordinates here.
return {
Coordinates.X: (0.0, 0.0001),
Coordinates.Y: (0.0, 0.0001),
Coordinates.Z: (0.0, 0.0001),
}
return self._coordinates

def tile_data(self) -> np.ndarray:
return cached_read_fn(self.file_path)[self.dapi_index, :, :]
Expand All @@ -95,19 +89,48 @@ class MERFISHTileFetcher(TileFetcher):
def __init__(self, input_dir, is_dapi):
self.input_dir = input_dir
self.is_dapi = is_dapi
self.coordinates = self.parse_coordinates(input_dir)

@staticmethod
def parse_coordinates(input_dir):
filename = os.path.join(input_dir, "stagePos.csv")
data = pd.read_csv(filename, names=['y_min', 'x_min'])
data['x_max'] = data['x_min'] + 200
data['y_max'] = data['y_min'] + 200
return data

def make_coordinates(self, fov):
return {
Coordinates.X: (
float(self.coordinates.loc[fov, 'x_min']),
float(self.coordinates.loc[fov, 'x_max'])
),
Coordinates.Y: (
float(self.coordinates.loc[fov, 'y_min']),
float(self.coordinates.loc[fov, 'y_max'])
),
Coordinates.Z: (0.0, 0.001)
}

def get_tile(self, fov: int, r: 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)
return MERFISHAuxTile(file_path, self.make_coordinates(fov))
else:
return MERFISHTile(file_path, r, ch)
return MERFISHTile(file_path, r, ch, self.make_coordinates(fov))


def format_data(input_dir, output_dir):
def add_codebook(experiment_json_doc):
experiment_json_doc['codebook'] = "codebook.json"

input_dir = os.path.abspath(input_dir)
output_dir = os.path.abspath(output_dir)

def add_scale_factors(experiment_json_doc):
filename = os.path.join(input_dir, "scale_factors.json")
with open(filename, 'r') as f:
data = json.load(f)
experiment_json_doc['extras'] = {"scale_factors": data}
return experiment_json_doc

num_fovs = 496
Expand Down Expand Up @@ -135,7 +158,7 @@ def add_codebook(experiment_json_doc):
aux_tile_fetcher={
'nuclei': MERFISHTileFetcher(input_dir, is_dapi=True),
},
postprocess_func=add_codebook,
postprocess_func=add_scale_factors,
default_shape=SHAPE
)

Expand Down

0 comments on commit bfc5f8d

Please sign in to comment.