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.

filereader.py 3.4 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # Copyright 2019 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ==============================================================================
  15. """
  16. This module is to read data from mindrecord.
  17. """
  18. from .shardreader import ShardReader
  19. from .shardheader import ShardHeader
  20. from .shardutils import populate_data
  21. from .shardutils import MIN_CONSUMER_COUNT, MAX_CONSUMER_COUNT, check_filename
  22. from .common.exceptions import ParamValueError, ParamTypeError
  23. __all__ = ['FileReader']
  24. class FileReader:
  25. """
  26. Class to read MindRecord File series.
  27. Args:
  28. file_name (str, list[str]): One of MindRecord File or a file list.
  29. num_consumer(int, optional): Number of consumer threads which load data to memory (default=4).
  30. It should not be smaller than 1 or larger than the number of CPUs.
  31. columns (list[str], optional): A list of fields where corresponding data would be read (default=None).
  32. operator(int, optional): Reserved parameter for operators (default=None).
  33. Raises:
  34. ParamValueError: If file_name, num_consumer or columns is invalid.
  35. """
  36. def __init__(self, file_name, num_consumer=4, columns=None, operator=None):
  37. if isinstance(file_name, list):
  38. for f in file_name:
  39. check_filename(f)
  40. else:
  41. check_filename(file_name)
  42. if num_consumer is not None:
  43. if isinstance(num_consumer, int):
  44. if num_consumer < MIN_CONSUMER_COUNT or num_consumer > MAX_CONSUMER_COUNT():
  45. raise ParamValueError("Consumer number should between {} and {}."
  46. .format(MIN_CONSUMER_COUNT, MAX_CONSUMER_COUNT()))
  47. else:
  48. raise ParamValueError("Consumer number is illegal.")
  49. else:
  50. raise ParamValueError("Consumer number is illegal.")
  51. if columns:
  52. if isinstance(columns, list):
  53. self._columns = columns
  54. else:
  55. raise ParamTypeError('columns', 'list')
  56. else:
  57. self._columns = None
  58. self._reader = ShardReader()
  59. self._reader.open(file_name, num_consumer, columns, operator)
  60. self._header = ShardHeader(self._reader.get_header())
  61. self._reader.launch()
  62. def get_next(self):
  63. """
  64. Yield a batch of data according to columns at a time.
  65. Yields:
  66. dictionary: keys are the same as columns.
  67. Raises:
  68. MRMUnsupportedSchemaError: If schema is invalid.
  69. """
  70. iterator = self._reader.get_next()
  71. while iterator:
  72. for blob, raw in iterator:
  73. yield populate_data(raw, blob, self._columns, self._header.blob_fields, self._header.schema)
  74. iterator = self._reader.get_next()
  75. def close(self):
  76. """Stop reader worker and close File."""
  77. self._reader.close()