Skip to content

Commit

Permalink
Next Release - v0.1.1 (#281)
Browse files Browse the repository at this point in the history
* bumped ark version used to 0.4.2, mibi-bin-tools version to 0.2.4, and toffy version to 0.1.1

* using ark v0.4.4

* updated environment.yml
  • Loading branch information
srivarra authored Nov 3, 2022
1 parent 12c9de4 commit 955b6b5
Show file tree
Hide file tree
Showing 15 changed files with 59 additions and 56 deletions.
4 changes: 2 additions & 2 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ dependencies:
- python=3.8
- pip
- pip:
- git+https://github.com/angelolab/ark-analysis.git@v0.3.2
- git+https://github.com/angelolab/[email protected].3
- git+https://github.com/angelolab/ark-analysis.git@v0.4.4
- git+https://github.com/angelolab/[email protected].4
- jupyter>=1.0.0,<2
- jupyter_contrib_nbextensions>=0.5.1,<1
- jupyterlab>=3.4.3,<4
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
ark-analysis @ git+https://github.com/angelolab/ark-analysis.git@v0.3.2
mibi-bin-tools @ git+https://github.com/angelolab/[email protected].3
ark-analysis @ git+https://github.com/angelolab/ark-analysis.git@v0.4.4
mibi-bin-tools @ git+https://github.com/angelolab/[email protected].4
jupyter>=1.0.0,<2
jupyter_contrib_nbextensions>=0.5.1,<1
jupyterlab>=3.4.3,<4
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from os import path, pardir
from setuptools import setup, find_packages

VERSION = '0.1.0'
VERSION = '0.1.1'

PKG_FOLDER = path.abspath(path.join(__file__, pardir))

Expand Down
2 changes: 1 addition & 1 deletion toffy/image_stitching.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def stitch_images(tiff_out_dir, run_dir=None, channels=None, img_sub_folder=None
for chan in channels:
image_data = load_utils.load_imgs_from_tree(tiff_out_dir, img_sub_folder=img_sub_folder,
fovs=folders, channels=[chan],
max_image_size=max_img_size, dtype='float32')
max_image_size=max_img_size)
stitched = data_utils.stitch_images(image_data, num_cols)
current_img = stitched.loc['stitched_image', :, :, chan].values
fname = os.path.join(stitched_dir, chan + "_stitched.tiff")
Expand Down
17 changes: 9 additions & 8 deletions toffy/image_utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import skimage.io as io
import pathlib
from typing import Dict, Union

import numpy as np
from typing import Union
import skimage.io as io


def save_image(fname: Union[str, pathlib.Path], data: np.ndarray,
compression_level: int = 6) -> None:
def save_image(
fname: Union[str, pathlib.Path], data: np.ndarray, compression_level: int = 8
) -> None:
"""
A thin wrapper around `skimage.io.imsave()`.
Expand All @@ -17,10 +19,9 @@ def save_image(fname: Union[str, pathlib.Path], data: np.ndarray,
increases compression ratio. The range of compress is `[1,9]`. Defaults to 6.
"""
# Compression Config:
plugin_args: dict[str, any] = {
'compress': compression_level,
plugin_args: Dict[str, Union[str, int, Dict]] = {
"compression": "zlib",
"compressionargs": {"level": compression_level},
}
if isinstance(fname, pathlib.Path):
fname: str = fname.as_posix()

io.imsave(fname=fname, arr=data, plugin="tifffile", check_contrast=False, **plugin_args)
37 changes: 22 additions & 15 deletions toffy/image_utils_test.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import skimage.io as io
import numpy as np
from toffy import image_utils
import os
import pytest
import pathlib
from typing import Iterator

import numpy as np
import pytest
import skimage.io as io

from toffy import image_utils


@pytest.fixture(scope="session")
def create_img_data() -> np.ndarray:
def create_img_data() -> Iterator[np.ndarray]:
"""
A Fixture which creates a numpy array for tiff file compression testing.
Returns:
np.ndarray: Returns a randomly generated (1000 x 1000) numpy array.
Iterator[np.ndarray]: Returns a randomly generated (1000 x 1000) numpy array.
"""

# Initialize a new generator - set seed for reproducibility
Expand All @@ -24,24 +27,28 @@ def create_img_data() -> np.ndarray:
yield data


class TestSaveImage():
class TestSaveImage:
@pytest.fixture(autouse=True)
def _setup(self, tmp_path, create_img_data):
self.uncompressed_fname: pathlib.Path = (tmp_path / "test_img.tiff")
self.compressed_fname: pathlib.Path = (tmp_path / "test_img_compressed.tiff")
self.uncompressed_fname: pathlib.Path = tmp_path / "test_img.tiff"
self.compressed_fname: pathlib.Path = tmp_path / "test_img_compressed.tiff"
self.data: np.ndarray = create_img_data

# save uncompressed image
io.imsave(self.uncompressed_fname.as_posix(), arr=self.data, plugin="tifffile",
check_contrast=False)
io.imsave(
self.uncompressed_fname,
arr=self.data,
plugin="tifffile",
check_contrast=False,
)

@pytest.mark.parametrize("compress_level", [1, 6, 9,
pytest.param(10, marks=pytest.mark.xfail)])
def test_save_compressed_img(self, compress_level):

# Fails when compression_level > 9
image_utils.save_image(fname=self.compressed_fname, data=self.data,
compression_level=compress_level)
image_utils.save_image(
fname=self.compressed_fname, data=self.data, compression_level=compress_level
)

# Assert that the compressed tiff file is smaller than the uncompressed tiff file
uncompressed_tiff_file_size: int = os.path.getsize(self.uncompressed_fname)
Expand All @@ -52,7 +59,7 @@ def test_save_compressed_img(self, compress_level):
# Assert that the values in the compressed tiff file and the uncompressed
# tiff file are equal.

uncompressed_data: np.ndarary = io.imread(self.uncompressed_fname)
uncompressed_data: np.ndarray = io.imread(self.uncompressed_fname)
compressed_data: np.ndarray = io.imread(self.compressed_fname)

np.testing.assert_array_equal(compressed_data, uncompressed_data)
4 changes: 2 additions & 2 deletions toffy/json_utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def test_read_json_file():

# Test bad path
bad_path = "/neasdf1246ljea/asdfje12ua3421ndsf/asdf.json"
with pytest.raises(ValueError, match=r'A bad path*'):
with pytest.raises(FileNotFoundError, match=r'A bad path*'):
json_utils.read_json_file(bad_path)

# Read json with read_json_file function assuming file path is good
Expand All @@ -127,7 +127,7 @@ def test_write_json_file():
bad_path = "/mf8575b20d/bgjeidu45483hdck/asdf.json"

# test bad path
with pytest.raises(ValueError, match=r"A bad path*"):
with pytest.raises(FileNotFoundError, match=r"A bad path*"):
json_utils.write_json_file(json_path=bad_path, json_object=moly_json)

# Write file after file path is validated
Expand Down
6 changes: 3 additions & 3 deletions toffy/mph_comp_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_get_estimated_time():
good_fov = 'fov-1-scan-1'

# bad directory path should raise an error
with pytest.raises(ValueError):
with pytest.raises(FileNotFoundError):
mph.get_estimated_time(bad_path, good_fov)

# bad fov name data should raise an error
Expand Down Expand Up @@ -88,7 +88,7 @@ def test_combine_mph_metrics():
bad_path = os.path.join(Path(__file__).parent, "data", "not-a-folder")

# bad directory path should raise an error
with pytest.raises(ValueError):
with pytest.raises(FileNotFoundError):
mph.combine_mph_metrics(bad_path)

data1 = create_sample_mph_data(fov='fov-1', mph_value=1000, total_count=50000, time=500)
Expand Down Expand Up @@ -121,7 +121,7 @@ def test_visualize_mph():
}, index=[0, 1])

