Skip to content

Commit

Permalink
Use the file IO mechanism in Codebook to write codebooks. (#640)
Browse files Browse the repository at this point in the history
This allows us to update the codebook format without mucking around in code elsewhere.
  • Loading branch information
ttung authored Sep 27, 2018
1 parent f95e502 commit 5388f35
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
6 changes: 4 additions & 2 deletions examples/get_iss_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from skimage.io import imread
from slicedimage import ImageFormat

from starfish import Codebook
from starfish.experiment.builder import FetchedTile, TileFetcher
from starfish.experiment.builder import write_experiment_json
from starfish.types import Coordinates, Features, Indices, Number
Expand Down Expand Up @@ -126,7 +127,7 @@ def add_codebook(experiment_json_doc):
default_shape=SHAPE
)

codebook = [
codebook_array = [
{
Features.CODEWORD: [
{Indices.ROUND.value: 0, Indices.CH.value: 3, Features.CODE_VALUE: 1},
Expand All @@ -146,8 +147,9 @@ def add_codebook(experiment_json_doc):
Features.TARGET: "ACTB_mouse"
},
]
codebook = Codebook.from_code_array(codebook_array)
codebook_json_filename = "codebook.json"
write_json(codebook, os.path.join(output_dir, codebook_json_filename))
codebook.to_json(os.path.join(output_dir, codebook_json_filename))


if __name__ == "__main__":
Expand Down
8 changes: 5 additions & 3 deletions starfish/experiment/builder/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
Writer,
)

from starfish.codebook.codebook import Codebook
from starfish.experiment.version import CURRENT_VERSION
from starfish.types import Coordinates, Indices
from .defaultproviders import RandomNoiseTile, tile_fetcher_factory
Expand Down Expand Up @@ -201,13 +202,14 @@ def write_experiment_json(
with open(os.path.join(path, "experiment.json"), "w") as fh:
json.dump(experiment_doc, fh, indent=4)

codebook_stub = [
codebook_array = [
{
"codeword": [
{"r": 0, "c": 0, "v": 1},
],
"target": "PLEASE_REPLACE_ME"
},
]
with open(os.path.join(path, "codebook.json"), "w") as fh:
json.dump(codebook_stub, fh, indent=4)
codebook = Codebook.from_code_array(codebook_array)
codebook_json_filename = "codebook.json"
codebook.to_json(os.path.join(path, codebook_json_filename))
15 changes: 4 additions & 11 deletions starfish/test/dataset_fixtures.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import json
import os
import shutil
import tempfile
from copy import deepcopy
from typing import Generator
Expand Down Expand Up @@ -97,15 +94,11 @@ def simple_codebook_array():

@pytest.fixture(scope='module')
def simple_codebook_json(simple_codebook_array) -> Generator[str, None, None]:
directory = tempfile.mkdtemp()
codebook_json = os.path.join(directory, 'simple_codebook.json')
with open(codebook_json, 'w') as f:
json.dump(simple_codebook_array, f)

yield codebook_json

shutil.rmtree(directory)
with tempfile.NamedTemporaryFile() as tf:
codebook = Codebook.from_code_array(simple_codebook_array)
codebook.to_json(tf.name)

yield tf.name

@pytest.fixture(scope='module')
def loaded_codebook(simple_codebook_json):
Expand Down

0 comments on commit 5388f35

Please sign in to comment.