Skip to content

Commit

Permalink
Merge pull request #147 from henrypinkard/main
Browse files Browse the repository at this point in the history
New major version of ndtiff python package
  • Loading branch information
henrypinkard authored Jun 13, 2024
2 parents f50f669 + 187bc20 commit 6f4a5ee
Show file tree
Hide file tree
Showing 11 changed files with 906 additions and 401 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,29 @@ from ndtiff import Dataset
data = Dataset('/path/to/data')
```

or to write a new dataset:

```python
import numpy as np
from ndtiff import NDTiffDataset

summary_metadata = {'name_1': 123, 'name_2': 'something else'} # make this whatever you want
dataset = NDTiffDataset('your/data/path', summary_metadata=summary_metadata, writable=True)

image_height = 256
image_width = 256

for time in range(10):
# generate random image
pixels = np.random.randint(0, 2 ** 16, (image_height, image_width), dtype=np.uint16)

image_coordinates = {'time': time, 'other_axis_name': 4} # this can be whatever you want
image_metadata = {'name_1': 123, 'name_2': 'something'} # whatever you want
dataset.put_image(image_coordinates, pixels, image_metadata)

dataset.finish()
```

## Rationale
The NDTiff library is optimized for size, speed, and flexibility. Several optimizations are in place to achieve high-performance in each category.

Expand Down
2 changes: 2 additions & 0 deletions python/ndtiff/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
name = "ndtiff"

from ndtiff.ndstorage_base import NDStorageAPI, WritableNDStorageAPI
from ndtiff._superclass import Dataset
from ndtiff.ndtiff_dataset import NDTiffDataset
from ndtiff.ndram_dataset import NDRAMDataset
from ndtiff.ndtiff_pyramid_dataset import NDTiffPyramidDataset
2 changes: 1 addition & 1 deletion python/ndtiff/_version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version_info = (2, 3, 0)
version_info = (3, 1, 0)
__version__ = ".".join(map(str, version_info))
67 changes: 67 additions & 0 deletions python/ndtiff/ndram_dataset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from typing import List, Dict, Union

import threading
from ndtiff.ndstorage_base import WritableNDStorageAPI, NDStorageBase


class NDRAMDataset(NDStorageBase, WritableNDStorageAPI):
"""
A class for holding data in RAM
Implements the methods needed to be a DataSink for AcqEngPy
"""

def __init__(self):
super().__init__()
self.images = {}
self.image_metadata = {}
self._finished_event = threading.Event()

def initialize(self, summary_metadata: dict):
self.summary_metadata = summary_metadata

def block_until_finished(self, timeout=None):
return self._finished_event.wait(timeout=timeout)

def finish(self):
self._finished_event.set()

def is_finished(self) -> bool:
return self._finished_event.is_set()

def put_image(self, coordinates, image, metadata):
key = frozenset(coordinates.items())
self.images[key] = image
self.image_metadata[key] = metadata
self._update_axes(coordinates)
self._new_image_event.set()

def get_image_coordinates_list(self) -> List[Dict[str, Union[int, str]]]:
frozen_set_list = list(self.images.keys())
# convert to dict
return [{axis_name: position for axis_name, position in key} for key in frozen_set_list]

#### ND Storage API ####
def close(self):
self.images = {}
self.image_metadata = {}
self.axes = {}

def has_image(self, channel=None, z=None, time=None, position=None, row=None, column=None, **kwargs):
axes = self._consolidate_axes(channel, z, position, time, row, column, **kwargs)
key = frozenset(axes.items())
return key in self.images.keys()

def read_image(self, channel=None, z=None, time=None, position=None, row=None, column=None, **kwargs):
axes = self._consolidate_axes(channel, z, position, time, row, column, **kwargs)
key = frozenset(axes.items())
if key not in self.images.keys():
raise Exception("image with keys {} not present in data set".format(key))
return self.images[key]

def read_metadata(self, channel=None, z=None, time=None, position=None, row=None, column=None, **kwargs):
axes = self._consolidate_axes(channel, z, position, time, row, column, **kwargs)
key = frozenset(axes.items())
if key not in self.images.keys():
raise Exception("image with keys {} not present in data set".format(key))
return self.image_metadata[key]

Loading

0 comments on commit 6f4a5ee

Please sign in to comment.