ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
HashMapIO.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
9 
10 #include <FileSystem.h>
11 
13 namespace cloudViewer {
14 namespace t {
15 namespace io {
16 
17 void WriteHashMap(const std::string& file_name, const core::HashMap& hashmap) {
18  core::Tensor keys = hashmap.GetKeyTensor();
19  std::vector<core::Tensor> values = hashmap.GetValueTensors();
20 
21  core::Device host("CPU:0");
22 
23  core::Tensor active_buf_indices_i32;
24  hashmap.GetActiveIndices(active_buf_indices_i32);
25  core::Tensor active_indices = active_buf_indices_i32.To(core::Int64);
26 
27  core::Tensor active_keys = keys.IndexGet({active_indices}).To(host);
28 
29  std::unordered_map<std::string, core::Tensor> output;
30  output.emplace("key", active_keys);
31  output.emplace(
32  "n_values",
34  std::vector<int64_t>{static_cast<int64_t>(values.size())},
35  {1}, core::Int64, host));
36  for (size_t i = 0; i < values.size(); ++i) {
37  core::Tensor active_value_i =
38  values[i].IndexGet({active_indices}).To(host);
39  output.emplace(fmt::format("value_{:03d}", i), active_value_i);
40  }
41 
42  std::string ext =
44  std::string postfix = ext != "npz" ? ".npz" : "";
45  WriteNpz(file_name + postfix, output);
46 }
47 
48 core::HashMap ReadHashMap(const std::string& file_name) {
49  std::unordered_map<std::string, core::Tensor> tensor_map =
50  t::io::ReadNpz(file_name);
51 
52  // Key
53  core::Tensor keys = tensor_map.at("key");
54 
55  core::Dtype key_dtype = keys.GetDtype();
56 
57  core::SizeVector shape_key = keys.GetShape();
58  core::SizeVector key_element_shape(shape_key.begin() + 1, shape_key.end());
59 
60  int64_t init_capacity = keys.GetLength();
61 
62  // Value(s)
63  int64_t n_values = tensor_map.at("n_values")[0].Item<int64_t>();
64 
65  std::vector<core::Tensor> arr_input_values;
66  std::vector<core::Dtype> dtypes_value;
67  std::vector<core::SizeVector> element_shapes_value;
68 
69  for (int64_t i = 0; i < n_values; ++i) {
70  core::Tensor value_i = tensor_map.at(fmt::format("value_{:03d}", i));
71 
72  core::Dtype value_dtype_i = value_i.GetDtype();
73 
74  core::SizeVector shape_value_i = value_i.GetShape();
75  core::SizeVector value_element_shape_i(shape_value_i.begin() + 1,
76  shape_value_i.end());
77 
78  arr_input_values.push_back(value_i);
79  dtypes_value.push_back(value_dtype_i);
80  element_shapes_value.push_back(value_element_shape_i);
81  }
82 
83  auto hashmap =
84  core::HashMap(init_capacity, key_dtype, key_element_shape,
85  dtypes_value, element_shapes_value, core::Device());
86 
87  core::Tensor masks, buf_indices;
88  hashmap.Insert(keys, arr_input_values, masks, buf_indices);
89 
90  return hashmap;
91 }
92 } // namespace io
93 } // namespace t
94 } // namespace cloudViewer
filament::Texture::InternalFormat format
std::vector< Tensor > GetValueTensors() const
Definition: HashMap.cpp:275
Tensor GetActiveIndices() const
Definition: HashMap.cpp:112
Tensor GetKeyTensor() const
Definition: HashMap.cpp:266
int64_t GetLength() const
Definition: Tensor.h:1125
Dtype GetDtype() const
Definition: Tensor.h:1164
Tensor IndexGet(const std::vector< Tensor > &index_tensors) const
Advanced indexing getter. This will always allocate a new Tensor.
Definition: Tensor.cpp:905
SizeVector GetShape() const
Definition: Tensor.h:1127
Tensor To(Dtype dtype, bool copy=false) const
Definition: Tensor.cpp:739
const Dtype Int64
Definition: Dtype.cpp:47
void To(const core::Tensor &src, core::Tensor &dst, double scale, double offset)
Definition: Image.cpp:17
core::HashMap ReadHashMap(const std::string &file_name)
Definition: HashMapIO.cpp:48
std::unordered_map< std::string, core::Tensor > ReadNpz(const std::string &file_name)
Definition: NumpyIO.cpp:675
void WriteHashMap(const std::string &file_name, const core::HashMap &hashmap)
Definition: HashMapIO.cpp:17
void WriteNpz(const std::string &file_name, const std::unordered_map< std::string, core::Tensor > &tensor_map)
Definition: NumpyIO.cpp:759
std::string GetFileExtensionInLowerCase(const std::string &filename)
Definition: FileSystem.cpp:281
Generic file read and write utility for python interface.