def infer_meta(data, metauri, uri): """Infer dlite metadata from Pandas dataframe `data`. `metauri` is a namespace/version/name URI for the metadata. `uri` is the location of the input storage. """ if not metauri: ext = Path(uri).suffix.lstrip('.') fmt = ext if ext else 'csv' with open(uri, 'rb') as f: hash = hashlib.sha256(f.read()).hexdigest() metauri = f'onto-ns.com/meta/1.0/generated_from_{fmt}_{hash}' elif dlite.has_instance(metauri): warnings.warn(f'csv option infer is true, but explicit instance id ' '"{metauri}" already exists') dims_ = [dlite.Dimension('rows', 'Number of rows.')] props = [] for i, col in enumerate(data.columns): name = infer_prop_name(col) type = data.dtypes[i].name dims = ['rows'] unit = infer_prop_unit(col) props.append(dlite.Property(name, type, dims, unit, None, None)) descr = f'Inferred metadata for {uri}' return dlite.Instance.create_metadata(metauri, dims_, props, descr)
import os import importlib import dlite from dlite import Instance from global_dlite_state_mod1 import assert_exists_in_module thisdir = os.path.abspath(os.path.dirname(__file__)) assert len(dlite.istore_get_uuids()) == 3 # 3 Hardcoded dlite instances coll = dlite.Collection() # (1) assert dlite.has_instance(coll.uuid) assert coll.uuid in dlite.istore_get_uuids() assert len(dlite.istore_get_uuids()) == 3 + 1 # Must exist in imported dlite in different module (mod1) assert_exists_in_module(coll.uuid) url = 'json://' + thisdir + '/MyEntity.json' + "?mode=r" e = Instance.create_from_url(url) # (2) assert len(dlite.istore_get_uuids()) == 3 + 2 inst1 = Instance.create_from_metaid(e.uri, [3, 2]) # (3) assert len(dlite.istore_get_uuids()) == 3 + 3 inst2 = Instance.create_from_metaid(e.uri, (3, 4), 'myinst') # (4) assert len(dlite.istore_get_uuids()) == 3 + 4 del inst1 assert len(dlite.istore_get_uuids()) == 3 + 3
myentity.save('json://xxx.json') except dlite.DLiteError: pass else: assert False, 'overwriting single-entity formatted file' # Create an instance of `myentity` with dimensions 2, 3 # For convinience, we give it an unique label "myid" that can be used # interchangable with its uuid inst = Instance.create_from_metaid(myentity.uri, [2, 3], 'myid') assert inst.dimensions == {'N': 2, 'M': 3} assert inst.is_data assert not inst.is_meta assert not inst.is_metameta assert dlite.has_instance(inst.uuid) assert inst.uuid in dlite.istore_get_uuids() # Assign properties inst['a-blob'] = bytearray(b'0123456789abcdef') inst['a-blob'] = b'0123456789abcdef' inst['a-blob-array'] = [[b'abcd', '00112233'], [np.int32(42), b'xyz_']] inst['a-blob-array'] = [[b'0123', b'4567'], [b'89ab', b'cdef']] inst['a-bool'] = False inst['a-bool-array'] = True, False inst['an-int'] = 42 inst['an-int-array'] = 1, 2, 3 inst['a-float'] = 42.3 inst['a-float64-array'] = 3.14, 5.0, 42.3 inst['a-fixstring'] = 'something' inst['a-fixstring-array'] = [['Al', 'X'], ['Mg', 'Si']]
def assert_exists_in_module(uuid): assert dlite.has_instance(uuid) assert uuid in dlite.istore_get_uuids()