Skip to content

Commit

Permalink
save work
Browse files Browse the repository at this point in the history
  • Loading branch information
Bechir-Brahem committed Jul 23, 2024
1 parent 7babcd8 commit 476aa98
Show file tree
Hide file tree
Showing 8 changed files with 456 additions and 57 deletions.
2 changes: 1 addition & 1 deletion src/file_io/include/aare/file_io/ClusterFile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace aare {
* - variable_length_array: 4 bytes (number of elements) + number_of_elements *
* dtype.bytes()
*
* - header_fields (array of field objects):
* - data_fields (array of field objects):
* - field_label: field_label_length bytes (string max 256 chars)
* - dtype: 3 chars (string)
* - is_array: (number) 0: not array, 1:fixed_length_array, 2:variable_length_array
Expand Down
36 changes: 30 additions & 6 deletions src/python/cpp/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,17 @@ void define_cluster_bindings(py::module &m) {
.def(py::init<std::string const &, Dtype, Field::ARRAY_TYPE, uint32_t>())
.def_readwrite("label", &Field::label)
.def_readwrite("dtype", &Field::dtype)
.def_readwrite("is_array", &Field::is_array)
.def_property(
"is_array", [](const Field &f) { return f.is_array; },
[](Field &f, bool is_array) {
if (is_array == 0) {
f.is_array = Field::NOT_ARRAY;
} else if (is_array == 1) {
f.array_size = Field::FIXED_LENGTH_ARRAY;
} else if (is_array == 2) {
f.array_size = Field::VARIABLE_LENGTH_ARRAY;
}
})
.def_readwrite("array_size", &Field::array_size)
.def("to_json", &Field::to_json)
.def("from_json", &Field::from_json);
Expand All @@ -39,14 +49,26 @@ void define_cluster_bindings(py::module &m) {
.def_static("get_fields", &ClusterDataVlen::get_fields)
.def("__repr__", &ClusterDataVlen::to_string);

py::class_<DynamicClusterData>(m, "Cluster")
.def(py::init())
py::class_<DynamicClusterData>(m, "Cluster", py::buffer_protocol())
.def(py::init<uint8_t, Dtype>())
.def_readwrite("x", &DynamicClusterData::x)
.def_readwrite("y", &DynamicClusterData::y)
.def("data", &DynamicClusterData::data, py::return_value_policy::reference)
.def_readwrite("dtype", &DynamicClusterData::dtype)
.def_readwrite("count", &DynamicClusterData::count)
.def("data",
[](DynamicClusterData &c) {
return py::memoryview::from_memory(c.data(), c.count * c.dtype.bytes());
})
.def_readonly("dtype", &DynamicClusterData::dtype)
.def_readonly("count", &DynamicClusterData::count)
.def("__repr__", &DynamicClusterData::to_string);

py::class_<tClusterData<int32_t, 9>>(m, "OldCluster")
.def(py::init())
.def_readwrite("x", &tClusterData<int32_t, 9>::x)
.def_readwrite("y", &tClusterData<int32_t, 9>::y)
.def("data", &tClusterData<int32_t, 9>::data, py::return_value_policy::reference)
.def_readwrite("array", &tClusterData<int32_t, 9>::array)
.def("get_fields", &tClusterData<int32_t, 9>::get_fields)
.def("__repr__", &tClusterData<int32_t, 9>::to_string);
}

template <typename T> void define_to_frame(py::module &m) {
Expand Down Expand Up @@ -200,4 +222,6 @@ void define_core_bindings(py::module &m) {
define_to_frame<int64_t>(m);
define_to_frame<float>(m);
define_to_frame<double>(m);

define_cluster_bindings(m);
}
22 changes: 18 additions & 4 deletions src/python/cpp/file_io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,30 @@ void define_file_io_bindings(py::module &m) {
.def("__ne__", &FileConfig::operator!=)
.def("__repr__", [](const FileConfig &a) { return "<FileConfig: " + a.to_string() + ">"; });

py::class_<ClusterFile<ClusterHeader, DynamicClusterData>::Result>(m, "ClusterFileResult")
.def_readwrite("header", &ClusterFile<ClusterHeader, DynamicClusterData>::Result::header)
.def_readwrite("data", &ClusterFile<ClusterHeader, DynamicClusterData>::Result::data);
py::class_<ClusterFileHeader>(m, "ClusterFileHeader")
.def(py::init<>())
.def_readwrite("version", &ClusterFileHeader::version)
.def_readwrite("n_records", &ClusterFileHeader::n_records)
.def_readwrite("metadata", &ClusterFileHeader::metadata)
.def_readwrite("header_fields", &ClusterFileHeader::header_fields)
.def_readwrite("data_fields", &ClusterFileHeader::data_fields)
.def("to_json", &ClusterFileHeader::to_json)
.def("from_json", &ClusterFileHeader::from_json)
.def("__eq__", &ClusterFileHeader::operator==)
.def("__ne__", &ClusterFileHeader::operator!=)
.def("__repr__",
[](const ClusterFileHeader &a) { return "<ClusterFileHeader: " + a.to_json() + ">"; });

py::class_<ClusterFile<ClusterHeader, DynamicClusterData>>(m, "ClusterFile")
.def(
py::init<const std::filesystem::path &, const std::string &, ClusterFileHeader, bool>())
.def("header", &ClusterFile<ClusterHeader, DynamicClusterData>::header)
.def("read", &ClusterFile<ClusterHeader, DynamicClusterData>::read)
.def("write", &ClusterFile<ClusterHeader, DynamicClusterData>::write)
.def("read",
[](ClusterFile<ClusterHeader, DynamicClusterData> &cf) {
ClusterFile<ClusterHeader, DynamicClusterData>::Result result = cf.read();
return std::pair(std::move(result.header), std::move(result.data));
})
.def("set_n_records", &ClusterFile<ClusterHeader, DynamicClusterData>::set_n_records)
.def("tell", &ClusterFile<ClusterHeader, DynamicClusterData>::tell);
}
Binary file removed src/python/example/aare_hist.png
Binary file not shown.
453 changes: 407 additions & 46 deletions src/python/example/bindings_walkthrough.ipynb

Large diffs are not rendered by default.

Binary file removed src/python/example/cluster sum for each (x,y).png
Binary file not shown.
Binary file removed src/python/example/cluster_sum_per_pixel.png
Binary file not shown.
Binary file removed src/python/example/simple_density.png
Binary file not shown.

0 comments on commit 476aa98

Please sign in to comment.