-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add TSWriter #8
Merged
Merged
Add TSWriter #8
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
6780144
Add tensorstore zarr writer
JesseMckinzie 35f48cc
Add tensorstore writer
JesseMckinzie 375b77d
Add python interface for zarr writer
JesseMckinzie 6872d82
Move utilities to separate directory
JesseMckinzie d7e79f4
Add python interface for TSWriter
JesseMckinzie 2ecf5cc
Move module setup to conftest.py
JesseMckinzie 182e03c
Add test for zarr writer
JesseMckinzie f360bfa
Add zarr requirement for unit tests
JesseMckinzie 217f946
Add zarr to TEST_REQUIRES
JesseMckinzie 39fa101
Remove unused methods
JesseMckinzie 61e85d8
Update test to use TSReader
JesseMckinzie 70d0186
Merge branch 'main' into tswriter
JesseMckinzie 23ad5dd
Fix flake8 formatting
JesseMckinzie 772326a
Reformat
JesseMckinzie 15949d6
Update data directory path
JesseMckinzie 6d827e0
Update module setup for tests
JesseMckinzie e5a0fae
Update TSWriter interface
JesseMckinzie 18462fc
Update writer test
JesseMckinzie 3098e58
Reformat tswriter
JesseMckinzie 8908c90
Freeze numpy version
JesseMckinzie a9743bf
Restrict numpy version to less than 2.0.0
JesseMckinzie 2887ecb
Remove duplicate definition
JesseMckinzie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
#include "tswriter.h" | ||
|
||
#include "../utilities/utilities.h" | ||
|
||
#include <variant> | ||
#include <string> | ||
|
||
using ::tensorstore::internal_zarr::ChooseBaseDType; | ||
|
||
namespace bfiocpp { | ||
|
||
TsWriterCPP::TsWriterCPP( | ||
const std::string& fname, | ||
const std::vector<std::int64_t>& image_shape, | ||
const std::vector<std::int64_t>& chunk_shape, | ||
const std::string& dtype_str | ||
): _filename(fname), _image_shape(image_shape), _chunk_shape(chunk_shape) { | ||
|
||
_dtype_code = GetDataTypeCode(dtype_str); | ||
|
||
std::string dtype_str_converted = (dtype_str == "float64") ? "double" : dtype_str; // change float64 numpy type to double | ||
|
||
auto dtype = GetTensorStoreDataType(dtype_str_converted); | ||
|
||
auto dtype_base = ChooseBaseDType(dtype).value().encoded_dtype; | ||
|
||
auto spec = GetZarrSpecToWrite(_filename, image_shape, chunk_shape, dtype_base); | ||
|
||
TENSORSTORE_CHECK_OK_AND_ASSIGN(_source, tensorstore::Open( | ||
spec, | ||
tensorstore::OpenMode::create | | ||
tensorstore::OpenMode::delete_existing, | ||
tensorstore::ReadWriteMode::write).result()); | ||
} | ||
|
||
|
||
void TsWriterCPP::write_image(py::array& py_image) { | ||
|
||
// use switch instead of template to avoid creating functions for each datatype | ||
switch(_dtype_code) | ||
{ | ||
case (1): { | ||
auto data_array = tensorstore::Array(py_image.mutable_unchecked<std::uint8_t, 1>().data(0), _image_shape, tensorstore::c_order); | ||
|
||
// Write data array to TensorStore | ||
auto write_result = tensorstore::Write(tensorstore::UnownedToShared(data_array), _source).result(); | ||
|
||
if (!write_result.ok()) { | ||
std::cerr << "Error writing image: " << write_result.status() << std::endl; | ||
} | ||
|
||
break; | ||
} | ||
case (2): { | ||
auto data_array = tensorstore::Array(py_image.mutable_unchecked<std::uint16_t, 1>().data(0), _image_shape, tensorstore::c_order); | ||
|
||
auto write_result = tensorstore::Write(tensorstore::UnownedToShared(data_array), _source).result(); | ||
if (!write_result.ok()) { | ||
std::cerr << "Error writing image: " << write_result.status() << std::endl; | ||
} | ||
break; | ||
} | ||
case (4): { | ||
auto data_array = tensorstore::Array(py_image.mutable_unchecked<std::uint32_t, 1>().data(0), _image_shape, tensorstore::c_order); | ||
|
||
auto write_result = tensorstore::Write(tensorstore::UnownedToShared(data_array), _source).result(); | ||
if (!write_result.ok()) { | ||
std::cerr << "Error writing image: " << write_result.status() << std::endl; | ||
} | ||
break; | ||
} | ||
case (8): { | ||
auto data_array = tensorstore::Array(py_image.mutable_unchecked<std::uint64_t, 1>().data(0), _image_shape, tensorstore::c_order); | ||
|
||
auto write_result = tensorstore::Write(tensorstore::UnownedToShared(data_array), _source).result(); | ||
if (!write_result.ok()) { | ||
std::cerr << "Error writing image: " << write_result.status() << std::endl; | ||
} | ||
break; | ||
} | ||
case (16): { | ||
auto data_array = tensorstore::Array(py_image.mutable_unchecked<std::int8_t, 1>().data(0), _image_shape, tensorstore::c_order); | ||
|
||
auto write_result = tensorstore::Write(tensorstore::UnownedToShared(data_array), _source).result(); | ||
if (!write_result.ok()) { | ||
std::cerr << "Error writing image: " << write_result.status() << std::endl; | ||
} | ||
break; | ||
} | ||
case (32): { | ||
auto data_array = tensorstore::Array(py_image.mutable_unchecked<std::int16_t, 1>().data(0), _image_shape, tensorstore::c_order); | ||
|
||
auto write_result = tensorstore::Write(tensorstore::UnownedToShared(data_array), _source).result(); | ||
if (!write_result.ok()) { | ||
std::cerr << "Error writing image: " << write_result.status() << std::endl; | ||
} | ||
break; | ||
} | ||
case (64): { | ||
auto data_array = tensorstore::Array(py_image.mutable_unchecked<std::int32_t, 1>().data(0), _image_shape, tensorstore::c_order); | ||
|
||
auto write_result = tensorstore::Write(tensorstore::UnownedToShared(data_array), _source).result(); | ||
if (!write_result.ok()) { | ||
std::cerr << "Error writing image: " << write_result.status() << std::endl; | ||
} | ||
break; | ||
} | ||
case (128): { | ||
auto data_array = tensorstore::Array(py_image.mutable_unchecked<std::int64_t, 1>().data(0), _image_shape, tensorstore::c_order); | ||
|
||
// Write data array to TensorStore | ||
auto write_result = tensorstore::Write(tensorstore::UnownedToShared(data_array), _source).result(); | ||
if (!write_result.ok()) { | ||
std::cerr << "Error writing image: " << write_result.status() << std::endl; | ||
} | ||
break; | ||
} | ||
case (256): { | ||
auto data_array = tensorstore::Array(py_image.mutable_unchecked<float, 1>().data(0), _image_shape, tensorstore::c_order); | ||
|
||
auto write_result = tensorstore::Write(tensorstore::UnownedToShared(data_array), _source).result(); | ||
if (!write_result.ok()) { | ||
std::cerr << "Error writing image: " << write_result.status() << std::endl; | ||
} | ||
break; | ||
} | ||
case (512): { | ||
auto data_array = tensorstore::Array(py_image.mutable_unchecked<double, 1>().data(0), _image_shape, tensorstore::c_order); | ||
|
||
auto write_result = tensorstore::Write(tensorstore::UnownedToShared(data_array), _source).result(); | ||
if (!write_result.ok()) { | ||
std::cerr << "Error writing image: " << write_result.status() << std::endl; | ||
} | ||
break; | ||
} | ||
default: { | ||
// should not be reached | ||
std::cerr << "Error writing image: unsupported data type" << std::endl; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
#include <memory> | ||
#include <vector> | ||
#include <variant> | ||
#include <iostream> | ||
#include <tuple> | ||
#include <optional> | ||
#include <unordered_map> | ||
#include "../reader/sequence.h" | ||
|
||
#include "tensorstore/tensorstore.h" | ||
#include "tensorstore/context.h" | ||
#include "tensorstore/array.h" | ||
#include "tensorstore/driver/zarr/dtype.h" | ||
#include "tensorstore/index_space/dim_expression.h" | ||
#include "tensorstore/kvstore/kvstore.h" | ||
#include "tensorstore/open.h" | ||
|
||
#include <pybind11/pybind11.h> | ||
#include <pybind11/stl.h> | ||
#include <pybind11/numpy.h> | ||
|
||
namespace py = pybind11; | ||
|
||
namespace bfiocpp{ | ||
|
||
class TsWriterCPP{ | ||
public: | ||
TsWriterCPP(const std::string& fname, const std::vector<std::int64_t>& image_shape, const std::vector<std::int64_t>& chunk_shape, const std::string& dtype); | ||
|
||
void write_image(py::array& py_image); | ||
|
||
private: | ||
std::string _filename; | ||
|
||
std::vector<std::int64_t> _image_shape, _chunk_shape; | ||
|
||
uint16_t _dtype_code; | ||
|
||
tensorstore::TensorStore<void, -1, tensorstore::ReadWriteMode::dynamic> _source; | ||
|
||
}; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from .tsreader import TSReader, Seq, FileType, get_ome_xml # NOQA: F401 | ||
from .tswriter import TSWriter # NOQA: F401 | ||
from . import _version | ||
|
||
__version__ = _version.get_versions()["version"] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you need this. You can just pass in
dtype_str
toGetZarrSpecToWrite