Skip to content

Commit

Permalink
Update dependencies (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
ludvb authored May 23, 2021
1 parent 32126e2 commit 5ac2333
Show file tree
Hide file tree
Showing 13 changed files with 703 additions and 643 deletions.
1,222 changes: 639 additions & 583 deletions poetry.lock

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@ numpy = "^1.19.4"
opencv-python = "^4.4.0"
pandas = "^1.1.4"
Pillow = "^8.0.1"
pyro-ppl = "^1.5.0"
pyro-ppl = ">=1.5.0,<1.6.0"
python = "^3.8"
scikit-learn = "^0.23.2"
scikit-learn = "^0.24.2"
scipy = "^1.5.4"
tensorboard = "^2.3.0"
tensorboard = "^2.5.0"
tifffile = "^2020.10.1"
tomlkit = "^0.7.0"
torch = "^1.7.0"
torchvision = "^0.8.1"
torch = "^1.8.1"
torchvision = "^0.9.1"
tqdm = "^4.51.0"
tabulate = "^0.8.7"

[tool.poetry.dev-dependencies]
mypy = "^0.790"
mypy = "^0.812"
pre-commit = "^2.8.2"
pylint = "^2.6.0"
pylint = "^2.8.2"
pytest = "^6.1.2"
pytest-console-scripts = "^1.0.0"
pytest-cov = "^2.10.1"
Expand Down
2 changes: 1 addition & 1 deletion scripts/sync_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
with open(project_file, "r") as fp:
project_config = tomlkit.loads(fp.read())

version = project_config["tool"]["poetry"]["version"]
version = project_config["tool"]["poetry"]["version"] # type: ignore

with open(
os.path.join(os.path.dirname(project_file), "xfuse", "__version__.py"), "w"
Expand Down
8 changes: 5 additions & 3 deletions xfuse/analyze/gene_maps.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import itertools as it
import re
import warnings
from typing import Any, Callable, Dict, Iterable, Tuple
from typing import Any, Callable, Dict, Iterable, Tuple, cast

import numpy as np
import torch
Expand Down Expand Up @@ -86,7 +86,9 @@ def _process_batch(samples):
for gene, gene_data in zip(
samples[0]["colnames"], data.permute(3, 0, 1, 2)
):
yield samples[0]["section"], gene, gene_data
yield samples[0]["section"], gene, cast(
np.ndarray, gene_data.cpu().numpy()
)

with Progressbar(
chunks_of(genes, genes_per_batch),
Expand Down Expand Up @@ -206,7 +208,7 @@ def _save_tensor(gene, samples, tissue_mask):
except KeyError:
tissue_mask = None
with chdir(slide_name):
write(gene, samples.numpy(), tissue_mask, **writer_args)
write(gene, samples, tissue_mask, **writer_args)


_register_analysis(
Expand Down
9 changes: 6 additions & 3 deletions xfuse/convert/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ def run(
}

if mask:
mask = compute_tissue_mask(image)
label = np.array(mask == 0, dtype=np.int16)
tissue_mask = compute_tissue_mask(image)
label = np.array(tissue_mask == 0, dtype=np.int16)
else:
label = np.zeros(image.shape[:2], dtype=np.int16)

Expand All @@ -52,7 +52,10 @@ def run(
image,
label,
type_label="ST",
annotation=annotation,
annotation={
k: (v, {x: str(x) for x in np.unique(v)})
for k, v in annotation.items()
},
auto_rotate=rotate,
path=output_file,
)
5 changes: 4 additions & 1 deletion xfuse/convert/st.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ def run(
image,
label,
type_label="ST",
annotation=annotation,
annotation={
k: (v, {x: str(x) for x in np.unique(v)})
for k, v in annotation.items()
},
auto_rotate=rotate,
path=output_file,
)
9 changes: 4 additions & 5 deletions xfuse/convert/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,11 @@ def rescale(
:param resample: Resampling filter
:returns: The rescaled image
"""
image = Image.fromarray(image)
image = image.resize(
[round(x * scaling_factor) for x in image.size], resample=resample,
image_pil = Image.fromarray(image)
image_pil = image_pil.resize(
[round(x * scaling_factor) for x in image_pil.size], resample=resample,
)
image = np.array(image)
return image
return np.array(image_pil)


def labels_from_spots(dst: np.ndarray, spots: List[Spot]) -> None:
Expand Down
5 changes: 4 additions & 1 deletion xfuse/convert/visium.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ def run(
image,
label,
type_label="ST",
annotation=annotation,
annotation={
k: (v, {x: str(x) for x in np.unique(v)})
for k, v in annotation.items()
},
auto_rotate=rotate,
path=output_file,
)
26 changes: 14 additions & 12 deletions xfuse/data/slide/data/annotated_image.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import annotations
from typing import Dict, List, Optional
from typing import Dict, List, Optional, Union

import numpy as np
import torch
Expand All @@ -25,22 +25,23 @@ def __init__(
self.image = image
self.label = annotation
self.name = name
self.label_names = label_names
self.set_label_names(label_names)

@property
def label_names(self) -> np.ndarray:
"""Names corresponding to the integer labels in :attr:`label`"""
return self._label_names_array

@label_names.setter
def label_names(self, x: Dict[int, str]):
self._label_names = x
self._label_names_array = np.full(
max(self._label_names.keys()) + 1, "", dtype="object"
)
self._label_names_array[list(self._label_names.keys())] = list(
self._label_names.values()
)
def set_label_names(self, x: Union[np.ndarray, Dict[int, str]]) -> None:
"""Sets the label names"""
if isinstance(x, np.ndarray):
label_names = x
elif isinstance(x, dict):
label_names = np.full(max(x.keys()) + 1, "", dtype="object")
label_names[list(x.keys())] = list(x.values())
else:
raise ValueError(f"Unsupported {type(x)=}")
self._label_names_array = label_names

@property
def data_type(self) -> str:
Expand All @@ -52,7 +53,8 @@ def genes(self) -> List[str]:

@genes.setter
def genes(self, genes: List[str]) -> AnnotatedImage:
pass
# pylint: disable=unused-argument
return self

@classmethod
def from_st_slide(
Expand Down
8 changes: 2 additions & 6 deletions xfuse/messengers/stats/writer/file.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import gzip
import os
import time
from multiprocessing import Pool
from typing import Dict, Optional
from warnings import warn

Expand All @@ -21,8 +20,7 @@
class FileWriter(StatsWriter):
r"""Stats writer emitting .jpg and .csv.gz files"""

def __init__(self, num_workers: int = 1):
self._worker_pool = Pool(num_workers)
def __init__(self):
self._file_cons: Dict[str, gzip.GzipFile] = {}

def write_histogram(
Expand Down Expand Up @@ -50,9 +48,7 @@ def write_image(self, tag: str, img_tensor: torch.Tensor) -> None:
img = img_tensor.detach().cpu().numpy()
img = _normalize(img)
img = (255 * img).astype(np.uint8)
self._worker_pool.apply_async(
imwrite, (os.path.abspath(filename), img)
)
imwrite(os.path.abspath(filename), img)

def write_images(self, tag: str, img_tensor: torch.Tensor) -> None:
r"""Logs an image grid"""
Expand Down
26 changes: 11 additions & 15 deletions xfuse/utility/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,10 @@ def center_crop(x: ArrayType, target_shape: Tuple[int, ...]) -> ArrayType:
r"""Crops `x` to the given `target_shape` from the center"""
return x[
tuple(
[
slice(round((a - b) / 2), round((a - b) / 2) + b)
if b is not None
else slice(None)
for a, b in zip(x.shape, target_shape)
]
slice(round((a - b) / 2), round((a - b) / 2) + b)
if b is not None
else slice(None)
for a, b in zip(x.shape, target_shape)
)
]

Expand All @@ -75,12 +73,11 @@ def rescale(
:param resample: Resampling filter
:returns: The rescaled image
"""
image = Image.fromarray(image)
image = image.resize(
[round(x * scaling_factor) for x in image.size], resample=resample,
image_pil = Image.fromarray(image)
image_pil = image_pil.resize(
[round(x * scaling_factor) for x in image_pil.size], resample=resample,
)
image = np.array(image)
return image
return np.array(image_pil)


def resize(
Expand All @@ -96,10 +93,9 @@ def resize(
:param resample: Resampling filter
:returns: The rescaled image
"""
image = Image.fromarray(image)
image = image.resize(target_shape[::-1], resample=resample)
image = np.array(image)
return image
image_pil = Image.fromarray(image)
image_pil = image_pil.resize(target_shape[::-1], resample=resample)
return np.array(image_pil)


def temp_attr(obj: object, attr: str, value: Any) -> ContextManager:
Expand Down
4 changes: 2 additions & 2 deletions xfuse/utility/state/state.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from copy import copy
from typing import Any, Dict, NamedTuple, OrderedDict
from typing import Any, Dict, NamedTuple

import pyro
import torch
Expand All @@ -16,7 +16,7 @@ class Param(NamedTuple):

class StateDict(NamedTuple):
r"""Data structure for the states of modules and non-module parameters"""
modules: Dict[str, OrderedDict[str, torch.Tensor]] # type: ignore
modules: Dict[str, Dict[str, torch.Tensor]] # type: ignore
params: Dict[str, Param]
optimizer: Dict[str, Dict[str, torch.Tensor]]

Expand Down
8 changes: 4 additions & 4 deletions xfuse/utility/visualization.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import warnings
from typing import Callable, Optional, Union, cast
from typing import Callable, Iterable, List, Optional, Tuple, Union, cast

import numpy as np
import pyro
Expand Down Expand Up @@ -109,7 +109,7 @@ def mask_background(

def visualize_metagenes(
method: str = "pca", num_training_samples: Optional[int] = None
) -> np.ndarray:
) -> Iterable[Tuple[str, np.ndarray, List[Tuple[str, np.ndarray]]]]:
r"""Creates visualizations of metagenes"""

# pylint: disable=too-many-locals,too-many-statements
Expand Down Expand Up @@ -284,13 +284,13 @@ def _default_transformation(x):
transformation = _default_transformation

if mask is None:
mask = np.ones(x.shape[:-1], dtype=np.bool)
mask = np.ones(x.shape[:-1], dtype=bool)
elif isinstance(mask, torch.Tensor):
mask = mask.detach().cpu().numpy().astype(bool)
mask = cast(np.ndarray, mask)

if isinstance(x, torch.Tensor):
x = x.detach().cpu().numpy()
x = cast(np.ndarray, x.detach().cpu().numpy())

values = transformation(x[mask])
dst = np.zeros((*mask.shape, values.shape[-1]))
Expand Down

0 comments on commit 5ac2333

Please sign in to comment.