You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

data_schema.h 9.1 kB

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /**
  2. * Copyright 2019-2021 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_ENGINE_DATA_SCHEMA_H_
  17. #define MINDSPORE_CCSRC_MINDDATA_DATASET_ENGINE_DATA_SCHEMA_H_
  18. #include <iostream>
  19. #include <map>
  20. #include <memory>
  21. #include <string>
  22. #include <unordered_map>
  23. #include <vector>
  24. #include <nlohmann/json.hpp>
  25. #include "minddata/dataset/include/dataset/constants.h"
  26. #include "minddata/dataset/core/data_type.h"
  27. #include "minddata/dataset/core/tensor_shape.h"
  28. #include "minddata/dataset/util/status.h"
  29. namespace mindspore {
  30. namespace dataset {
  31. /// \class ColDescriptor data_schema.h
  32. /// \brief A simple class to provide meta info about a column.
  33. class ColDescriptor {
  34. public:
  35. /// \brief Constructor 1: Simple constructor that leaves things uninitialized.
  36. ColDescriptor();
  37. /// \brief Constructor 2: Main constructor
  38. /// \param[in] col_name - The name of the column
  39. /// \param[in] col_type - The DE Datatype of the column
  40. /// \param[in] tensor_impl - The (initial) type of tensor implementation for the column
  41. /// \param[in] rank - The number of dimension of the data
  42. /// \param[in] in_shape - option argument for input shape
  43. ColDescriptor(const std::string &col_name, DataType col_type, TensorImpl tensor_impl, int32_t rank,
  44. const TensorShape *in_shape = nullptr);
  45. /// \brief Explicit copy constructor is required
  46. /// \param[in] in_cd - the source ColDescriptor
  47. ColDescriptor(const ColDescriptor &in_cd);
  48. /// \brief Assignment overload
  49. /// \param in_cd - the source ColDescriptor
  50. ColDescriptor &operator=(const ColDescriptor &in_cd);
  51. /// \brief Destructor
  52. ~ColDescriptor();
  53. /// \brief A print method typically used for debugging
  54. /// \param out - The output stream to write output to
  55. void Print(std::ostream &out) const;
  56. /// \brief Given a number of elements, this function will compute what the actual Tensor shape would be.
  57. /// If there is no starting TensorShape in this column, or if there is a shape but it contains
  58. /// an unknown dimension, then the output shape returned shall resolve dimensions as needed.
  59. /// \param[in] num_elements - The number of elements in the data for a Tensor
  60. /// \param[in/out] out_shape - The materialized output Tensor shape
  61. /// \return Status The status code returned
  62. Status MaterializeTensorShape(int32_t num_elements, TensorShape *out_shape) const;
  63. /// \brief << Stream output operator overload
  64. /// This allows you to write the debug print info using stream operators
  65. /// \param[in] out - reference to the output stream being overloaded
  66. /// \param[in] cd - reference to the ColDescriptor to display
  67. /// \return - the output stream must be returned
  68. friend std::ostream &operator<<(std::ostream &out, const ColDescriptor &cd) {
  69. cd.Print(out);
  70. return out;
  71. }
  72. /// \brief getter function
  73. /// \return The column's DataType
  74. DataType type() const { return type_; }
  75. /// \brief getter function
  76. /// \return The column's rank
  77. int32_t rank() const { return rank_; }
  78. /// \brief getter function
  79. /// \return The column's name
  80. std::string name() const { return col_name_; }
  81. /// \brief getter function
  82. /// \return The column's shape
  83. TensorShape shape() const;
  84. /// \brief getter function
  85. /// \return TF if the column has an assigned fixed shape.
  86. bool hasShape() const { return tensor_shape_ != nullptr; }
  87. /// \brief getter function
  88. /// \return The column's tensor implementation type
  89. TensorImpl tensorImpl() const { return tensor_impl_; }
  90. private:
  91. DataType type_; // The columns type
  92. int32_t rank_; // The rank for this column (number of dimensions)
  93. TensorImpl tensor_impl_; // The initial flavour of the tensor for this column
  94. std::unique_ptr<TensorShape> tensor_shape_; // The fixed shape (if given by user)
  95. std::string col_name_; // The name of the column
  96. };
  97. /// \class DataSchema data_schema.h
  98. /// \brief A list of the columns.
  99. class DataSchema {
  100. public:
  101. /// \brief Constructor
  102. DataSchema();
  103. /// \brief Destructor
  104. ~DataSchema();
  105. /// \brief Parses a schema json file and populates the columns and meta info.
  106. /// \param[in] schema_file_path - the schema file that has the column's info to load
  107. /// \param[in] columns_to_load - list of strings for columns to load. if empty, assumes all columns.
  108. /// \return Status The status code returned
  109. Status LoadSchemaFile(const std::string &schema_file_path, const std::vector<std::string> &columns_to_load);
  110. /// \brief Parses a schema JSON string and populates the columns and meta info.
  111. /// \param[in] schema_json_string - the schema file that has the column's info to load
  112. /// \param[in] columns_to_load - list of strings for columns to load. if empty, assumes all columns.
  113. /// \return Status The status code returned
  114. Status LoadSchemaString(const std::string &schema_json_string, const std::vector<std::string> &columns_to_load);
  115. /// \brief A print method typically used for debugging
  116. /// \param[in] out - The output stream to write output to
  117. void Print(std::ostream &out) const;
  118. /// \brief << Stream output operator overload. This allows you to write the debug print info using stream operators
  119. /// \param[in] out - reference to the output stream being overloaded
  120. /// \param[in] ds - reference to the DataSchema to display
  121. /// \return - the output stream must be returned
  122. friend std::ostream &operator<<(std::ostream &out, const DataSchema &ds) {
  123. ds.Print(out);
  124. return out;
  125. }
  126. /// \brief Adds a column descriptor to the schema
  127. /// \param[in] cd - The ColDescriptor to add
  128. /// \return Status The status code returned
  129. Status AddColumn(const ColDescriptor &cd);
  130. /// \brief getter
  131. /// \return The reference to a ColDescriptor to get (const version)
  132. const ColDescriptor &column(int32_t idx) const;
  133. /// \brief getter
  134. /// \return The number of columns in the schema
  135. int32_t NumColumns() const { return col_descs_.size(); }
  136. bool Empty() const { return NumColumns() == 0; }
  137. /// \brief getter
  138. /// \return The number of rows read from schema
  139. int64_t num_rows() const { return num_rows_; }
  140. static const char DEFAULT_DATA_SCHEMA_FILENAME[];
  141. /// \brief Loops through all columns in the schema and returns a map with the column name to column index number.
  142. /// \param[in/out] out_column_name_map - The output map of columns names to column index
  143. /// \return Status The status code returned
  144. Status GetColumnNameMap(std::unordered_map<std::string, int32_t> *out_column_name_map);
  145. private:
  146. /// \brief Internal helper function. Parses the json schema file in any order and produces a schema that
  147. /// does not follow any particular order (json standard does not enforce any ordering protocol).
  148. /// This one produces a schema that contains all of the columns from the schema file.
  149. /// \param[in] column_tree - The nlohmann tree from the json file to parse
  150. /// \return Status The status code returned
  151. Status AnyOrderLoad(nlohmann::json column_tree);
  152. /// \brief Internal helper function. For each input column name, perform a lookup to the json document to
  153. /// find the matching column. When the match is found, process that column to build the column
  154. /// descriptor and add to the schema in the order in which the input column names are given.
  155. /// \param[in] column_tree - The nlohmann tree from the json file to parse
  156. /// \param[in] columns_to_load - list of strings for the columns to add to the schema
  157. /// \return Status The status code returned
  158. Status ColumnOrderLoad(nlohmann::json column_tree, const std::vector<std::string> &columns_to_load);
  159. /// \brief Internal helper function. Given the json tree for a given column, load it into our schema.
  160. /// \param[in] columnTree - The nlohmann child tree for a given column to load.
  161. /// \param[in] col_name - The string name of the column for that subtree.
  162. /// \return Status The status code returned
  163. Status ColumnLoad(nlohmann::json column_child_tree, const std::string &col_name);
  164. /// \brief Internal helper function. Performs sanity checks on the json file setup.
  165. /// \param[in] js - The nlohmann tree for the schema file
  166. /// \return Status The status code returned
  167. Status PreLoadExceptionCheck(const nlohmann::json &js);
  168. std::vector<ColDescriptor> col_descs_; // Vector of column descriptors
  169. int64_t num_rows_;
  170. };
  171. } // namespace dataset
  172. } // namespace mindspore
  173. #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_ENGINE_DATA_SCHEMA_H_