| 103 | } |
| 104 | |
| 105 | EvalDatasetPtr |
| 106 | EvalDataset::Load(const std::string& filename) { |
| 107 | H5::H5File file(filename, H5F_ACC_RDONLY); |
| 108 | |
| 109 | // check datasets exist |
| 110 | bool has_labels = false; |
| 111 | bool has_valid_ratio = false; |
| 112 | { |
| 113 | auto datasets = get_datasets(file); |
| 114 | assert(datasets.count("train")); |
| 115 | assert(datasets.count("test")); |
| 116 | assert(datasets.count("neighbors")); |
| 117 | assert(datasets.count("distances")); |
| 118 | has_labels = datasets.count("train_labels") && datasets.count("test_labels"); |
| 119 | has_valid_ratio = datasets.count("valid_ratios") > 0; |
| 120 | } |
| 121 | |
| 122 | // get and (should check shape) |
| 123 | auto train_shape = get_shape(file, "train"); |
| 124 | spdlog::debug("train.shape: " + to_string(train_shape)); |
| 125 | auto test_shape = get_shape(file, "test"); |
| 126 | spdlog::debug("test.shape: " + to_string(test_shape)); |
| 127 | auto neighbors_shape = get_shape(file, "neighbors"); |
| 128 | spdlog::debug("neighbors.shape: " + to_string(neighbors_shape)); |
| 129 | assert(train_shape.second == test_shape.second); |
| 130 | |
| 131 | auto obj = std::make_shared<EvalDataset>(); |
| 132 | obj->file_path_ = filename; |
| 133 | obj->train_shape_ = train_shape; |
| 134 | obj->test_shape_ = test_shape; |
| 135 | obj->neighbors_shape_ = neighbors_shape; |
| 136 | obj->dim_ = train_shape.second; |
| 137 | obj->number_of_base_ = train_shape.first; |
| 138 | obj->number_of_query_ = test_shape.first; |
| 139 | |
| 140 | // compatible with the hdf5 files from internet |
| 141 | if (not file.attrExists("type")) { |
| 142 | obj->vector_type_ = DENSE_VECTORS; |
| 143 | } else { |
| 144 | try { |
| 145 | H5::Attribute attr = file.openAttribute("type"); |
| 146 | H5::StrType str_type = attr.getStrType(); |
| 147 | std::string type; |
| 148 | attr.read(str_type, type); |
| 149 | if (type == "dense") { |
| 150 | obj->vector_type_ = DENSE_VECTORS; |
| 151 | } else if (type == "sparse") { |
| 152 | obj->vector_type_ = SPARSE_VECTORS; |
| 153 | } |
| 154 | } catch (H5::Exception& err) { |
| 155 | throw std::runtime_error("fail to read metric: there is no 'type' in the dataset"); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | if (obj->vector_type_ == DENSE_VECTORS) { |
| 160 | // read from file |
| 161 | { |
| 162 | H5::DataSet dataset = file.openDataSet("/train"); |
nothing calls this directly
no test coverage detected