Skip to content

Commit

Permalink
chore: remove deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
tlambert03 committed Feb 19, 2024
1 parent d5fa29a commit c20c43a
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 109 deletions.
18 changes: 0 additions & 18 deletions src/nd2/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import math
import re
import warnings
from datetime import datetime, timezone
from itertools import product
from typing import TYPE_CHECKING, BinaryIO, NamedTuple, cast
Expand All @@ -13,7 +12,6 @@

from typing_extensions import Final

from nd2.readers import ND2Reader
from nd2.structures import ExpLoop

StrOrPath = Union[str, PathLike]
Expand Down Expand Up @@ -77,19 +75,6 @@ def is_legacy(path: StrOrPath) -> bool:
return fh.read(4) == OLD_HEADER_MAGIC


def get_reader(
path: str, validate_frames: bool = False, search_window: int = 100
) -> ND2Reader: # pragma: no cover
warnings.warn(
"Deprecated, use nd2.readers.ND2Reader.create if you want to "
"directly instantiate a reader subclass.",
stacklevel=2,
)
from nd2.readers import ND2Reader

return ND2Reader.create(path, search_window * 1000 if validate_frames else None)


def is_new_format(path: str) -> bool:
# TODO: this is just for dealing with missing test data
with open(path, "rb") as fh:
Expand Down Expand Up @@ -159,9 +144,6 @@ def parse_time(time_str: str) -> datetime:
raise ValueError(f"Could not parse {time_str}") # pragma: no cover


# utils for converting records to dicts, in recorded_data method


def convert_records_to_dict_of_lists(
records: ListOfDicts, null_val: Any = float("nan")
) -> DictOfLists:
Expand Down
83 changes: 13 additions & 70 deletions src/nd2/nd2file.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,6 @@ class ND2File:
search_window : int
When validate_frames is true, this is the search window (in KB) that will
be used to try to find the actual chunk position. by default 100 KB
read_using_sdk : Optional[bool]
:warning: **DEPRECATED**. No longer does anything.
If `True`, use the SDK to read the file. If `False`, inspects the chunkmap
and reads from a `numpy.memmap`. If `None` (the default), uses the SDK if
the file is compressed, otherwise uses the memmap. Note: using
`read_using_sdk=False` on a compressed file will result in a ValueError.
"""

def __init__(
Expand All @@ -100,15 +93,7 @@ def __init__(
*,
validate_frames: bool = False,
search_window: int = 100,
read_using_sdk: bool | None = None,
) -> None:
if read_using_sdk is not None:
warnings.warn(
"The `read_using_sdk` argument is deprecated and will be removed in "
"a future version.",
FutureWarning,
stacklevel=2,
)
self._error_radius: int | None = (
search_window * 1000 if validate_frames else None
)
Expand Down Expand Up @@ -343,17 +328,16 @@ def experiment(self) -> list[ExpLoop]:
@overload
def events(
self, *, orient: Literal["records"] = ..., null_value: Any = ...
) -> ListOfDicts: ...
) -> ListOfDicts:
...

@overload
def events(
self, *, orient: Literal["list"], null_value: Any = ...
) -> DictOfLists: ...
def events(self, *, orient: Literal["list"], null_value: Any = ...) -> DictOfLists:
...

@overload
def events(
self, *, orient: Literal["dict"], null_value: Any = ...
) -> DictOfDicts: ...
def events(self, *, orient: Literal["dict"], null_value: Any = ...) -> DictOfDicts:
...

def events(
self,
Expand Down Expand Up @@ -406,7 +390,6 @@ def unstructured_metadata(
strip_prefix: bool = True,
include: set[str] | None = None,
exclude: set[str] | None = None,
unnest: bool | None = None,
) -> dict[str, Any]:
"""Exposes, and attempts to decode, each metadata chunk in the file.
Expand All @@ -431,8 +414,6 @@ def unstructured_metadata(
all metadata sections found in the file are included.
exclude : set[str] | None, optional
If provided, exclude the specified keys from the output. by default `None`
unnest : bool, optional
:warning: **DEPRECATED**. No longer does anything.
Returns
-------
Expand All @@ -441,12 +422,6 @@ def unstructured_metadata(
metadata chunk (things like 'CustomData|RoiMetadata_v1' or
'ImageMetadataLV'), and values that are associated metadata chunk.
"""
if unnest is not None:
warnings.warn(
"The unnest parameter is deprecated, and no longer has any effect.",
FutureWarning,
stacklevel=2,
)
return self._rdr.unstructured_metadata(strip_prefix, include, exclude)

@cached_property
Expand Down Expand Up @@ -1157,27 +1132,6 @@ def __repr__(self) -> str:
extra = ""
return f"<ND2File at {hex(id(self))}{extra}>"

@property
def recorded_data(self) -> DictOfLists:
"""Return tabular data recorded for each frame of the experiment.
!!! warning "Deprecated"
This method is deprecated and will be removed in a future version.
Please use the [`events`][nd2.ND2File.events] method instead. To get the
same dict-of-lists output that `recorded_data` returns, use
`ndfile.events(orient='list')`
"""
warnings.warn(
"recorded_data is deprecated and will be removed in a future version."
"Please use the `events` method instead. To get the same dict-of-lists "
"output, use `events(orient='list')`",
FutureWarning,
stacklevel=2,
)

return self.events(orient="list")

@cached_property
def binary_data(self) -> BinaryLayers | None:
"""Return binary layers embedded in the file.
Expand Down Expand Up @@ -1235,8 +1189,8 @@ def imread(
dask: Literal[False] = ...,
xarray: Literal[False] = ...,
validate_frames: bool = ...,
read_using_sdk: bool | None = None,
) -> np.ndarray: ...
) -> np.ndarray:
...


@overload
Expand All @@ -1246,8 +1200,8 @@ def imread(
dask: bool = ...,
xarray: Literal[True],
validate_frames: bool = ...,
read_using_sdk: bool | None = None,
) -> xr.DataArray: ...
) -> xr.DataArray:
...


