class TestDataset(Dataset): feat1 = Feature(shape=(1, 2, 3), dtype=int) feat2 = Feature(shape=None, dtype=str, metadata=Metadata(abc=123, xyz=False, pi=3.14)) class DefaultMetadata: attr1 = "val1" attr2 = "val2"
def __make_random_feature(): shape = (None if random.random() < 0.2 else tuple( random.randint(1, 50) for _ in range(random.randint(1, 4)))) # dtype = random.choice([str, int, float, bool, np.uint8, np.float32]) dtype = random.choice([str]) metadata = make_random_metadata_object() return Feature(shape=shape, dtype=dtype, metadata=metadata)
def test_feature_eq(make_random_feature): feat1 = make_random_feature() feat2 = Feature(**feat1.dict()) assert feat1 is not feat2 assert feat1 == feat2 feat3 = make_random_feature() while feat3.dict() == feat1.dict(): feat3 = make_random_feature() assert feat3 != feat1
def test_dataset_subclass(): class TestDataset(Dataset): feat1 = Feature(shape=(1, 2, 3), dtype=int) feat2 = Feature(shape=None, dtype=str, metadata=Metadata(abc=123, xyz=False, pi=3.14)) class DefaultMetadata: attr1 = "val1" attr2 = "val2" assert TestDataset.features["feat1"] == Feature(shape=(1, 2, 3), dtype=int) assert TestDataset.features["feat2"] == Feature(shape=None, dtype=str, metadata=Metadata( abc=123, xyz=False, pi=3.14)) assert TestDataset.metadata["attr1"] == "val1" assert TestDataset.metadata["attr2"] == "val2"
class DatasetB(DatasetY, DatasetZ): feature = Feature(shape=None, dtype=int, metadata=Metadata(source="B")) class DefaultMetadata: source = "B"
class DatasetX(Dataset): feature = Feature(shape=None, dtype=int, metadata=Metadata(source="X")) class DefaultMetadata: source = "X"
def test_feature(make_random_metadata_object): metadata = make_random_metadata_object() vector_feat = Feature(shape=(123, ), dtype=int, metadata=metadata) assert vector_feat.shape == (123, ) assert vector_feat.dtype == np.dtype(int) assert vector_feat.metadata == metadata
def test_feature_serialize_deserialize(make_random_feature): feat = make_random_feature() feat_deserialized = Feature.deserialize(feat.serialize()) assert feat_deserialized == feat