forked from mindspore-Ecosystem/mindspore
fix schema.parse_columns function
This commit is contained in:
parent
f7aadb4da8
commit
e0393b482f
|
@ -2464,47 +2464,53 @@ class Schema:
|
||||||
Parse the columns and add it to self.
|
Parse the columns and add it to self.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
columns (dict or list[str]): names of columns.
|
columns (dict or list[dict]): dataset attribution information, decoded from schema file.
|
||||||
|
if list: columns element must be dict, 'name' and 'type' must be in keys, 'shape' optional.
|
||||||
|
if dict: columns.keys() as name, element in columns.values() is dict, and 'type' inside, 'shape' optional.
|
||||||
|
example 1)
|
||||||
|
[{'name': 'image', 'type': 'int8', 'shape': [3, 3]},
|
||||||
|
{'name': 'label', 'type': 'int8', 'shape': [1]}]
|
||||||
|
example 2)
|
||||||
|
{'image': {'shape': [3, 3], 'type': 'int8'}, 'label': {'shape': [1], 'type': 'int8'}}
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
RuntimeError: If failed to parse schema file.
|
RuntimeError: If failed to parse columns.
|
||||||
RuntimeError: If unknown items in schema file.
|
RuntimeError: If unknown items in columns.
|
||||||
RuntimeError: If column's name field is missing.
|
RuntimeError: If column's name field is missing.
|
||||||
RuntimeError: If column's type field is missing.
|
RuntimeError: If column's type field is missing.
|
||||||
"""
|
"""
|
||||||
if columns is None:
|
|
||||||
raise TypeError("Expected non-empty dict or string list.")
|
|
||||||
self.columns = []
|
self.columns = []
|
||||||
for col in columns:
|
if isinstance(columns, list):
|
||||||
name = None
|
for column in columns:
|
||||||
shape = None
|
try:
|
||||||
data_type = None
|
name = column.pop("name")
|
||||||
col_details = None
|
except KeyError:
|
||||||
if isinstance(columns, list):
|
raise RuntimeError("Column's name is missing")
|
||||||
col_details = col
|
try:
|
||||||
if "name" in col:
|
de_type = column.pop("type")
|
||||||
name = col["name"]
|
except KeyError:
|
||||||
elif isinstance(columns, dict):
|
raise RuntimeError("Column' type is missing")
|
||||||
col_details = columns[col]
|
shape = column.pop("shape", None)
|
||||||
name = col
|
column.pop("t_impl", None)
|
||||||
else:
|
column.pop("rank", None)
|
||||||
raise RuntimeError("Error parsing the schema file")
|
if column:
|
||||||
|
raise RuntimeError("Unknown field {}".format(",".join(column.keys())))
|
||||||
for k, v in col_details.items():
|
self.add_column(name, de_type, shape)
|
||||||
if k == "shape":
|
elif isinstance(columns, dict):
|
||||||
shape = v
|
for key, value in columns.items():
|
||||||
elif k == "type":
|
name = key
|
||||||
data_type = v
|
try:
|
||||||
elif k in ("t_impl", "rank"):
|
de_type = value.pop("type")
|
||||||
pass
|
except KeyError:
|
||||||
else:
|
raise RuntimeError("Column' type is missing")
|
||||||
raise RuntimeError("Unknown field %s" % k)
|
shape = value.pop("shape", None)
|
||||||
|
value.pop("t_impl", None)
|
||||||
if name is None:
|
value.pop("rank", None)
|
||||||
raise RuntimeError("Column's name field is missing.")
|
if value:
|
||||||
if data_type is None:
|
raise RuntimeError("Unknown field {}".format(",".join(value.keys())))
|
||||||
raise RuntimeError("Column's type field is missing.")
|
self.add_column(name, de_type, shape)
|
||||||
self.add_column(name, data_type, shape)
|
else:
|
||||||
|
raise RuntimeError("columns must be dict or list, columns contain name, type, shape(optional).")
|
||||||
|
|
||||||
def from_json(self, json_obj):
|
def from_json(self, json_obj):
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in New Issue