# bad output directory path should raise an error
with pytest.raises(ValueError):
with pytest.raises(FileNotFoundError):
mph.visualize_mph(mph_data, bad_path, regression=False)

with tempfile.TemporaryDirectory() as temp_dir:
Expand Down
2 changes: 1 addition & 1 deletion toffy/mph_inspect_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def test_visualize_intensity_data(mocker):
mass_stop = 98.5

# bad path should raise an error
with pytest.raises(ValueError):
with pytest.raises(FileNotFoundError):
mph_inspect.visualize_intensity_data(bad_path, mass, mass_start, mass_stop)

# bad fov name should raise error
Expand Down
2 changes: 1 addition & 1 deletion toffy/normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ def normalize_image_data(img_dir, norm_dir, pulse_height_dir, panel_info,

# get images
images = load_utils.load_imgs_from_tree(img_dir, fovs=[fov], channels=channels,
dtype='float32', img_sub_folder=img_sub_folder)
img_sub_folder=img_sub_folder)

# normalize and save
normalize_fov(img_data=images, norm_vals=norm_vals, norm_dir=norm_dir, fov=fov,
Expand Down
3 changes: 1 addition & 2 deletions toffy/qc_comp.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,7 @@ def compute_qc_metrics(bin_file_path, extracted_imgs_path, fov_name,

# retrieve the image data from extracted tiff files
# the image coords should be: ['fov', 'type', 'x', 'y', 'channel']
image_data = load_utils.load_imgs_from_tree(extracted_imgs_path, fovs=[fov_name],
dtype='float32')
image_data = load_utils.load_imgs_from_tree(extracted_imgs_path, fovs=[fov_name])
image_data = format_img_data(image_data)

metric_csvs = compute_qc_metrics_direct(image_data, fov_name, gaussian_blur, blur_factor)
Expand Down
5 changes: 2 additions & 3 deletions toffy/qc_metrics_plots.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from ark.utils.load_utils import load_imgs_from_tree, load_imgs_from_dir
from ark.utils.load_utils import load_imgs_from_tree
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
Expand All @@ -23,8 +23,7 @@ def call_violin_swarm_plot(plotting_df, fig_label, figsize=(20, 3), fig_dir=None
plt.xticks(rotation=45)
if fig_dir:
plt.savefig(fig_dir+fig_label+"_batch_effects.png", dpi=300)
plt.show()
plt.close()
return ax


def make_batch_effect_plot(data_dir, normal_tissues, exclude_channels=None,
Expand Down
10 changes: 4 additions & 6 deletions toffy/qc_metrics_plots_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from ark.utils.load_utils import load_imgs_from_tree, load_imgs_from_dir
from ark.utils.load_utils import load_imgs_from_tree, load_imgs_from_dir
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
Expand All @@ -22,15 +20,15 @@ def test_call_violin_swarm():

# Test that file is created.
with tempfile.TemporaryDirectory() as temp_dir:
qc_metrics_plots.call_violin_swarm_plot(plotting_df, fig_label="test123", figsize=(20, 3),
fig_dir=temp_dir)
_ = qc_metrics_plots.call_violin_swarm_plot(plotting_df, fig_label="test123",
figsize=(20, 3), fig_dir=temp_dir)
assert os.path.exists(temp_dir+"test123"+"_batch_effects.png")

# Test that no file is created when not passing fig_dir
with tempfile.TemporaryDirectory() as temp_dir:
file_number_before = len(os.listdir(temp_dir))
qc_metrics_plots.call_violin_swarm_plot(plotting_df, fig_label="test123", figsize=(20, 3),
fig_dir=None)
_ = qc_metrics_plots.call_violin_swarm_plot(plotting_df, fig_label="test123",
figsize=(20, 3), fig_dir=None)
file_number_after = len(os.listdir(temp_dir))
assert file_number_before == file_number_after

Expand Down
9 changes: 4 additions & 5 deletions toffy/rosetta.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@
import skimage.io as io
from scipy.ndimage import gaussian_filter

from ark.utils.load_utils import load_imgs_from_tree, load_imgs_from_dir
from ark.utils.load_utils import load_imgs_from_tree
from ark.utils.io_utils import list_folders, validate_paths, list_files, remove_file_extensions
from ark.utils.misc_utils import verify_same_elements, verify_in_list
from toffy.image_utils import save_image

from toffy.streak_detection import streak_correction
from toffy.json_utils import read_json_file
from toffy import image_stitching


def transform_compensation_json(json_path, comp_mat_path):
Expand Down Expand Up @@ -111,7 +110,7 @@ def validate_inputs(raw_data_dir, comp_mat, acquired_masses, acquired_targets, i
verify_same_elements(acquired_masses=acquired_masses, compensation_masses=all_masses)

# check first FOV to make sure all channels are present
test_data = load_imgs_from_tree(data_dir=raw_data_dir, fovs=fovs[0:1], dtype='float32',
test_data = load_imgs_from_tree(data_dir=raw_data_dir, fovs=fovs[0:1],
img_sub_folder=raw_data_sub_folder)

verify_in_list(listed_channels=acquired_targets, image_files=test_data.channels.values)
Expand Down Expand Up @@ -247,7 +246,7 @@ def compensate_image_data(raw_data_dir, comp_data_dir, comp_mat_path, panel_info
# load batch of fovs
batch_fovs = fovs[i: i + batch_size]
batch_data = load_imgs_from_tree(data_dir=raw_data_dir, fovs=batch_fovs,
channels=acquired_targets, dtype='float32',
channels=acquired_targets,
img_sub_folder=raw_data_sub_folder)

# blur data
Expand Down Expand Up @@ -370,7 +369,7 @@ def add_source_channel_to_tiled_image(raw_img_dir, tiled_img_dir, output_dir, so

# load source images
source_imgs = load_imgs_from_tree(raw_img_dir, channels=[source_channel],
dtype='float32', img_sub_folder=img_sub_folder,
img_sub_folder=img_sub_folder,
max_image_size=max_img_size)

# convert stacked images to concatenated row
Expand Down
8 changes: 4 additions & 4 deletions toffy/rosetta_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def test_compensate_image_data(output_masses, input_masses, gaus_rad, save_forma
# make fake data for testing
fovs, chans = test_utils.gen_fov_chan_names(num_fovs=3, num_chans=3)
filelocs, data_xr = test_utils.create_paired_xarray_fovs(
data_dir, fovs, chans, img_shape=(10, 10), fills=True)
data_dir, fovs, chans, img_shape=(10, 10), fills=True, dtype=np.float64)
os.makedirs(os.path.join(data_dir, 'stitched_images'))

# create compensation matrix
Expand Down Expand Up @@ -467,7 +467,7 @@ def test_copy_image_files(mocker):
rosetta.copy_image_files('cohort_name', ['bad_name'], temp_dir2, temp_dir)

# bad paths should raise an error
with pytest.raises(ValueError, match='could not be found'):
with pytest.raises(FileNotFoundError, match='could not be found'):
rosetta.copy_image_files('cohort_name', run_names, 'bad_path', temp_dir)
rosetta.copy_image_files('cohort_name', run_names, temp_dir2, 'bad_path')

Expand Down Expand Up @@ -502,7 +502,7 @@ def test_rescale_raw_imgs():
img_data = load_imgs_from_tree(temp_dir)

# bad extracted img path should raise an error
with pytest.raises(ValueError):
with pytest.raises(FileNotFoundError):
rosetta.rescale_raw_imgs('bad_path')

# test successful image saving
Expand Down Expand Up @@ -558,7 +558,7 @@ def test_generate_rosetta_test_imgs(mocker):

with tempfile.TemporaryDirectory() as temp_dir:
# bad paths should raise error
with pytest.raises(ValueError):
with pytest.raises(FileNotFoundError):
rosetta.generate_rosetta_test_imgs('bad_path', temp_img_dir, mults,
temp_dir, panel)
rosetta.generate_rosetta_test_imgs(rosetta_mat_path, 'bad_path', mults,
Expand Down

0 comments on commit 955b6b5

Please sign in to comment.