def test_ExampleConcatenatedDataset_slicing(): D1 = DebugDataset(size=10) D2 = DebugDataset(size=10, offset=1) D3 = DebugDataset(size=10, offset=2) D4 = DebugDataset(size=10, offset=3) E = ExampleConcatenatedDataset(D1, D2, D3, D4) E.set_example_pars(start=1, step=2) assert len(E) == 10 d = E[2] assert d == {"val": [2 + 1, 2 + 3], "index_": 2, "other": [2 + 1, 2 + 3]} assert len(E.labels["label1"]) == 10 assert np.all(E.labels["label1"] == [[i + 1, i + 3] for i in range(10)]) E.set_example_pars(start=0, stop=-1, step=2) assert len(E) == 10 d = E[2] assert d == {"val": [2, 2 + 2], "index_": 2, "other": [2, 2 + 2]} assert len(E.labels["label1"]) == 10 assert np.all(E.labels["label1"] == [[i, i + 2] for i in range(10)]) E.set_example_pars(start=1, stop=-1, step=2) assert len(E) == 10 d = E[2] assert d == {"val": [2 + 1], "index_": 2, "other": [2 + 1]} assert len(E.labels["label1"]) == 10 assert np.all(E.labels["label1"] == [[i + 1] for i in range(10)])
def test_unseq_vanilla(): D1 = DebugDataset(size=10) D2 = DebugDataset(size=10) D = D1 + D2 S = SequenceDataset(D, 3, fid_key="label1") U = UnSequenceDataset(S) print(np.shape(U.labels["label1"])) assert len(U) == len(S) * 3 assert len(U.labels["label1"]) == len(S.labels["label1"]) * 3 du = U[[0, 1, 2]] ds = S[0] for k in ds.keys(): if k == "index_": continue for i in range(3): print(k, i) val_u = du[i][k] val_s = ds[k][i] assert val_s == val_u
def test_dset_mxin_ops(): """Basically test the ConcatenatedDataset""" class MyDset(DatasetMixin): def __init__(self): self.data = DebugDataset(size=10) D1 = DebugDataset(size=10) D2 = DebugDataset(size=10) D3 = D1 + D2 assert len(D3) == 20 D3[13] D3 = D2 + D1 assert len(D3) == 20 D3[13] D4 = 3 * D1 assert len(D4) == 30 D4[13] D4[23] D4 = D1 * 3 assert len(D4) == 30 D4[13] D4[23]
def test_ExampleConcatenatedDataset_step(): D1 = DebugDataset(size=10) D2 = DebugDataset(size=10) E = ExampleConcatenatedDataset(D1, D2) E.set_example_pars(step=2) assert len(E) == 10 d = E[2] assert d == {"val": [2], "index_": 2, "other": [2]} assert len(E.labels["label1"]) == 10 assert np.all(E.labels["label1"] == [[i, i] for i in range(10)]) D3 = DebugDataset(size=20) with pytest.raises(AssertionError): ExampleConcatenatedDataset(D1, D3) D4 = DebugDataset(size=10) D5 = DebugDataset(size=10) E = ExampleConcatenatedDataset(D1, D2, D4, D5) E.set_example_pars(step=2) assert len(E) == 10 d = E[2] assert d == {"val": [2, 2], "index_": 2, "other": [2, 2]} assert len(E.labels["label1"]) == 10 assert np.all(E.labels["label1"] == [[i, i] for i in range(10)])
def test_sequence_dset_vanilla(): D1 = DebugDataset(size=10) D2 = DebugDataset(size=10) D3 = DebugDataset(size=10) D = D1 + D2 + D3 S = SequenceDataset(D, 3, fid_key="label1") assert len(S) == (3 * (10 - 2)) assert len(S.labels["label1"]) == (3 * (10 - 2)) d = S[0] ref = { "val": [0, 1, 2], "other": [0, 1, 2], "index_": 0, "dataset_index_": [0, 0, 0], } assert d == ref l = np.array(S.labels["label1"][:3]) refl = np.array([[0, 1, 2], [1, 2, 3], [2, 3, 4]]) assert np.all(l == refl)
def test_seq_wrong_fid_dtype(): D1 = DebugDataset(size=10) D2 = DebugDataset(size=10) D = D1 + D2 D.labels["label1"] = np.array(D.labels["label1"], dtype=float) with pytest.raises(TypeError): S = SequenceDataset(D, 3, fid_key="label1")
def test_seq_offset_fid_strat_reset(): D1 = DebugDataset(size=10) D2 = DebugDataset(size=12, offset=3) D = D1 + D2 S = SequenceDataset(D, 3, fid_key="label1", strategy="reset") assert len(S) == (len(D1) - 2 + len(D2) - 2)
def test_seq_wrong_fid_len(): D1 = DebugDataset(size=10) D2 = DebugDataset(size=10) D = D1 + D2 D.labels["label1"] = np.array(D.labels["label1"])[:10] with pytest.raises(ValueError): S = SequenceDataset(D, 3, fid_key="label1")
def test_DisjunctExampleConcatenatedDataset_not_same_length(): D1 = DebugDataset(size=10) D2 = DebugDataset(size=20) with pytest.raises(AssertionError): D = DisjunctExampleConcatenatedDataset(D1, D2, disjunct=False, same_length=True) D = DisjunctExampleConcatenatedDataset(D1, D2, disjunct=False, same_length=False) assert len(D) == 10
def test_DisjunctExampleConcatenatedDataset_not_disjunct(): D1 = DebugDataset(size=10, offset=0) D2 = DebugDataset(size=10, offset=10) D = DisjunctExampleConcatenatedDataset(D1, D2, disjunct=False) assert len(D) == 10 assert list(D.labels.keys()) == ["label1", "label2"] assert D.labels == D2.labels assert D[2] == {"val": 12, "index_": 2, "other": 12} assert D[2] == D2[2]
def test_extra_labels(): D = DebugDataset(size=10) D.append_labels = True E = ExtraLabelsDataset(D, lambda dset, idx: {"new": idx}) de = E[0] ref = D[0] set_value(ref, "labels_/new", 0) assert de == ref assert "new" in E.labels assert np.all(E.labels["new"] == np.arange(10))
def test_sequence_dset_offset_fid(): D1 = DebugDataset(size=10) D2 = DebugDataset(size=10) D3 = DebugDataset(size=10, offset=3) D4 = DebugDataset(size=10, offset=3) D = D1 + D2 + D3 + D4 labels = np.array(D.labels["label1"]) diffs = labels[1:] - labels[:-1] idxs = np.where(diffs <= 0)[0] + 1 with pytest.raises(ValueError): S = SequenceDataset(D, 3, fid_key="label1")
def test_get_seq(): config = { "dataset": "edflow.data.believers.sequence.getSeqDataset", "seqdataset": { "dataset": "edflow.debug.ConfigDebugDataset", "length": 3, "step": 1, "fid_key": "label1", }, "size": 10, } S1 = getSeqDataset(config) D = DebugDataset(size=10) S2 = SequenceDataset(D, 3, fid_key="label1") assert len(S1) == len(S2) assert len(S1.labels["label1"]) == len(S2.labels["label1"]) assert len(S1) == len(S1.labels["label1"]) s1 = S1[0] s2 = S2[0] assert s1 == s2 assert np.all(S1.labels["label1"] == S2.labels["label1"])
def test_label_dset(): D = DebugDataset(size=10) L = LabelDataset(D) assert L[0] == {"label1": 0, "label2": 0, "index_": 0, "base_index_": 0} assert len(L) == 10 assert len(L.labels["label1"]) == 10
def test_processed_dset_no_update(): D = DebugDataset(size=10) P = ProcessedDataset(D, lambda val, index_, other: {"val": val**2}, False) assert len(D) == len(P) assert len(D.labels["label1"]) == len(P.labels["label1"]) dp = P[0] assert dp == {"val": 0, "index_": 0}
def test_processed_dset(): D = DebugDataset(size=10) P = ProcessedDataset(D, lambda val, index_, other: {"val": val**2}) assert len(D) == len(P) assert len(D.labels["label1"]) == len(P.labels["label1"]) dp = P[0] ref = D[0] ref["val"] = ref["val"]**2 assert dp == ref
def test_DisjunctExampleConcatenatedDataset(): D1 = DebugDataset(size=10, other_labels=False, other_ex_keys=False) D2 = DebugDataset(size=10, other_labels=True, other_ex_keys=True) D3 = DebugDataset(size=10, other_labels=True, other_ex_keys=False) D = DisjunctExampleConcatenatedDataset(D1, D2) assert len(D) == 10 d = D[2] assert d == {"val": 2, "index_": 2, "other": 2, "val_other": 2, "other_other": 2} assert list(D.labels.keys()) == ["label1", "label2", "label1_other", "label2_other"] assert len(D.labels["label1"]) == 10 assert len(D.labels["label1_other"]) == 10 with pytest.raises(AssertionError): DisjunctExampleConcatenatedDataset(D1, D1) D = DisjunctExampleConcatenatedDataset(D1, D3) with pytest.raises(AssertionError): D[2]
def test_debug_dset(): D = DebugDataset(size=10) print( bcolors.WARNING + bcolors.BOLD + "Dear hacker,\nShould you see this message you probably have changed " "something in the DebugDataset code. Please consider that all " "dataset test depend on this piece of code, so you want to be very " "careful what you change.\n Yours sincerely, jhaux" + bcolors.ENDC) assert len(D) == 10 assert len(D.labels["label1"]) == 10 assert D[0] == {"val": 0, "other": 0, "index_": 0} assert np.all(D.labels["label1"] == np.arange(10))
def test_ConcatenatedDataset_balanced(): D1 = DebugDataset(size=10) D2 = DebugDataset(size=20) C = ConcatenatedDataset(D1, D2, balanced=True) assert len(C) == 40 ref = D1[2] ref["dataset_index_"] = 0 ref["index_"] = 2 d = C[2] assert d == ref ref["index_"] = 12 d = C[12] assert d == ref ref = D2[2] ref["dataset_index_"] = 1 ref["index_"] = 22 d = C[22] assert d == ref ref = D2[12] ref["dataset_index_"] = 1 ref["index_"] = 32 d = C[32] assert d == ref assert len(C.labels["label1"]) == 40 lref = D1.labels["label1"][2] l = C.labels["label1"][2] assert l == lref l = C.labels["label1"][12] assert l == lref l = C.labels["label1"][12] assert l == lref
def test_sub(): D = DebugDataset(10) I = np.array([9, 1, 2, 4, 3, 5, 7, 6, 8, 0]) S = SubDataset(D, I) ref0 = {"val": 9, "other": 9, "index_": 0} ref2 = {"val": 2, "other": 2, "index_": 2} ref6 = {"val": 7, "other": 7, "index_": 6} assert S[0] == ref0 assert S[2] == ref2 assert S[6] == ref6 assert all(S.labels["label1"] == I)
def test_ConcatenatedDataset(): D = DebugDataset(size=10) C = ConcatenatedDataset(D, D) assert len(C) == 20 ref = D[2] ref["dataset_index_"] = 0 d = C[2] assert d == ref d = C[12] ref["dataset_index_"] = 1 ref["index_"] = 12 assert d == ref assert len(C.labels["label1"]) == 20 lref = D.labels["label1"][2] l = C.labels["label1"][2] assert l == lref l = C.labels["label1"][12] assert l == lref
if 'model_output.csv' in root: root = root[:-len('model_output.csv')] savename_score = os.path.join(save_dir, 'score.txt') #savename_std = os.path.join(save_dir, 'std.txt') fid_score = np.array(fids).mean() #fid_std = np.array(fids).std() with open(savename_score, 'w+') as f: f.write(str(fid_score)) #with open(savename_std, 'w+') as f: # f.write(str(fid_std)) print('\nFID SCORE: {:.2f}'.format(fid_score)) return {"scalars": {"fid": fid_score}} if __name__ == "__main__": from edflow.debug import DebugDataset from edflow.data.dataset import ProcessedDataset D1 = DebugDataset(size=100) D2 = DebugDataset(size=100) P = lambda *args, **kwargs: {'image': np.ones([256, 256, 3])} D1 = ProcessedDataset(D1, P) D2 = ProcessedDataset(D2, P) print(D1[0]) fid('.', D1, D2, {})
def test_seq_offset_fid_strat_unknown(): D = DebugDataset(size=12, offset=3) with pytest.raises(ValueError): S = SequenceDataset(D, 3, fid_key="label1", strategy="recolorize")
def __init__(self): self.data = DebugDataset(size=10) self.append_labels = True
def __init__(self): self.data = DebugDataset(size=10)
# return {'pid': int(pid), 'vid': 0, 'fid': int(fid), 'action': act} # D = DataFolder(r, # rfn, # lfn, # ['pid', 'vid', 'fid']) # for i in range(10): # d = D[i] # print(',\n '.join(['{}: {}'.format(k, v if not hasattr(v, 'shape') # else v.shape) # for k, v in d.items()])) from edflow.debug import DebugDataset D = DebugDataset() def labels(data, i): return {"fid": i} D = ExtraLabelsDataset(D, labels) print("D") for k, v in D.labels.items(): print(k) print(np.shape(v)) S = SequenceDataset(D, 2) print("S") for k, v in S.labels.items(): print(k) print(np.shape(v))