@overload
Expand All @@ -1257,8 +1211,8 @@ def imread(
dask: Literal[True],
xarray: Literal[False] = ...,
validate_frames: bool = ...,
read_using_sdk: bool | None = None,
) -> dask.array.core.Array: ...
) -> dask.array.core.Array:
...


def imread(
Expand All @@ -1267,7 +1221,6 @@ def imread(
dask: bool = False,
xarray: bool = False,
validate_frames: bool = False,
read_using_sdk: bool | None = None,
) -> np.ndarray | xr.DataArray | dask.array.core.Array:
"""Open `file`, return requested array type, and close `file`.
Expand All @@ -1290,23 +1243,13 @@ def imread(
shifted relative to the predicted offset (i.e. in a corrupted file).
This comes at a slight performance penalty at file open, but may "rescue"
some corrupt files. by default False.
read_using_sdk : Optional[bool]
:warning: **DEPRECATED**. No longer used.
If `True`, use the SDK to read the file. If `False`, inspects the chunkmap and
reads from a `numpy.memmap`. If `None` (the default), uses the SDK if the file
is compressed, otherwise uses the memmap.
Note: using `read_using_sdk=False` on a compressed file will result in a
ValueError.
Returns
-------
Union[np.ndarray, dask.array.Array, xarray.DataArray]
Array subclass, depending on arguments used.
"""
with ND2File(
file, validate_frames=validate_frames, read_using_sdk=read_using_sdk
) as nd2:
with ND2File(file, validate_frames=validate_frames) as nd2:
if xarray:
return nd2.to_xarray(delayed=dask)
elif dask:
Expand Down
2 changes: 1 addition & 1 deletion src/nd2/readers/_legacy/legacy_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ def header(self) -> dict:

def events(self, orient: str, null_value: Any) -> list | Mapping:
warnings.warn(
"`recorded_data` is not implemented for legacy ND2 files",
"`events` is not implemented for legacy ND2 files",
UserWarning,
stacklevel=2,
)
Expand Down
14 changes: 0 additions & 14 deletions tests/test_deprecations.py

This file was deleted.

9 changes: 3 additions & 6 deletions tests/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,11 @@ def test_metadata_extraction(new_nd2: Path) -> None:

assert isinstance(nd.unstructured_metadata(), dict)
assert isinstance(nd.events(), list)
with pytest.warns(FutureWarning):
assert isinstance(nd.recorded_data, dict)

assert nd.closed


def test_metadata_extraction_legacy(old_nd2):
def test_metadata_extraction_legacy(old_nd2) -> None:
assert ND2File.is_supported_file(old_nd2)
with ND2File(old_nd2) as nd:
assert repr(nd)
Expand All @@ -121,12 +119,11 @@ def test_metadata_extraction_legacy(old_nd2):
assert nd.closed


def test_recorded_data() -> None:
def test_events() -> None:
# this method is smoke-tested for every file above...
# but specific values are asserted here:
with ND2File(DATA / "cluster.nd2") as f:
with pytest.warns(FutureWarning, match="deprecated"):
rd = f.recorded_data
rd = f.events(orient="list")

headers = list(rd)
row_0 = [rd[h][0] for h in headers]
Expand Down

0 comments on commit c20c43a

Please sign in to comment.