def test_mdoc(self): jordan = odml.Document(author="Michael Jordan", date=dt.date(1991, 9, 1), version=0.01) mjordan = odml.Document(author="Michael Jordan", date=dt.date(1991, 9, 1), version=0.01) section_bulls = odml.Section( name="Chicago_Bulls", definition="NBA team based in Chicago, IL", type="team") section_nc = odml.Section( name="North_Caroline", definition="NCAA team based in Wilmington, NC", type="team") jordan.append(section_bulls) jordan.append(section_nc) mjordan.append( section_bulls, section_nc, ) self.assertTrue(jordan.sections[0].name == mjordan.sections[0].name) self.assertTrue(jordan.sections[1].name == mjordan.sections[1].name)
def get_odml_doc(nix_file): """ Extracts sections from a provided nix.File and returns sections and a new odml.Document. If the metadata root of the nix.File contains a specific section with the name 'odML document' only the subsections of this section will be returned. Otherwise all sections of the metadata root will be returned. :param nix_file: a nix.File :return: tuple containing an odml.Document and a list of nix.Sections """ if nix_file.find_sections(filtr=lambda x: "odML document" in x.name): doc_section = nix_file.sections['odML document'] # generate odml document attributes = ['author', 'version', 'repository', 'date'] doc_attributes = { attr: doc_section.props['odML {}'.format(attr)].values[0] for attr in attributes if 'odML {}'.format(attr) in doc_section.props } doc_attributes['oid'] = doc_section.id doc = odml.Document(**doc_attributes) elif nix_file.sections: doc = odml.Document() doc_section = nix_file else: raise ValueError('No exportable sections present in nix file.') return doc, doc_section.sections
def test_rdf_subclassing_definitions(self): """ Test that RDF Subclass definitions are written to the resulting graph. """ # -- Test default subclassing doc = odml.Document() _ = odml.Section(name="test_subclassing", type="cell", parent=doc) rdf_writer = RDFWriter([doc]) curr_str = " ".join(rdf_writer.get_rdf_str().split()) self.assertIn("odml:Cell a rdfs:Class ; rdfs:subClassOf odml:Section", curr_str) self.assertIn("odml:Section a rdfs:Class", curr_str) # -- Test multiple entries; a definition should only occur once in an RDF document doc = odml.Document() sec = odml.Section(name="test_subclassing", type="cell", parent=doc) sub_sec = odml.Section(name="test_subclassing", type="cell", parent=sec) _ = odml.Section(name="test_subclassing", type="cell", parent=sub_sec) rdf_writer = RDFWriter([doc]) curr_str = " ".join(rdf_writer.get_rdf_str().split()) self.assertIn("odml:Cell a rdfs:Class ; rdfs:subClassOf odml:Section", curr_str) self.assertIs( curr_str.count( "odml:Cell a rdfs:Class ; rdfs:subClassOf odml:Section"), 1) self.assertIn("odml:Section a rdfs:Class", curr_str) self.assertIs(curr_str.count("odml:Section a rdfs:Class"), 1) # -- Test custom subclassing type_custom_class = "species" custom_class_dict = {type_custom_class: "Species"} doc = odml.Document() _ = odml.Section(name="test_subclassing", type="cell", parent=doc) _ = odml.Section(name="test_custom_subclassing", type=type_custom_class, parent=doc) rdf_writer = RDFWriter([doc], custom_subclasses=custom_class_dict) curr_str = " ".join(rdf_writer.get_rdf_str().split()) self.assertIn("odml:Cell a rdfs:Class ; rdfs:subClassOf odml:Section", curr_str) self.assertIn( "odml:Species a rdfs:Class ; rdfs:subClassOf odml:Section", curr_str) self.assertIn("odml:Section a rdfs:Class", curr_str) # -- Test inactive subclassing doc = odml.Document() _ = odml.Section(name="test_subclassing", type="cell", parent=doc) rdf_writer = RDFWriter([doc], rdf_subclassing=False) curr_str = " ".join(rdf_writer.get_rdf_str().split()) self.assertNotIn("odml:Section a rdfs:Class", curr_str) self.assertNotIn( "odml:Cell a rdfs:Class ; rdfs:subClassOf odml:Section", curr_str)
def setUp(self): self.odml_doc = odml.Document(author='me', date=datetime.date.today(), version='0.0.1', repository='unknown') odml.Section(name='first section', definition='arbitrary definiton', type='testsection', parent=self.odml_doc, reference='reference 1', repository='also unknown', link='???', include=False) odml.Property(name='first property', value=[1, 2, 3], parent=self.odml_doc.sections[0], unit='Volt', uncertainty=3, reference='still unknown', definition='first property recorded', dependency='unknown', dependency_value='also unknown', dtype='int', value_origin='ref 2')
def test_simple_attributes(self): """ This test checks correct writing and loading of 'simple' Document format attributes. """ author = "HPL" version = "ver64" date = "1890-08-20" repository = "invalid" self.doc = odml.Document(author, date, version, repository) jdoc, xdoc, ydoc = self.save_load() # Test correct JSON save and load. self.assertEqual(jdoc.author, author) self.assertEqual(jdoc.version, version) self.assertEqual(str(jdoc.date), date) self.assertEqual(jdoc.repository, repository) # Test correct XML save and load. self.assertEqual(xdoc.author, author) self.assertEqual(xdoc.version, version) self.assertEqual(str(xdoc.date), date) self.assertEqual(xdoc.repository, repository) # Test correct YAML save and load. self.assertEqual(ydoc.author, author) self.assertEqual(ydoc.version, version) self.assertEqual(str(ydoc.date), date) self.assertEqual(ydoc.repository, repository)
def test_doc_repository_attribute_init(self): repo = "http://portal.g-node.org/odml/terminologies/v1.0/terminologies.xml" doc = odml.Document(repository=repo) self.assertEqual(doc._repository, repo, "Document needs to init its baseclass first, as it overwrites " "the repository attribute") self.assertEqual( doc.repository, repo)
def create_compare_test(sections=3, properties=3, levels=1): """ """ doc = odml.Document() def append_children(sec, level): if level < levels: for i in list(range(sections)): sec.append(odml.Section(name='Section' + str(i + 1))) parent = sec['Section' + str(i + 1)] append_children(parent, level + 1) if (i != 2): for j in list(range(properties)): parent.append( odml.Property(name='Property' + str(j + 1), values=[i + j])) else: for j in list(range(properties - 2)): parent.append( odml.Property(name='Property' + str(j + 1), values=[i + j])) parent.append( odml.Property(name='Property' + str(properties), values=[i + properties - 1])) append_children(doc, 0) doc.append(odml.Section(name='One more Section')) parent = doc['One more Section'] parent.append(odml.Property(name='Property2', values=[11])) return doc
def save_metadata(self): trial_name = 'trial_{0:04d}'.format(self.trial_counter) file_list = [f for f in os.listdir(self.data_dir) if f.startswith(trial_name)] """ create a document """ doc = odml.Document() """ create dataset section """ ds = odml.Section('datasets', 'dataset') p = odml.Property('files', None) ds.append(p) for f in file_list: p.append('{0:s}/{1:s}'.format(self.data_dir, f)) doc.append(ds) for t in list(self.metadata_tabs.values()): m = t.metadata() if m.type == 'recording': v = odml.Value(self.record_timestamp, dtype='datetime') m.append(odml.Property('StartTime', v)) doc.append(m) for cam_name, cam in list(self.cameras.items()): s = odml.Section(cam_name,'hardware/camera') if not cam.is_raspicam(): v = odml.Value(frames_per_second, unit="Hz") s.append(odml.Property('Framerate', v)) for p, v in list(cam.get_properties().items()): prop = odml.Property(p, v) s.append(prop) doc.append(s) doc.append(self.event_list) print(self.event_list) from odml.tools.xmlparser import XMLWriter writer = XMLWriter(doc) writer.write_file('{0}/{1}.xml'.format(self.data_dir, trial_name))
def test_doc_repository_attribute_init(self): doc = odml.Document(repository=REPOSITORY) self.assertEqual( doc._repository, REPOSITORY, "Document needs to init its baseclass first, " "as it overwrites the repository attribute") self.assertEqual(doc.repository, REPOSITORY)
def test_merge_overwrite_values_true(self): doc1 = odml.Document() doc1.append(odml.Section('first sec')) doc1.sections[0].append( odml.Property('first prop', values='first value')) doc2 = odml.Document() doc2.append(odml.Section('first sec')) doc2.sections[0].append( odml.Property('first prop', values='second value')) self.test_table.load_from_odmldoc(doc1) self.test_table.merge(doc2, overwrite_values=True) self.assertEqual(len(self.test_table._odmldict[0]['Value']), 1) self.assertEqual(self.test_table._odmldict[0]['Value'][0], doc2.sections[0].properties[0].values[0])
def test_xml_writer_version(self): doc = odml.Document() if sys.version_info < (3, 0): val = unicode(xmlparser.XMLWriter(doc)) else: val = str(xmlparser.XMLWriter(doc)) self.assertIn('version="%s"' % xmlparser.XML_VERSION, val) doc = xmlparser.XMLReader().fromString(val)
def test_merge_update_docprops(self): doc1 = odml.Document(author='me', repository='somewhere', version=1.1, date=None) doc2 = odml.Document(author='', repository='anywhere', version=1.1, date=datetime.date.today()) self.test_table.load_from_odmldoc(doc1) self.test_table.merge(doc2) self.assertEqual(self.test_table._docdict['author'], doc1.author) self.assertEqual(self.test_table._docdict['repository'], doc1.repository) self.assertEqual(self.test_table._docdict['version'], doc1.version) self.assertEqual(self.test_table._docdict['date'], doc2.date)
def setUp(self): self.test_dir = tempfile.mkdtemp("_odmlnix", "test_", tempfile.gettempdir()) self.odml_doc = odml.Document(author='me', date=datetime.date.today(), version='0.0.1', repository='unknown')
def create_ui_doc(author=""): doc = odml.Document(author=author) sec = odml.Section(name="sec", parent=doc) _ = odml.Property(name="prop", parent=sec) for sec in doc.sections: handle_section_import(sec) return doc
def test_section_name_readable(self): """ Test if section name is not uuid and thus more readable. """ doc = odml.Document() sec = odml.Section("sec", parent=doc) sec.name = sec.id res = Validate(doc) self.assertError(res, "Name not assigned")
def create_odmltable_example(): """ """ species = '' gender = '' weight = 0.0 start_date = datetime.date(1, 2, 3) end_date = datetime.date(1, 2, 3) duration = 1 doc = odml.Document() doc.append(odml.Section(name='Subject', definition='Information on the investigated ' 'experimental subject (animal or ' 'person)')) parent = doc['Subject'] parent.append(odml.Section(name='Training', definition='Information on the training given ' 'to subject')) parent.append(odml.Property(name='Species', definition='Binomial species name', values=species, dtype=odml.DType.string)) parent.append(odml.Property(name='Gender', definition='Gender (male or female)', values=gender, dtype=odml.DType.string)) parent.append(odml.Property(name='Weight', values=weight, dtype=odml.DType.float, unit='kg', uncertainty=5)) parent = doc['Subject']['Training'] parent.append(odml.Property(name='PeriodStart', definition='start date of training', values=start_date, dtype=odml.DType.date)) parent.append(odml.Property(name='PeriodEnd', definition='end date of training', values=end_date, dtype=odml.DType.date)) parent.append(odml.Property(name='Duration', definition='Duration of the training', values=duration, dtype=odml.DType.int, unit='work days')) return doc
def test_property_name_readable(self): """ Test if property name is not uuid and thus more readable. """ doc = odml.Document() sec = odml.Section("sec", parent=doc) prop = odml.Property("prop", parent=sec) prop.name = prop.id res = Validate(doc) self.assertError(res, "Name not assigned")
def test_mdoc_property_add(self): jordan = odml.Document(author="Michael Jordan", date=dt.date(1991, 9, 1), version=0.01) prop1 = odml.Property(name="Coach", value="Phil Jackson", definition="Run the team.") self.assertRaises(KeyError, lambda: jordan.append(prop1))
def create_showall_test_odml(): """ creates a test-odml-document specifically to test the 'showall'-attributes (showall_sections, showall_properties, showall_value_information) to test if the right cells are left free """ doc = odml.Document() doc.append(odml.Section(name='section1', definition='sec1')) doc.append(odml.Section(name='section2', definition='sec2')) doc.append(odml.Section(name='section3', definition='sec3')) parent = doc['section1'] parent.append( odml.Property(name='property1', definition='prop1', value=['value1', 'value2', 'value3'], dtype=odml.DType.string, unit='g', uncertainty=1)) parent.append( odml.Property(name='property2', definition='prop2', value='value1', dtype=odml.DType.text, unit='g', uncertainty=1)) parent.append( odml.Property(name='property3', definition='prop3', value='value1', dtype=odml.DType.text, unit='g', uncertainty=1)) parent = doc['section2'] parent.append( odml.Property(name='property1', definition='prop1', value='value1', dtype=odml.DType.string, unit='g', uncertainty=1)) parent = doc['section3'] parent.append( odml.Property(name='property1', definition='prop1', value=['value1', 'value2'], dtype=odml.DType.string, unit='g', uncertainty=1)) return doc
def setUp(self): # Set up test environment self.tmp_dir = create_test_dir(__file__) self.json_file = os.path.join(self.tmp_dir, "test.json") self.xml_file = os.path.join(self.tmp_dir, "test.xml") self.yaml_file = os.path.join(self.tmp_dir, "test.yaml") # Set up odML document stub doc = odml.Document() self.doc = doc
def setUp(self): # Set up test environment self.tmp_dir = tempfile.mkdtemp(suffix=".odml") self.json_file = os.path.join(self.tmp_dir, "test.json") self.xml_file = os.path.join(self.tmp_dir, "test.xml") self.yaml_file = os.path.join(self.tmp_dir, "test.yaml") # Set up odML document stub doc = odml.Document() self.doc = doc
def setUp(self): # Set up test environment self.tmp_dir = create_test_dir(__file__) self.json_file = os.path.join(self.tmp_dir, "test.json") self.xml_file = os.path.join(self.tmp_dir, "test.xml") self.yaml_file = os.path.join(self.tmp_dir, "test.yaml") # Set up odML document stub doc = odml.Document() _ = odml.Section(name="properties", type="test", parent=doc) self.doc = doc
def test_merge_sections(self): # set up 2 odmls with partially overlapping sections doc1 = odml.Document(author='Me') doc2 = odml.Document(author='You') doc1.extend([odml.Section('MySection'), odml.Section('OurSection')]) doc2.extend([odml.Section('YourSection'), odml.Section('OurSection')]) # adding properties to sections, because odml is omitting sections without properties for sec in doc1.sections + doc2.sections: sec.append(odml.Property('prop')) table1 = OdmlTable(load_from=doc1) table2 = OdmlTable(load_from=doc2) table1.merge(table2, strict=False) result = table1.convert2odml() expected = ['MySection', 'OurSection', 'YourSection'] self.assertListEqual([s.name for s in result.sections], expected)
def test_merge_append_identical_value(self): doc1 = odml.Document() doc1.append(odml.Section('first sec')) doc1.sections[0].append( odml.Property('first prop', values=['value 1', 'value 2'])) doc2 = odml.Document() doc2.append(odml.Section('first sec')) doc2.sections[0].append( odml.Property('first prop', values=['value 2', 'value 3'])) self.test_table.load_from_odmldoc(doc1) self.test_table.merge(doc2, overwrite_values=False) self.assertEqual(len(self.test_table._odmldict[0]['Value']), 3) expected = doc1.sections[0].properties[0].values + doc2.sections[ 0].properties[0].values expected = list(set(expected)) # comparing as set to disregard item order self.assertEqual(set(self.test_table._odmldict[0]['Value']), set(expected))
def setUp(self): # Set up test environment self.tmp_dir = tempfile.mkdtemp(suffix=".odml") self.json_file = os.path.join(self.tmp_dir, "test.json") self.xml_file = os.path.join(self.tmp_dir, "test.xml") self.yaml_file = os.path.join(self.tmp_dir, "test.yaml") # Set up odML document stub doc = odml.Document() _ = odml.Section(name="dtypes", type="test", parent=doc) self.doc = doc
def test_section_unique_ids(self): """ Test if identical ids in sections raise a validation error. """ doc = odml.Document() sec = odml.Section("sec", parent=doc) csec = sec.clone(keep_id=True) sec.append(csec) res = validate(doc) self.assertError(res, "Duplicate id in Section")
def test_property_values_cardinality(self): doc = odml.Document() sec = odml.Section(name="sec", type="sec_type", parent=doc) # Test no caught warning on empty cardinality prop = odml.Property(name="prop_empty_cardinality", values=[1, 2, 3, 4], parent=sec) # Check that the current property is not in the list of validation warnings or errors for err in Validate(doc).errors: self.assertNotEqual(err.obj.id, prop.id) # Test no warning on valid cardinality prop = odml.Property(name="prop_valid_cardinality", values=[1, 2, 3, 4], val_cardinality=(2, 10), parent=sec) for err in Validate(doc).errors: self.assertNotEqual(err.obj.id, prop.id) # Test maximum value cardinality validation test_val = [1, 2, 3] test_card = 2 prop = odml.Property(name="prop_invalid_max_val", values=test_val, val_cardinality=test_card, parent=sec) test_msg_base = "Property values cardinality violated" test_msg = "%s (maximum %s values, %s found)" % ( test_msg_base, test_card, len(prop.values)) for err in Validate(doc).errors: if err.obj.id == prop.id: self.assertFalse(err.is_error) self.assertIn(test_msg, err.msg) # Test minimum value cardinality validation test_val = "I am a nice text to test" test_card = (4, None) prop = odml.Property(name="prop_invalid_min_val", values=test_val, val_cardinality=test_card, parent=sec) test_msg = "%s (minimum %s values, %s found)" % ( test_msg_base, test_card[0], len(prop.values)) for err in Validate(doc).errors: if err.obj.id == prop.id: self.assertFalse(err.is_error) self.assertIn(test_msg, err.msg)
def setUp(self): # Set up test environment self.xmlfile = os.path.join(RES_DIR, "version_conversion_int.xml") self.tmp_dir = create_test_dir(__file__) self.outfile = os.path.join(self.tmp_dir, "xml_writer.xml") doc = odml.Document() sec = doc.create_section(name="sec", type="test") _ = sec.create_property(name="prop", value=['a', 'b', 'c', 'μ']) self.doc = doc self.writer = XMLWriter(doc)
def new(self, doc=None): """ initialize a new document """ if doc is None: doc = odml.Document() sec = odml.Section(name="Default Section") doc.append(sec) self.window.registry.add(doc) self.document = doc self.file_uri = None
def parse(data): """ Parses strings to quickly create odml-documents e.g.: s1[t1] mapping [T1] - p1 s2[t1] - s21[t2] linked to /s1/s2 """ lines = data.strip(" ").strip("\n").split("\n") offset = len(re.compile(r'(\s*)').match(lines[0]).group()) pat = re.compile(r'(?P<name>\w+)(\[(?P<type>\w+)\])?(\s+mapping \[(?P<dst>' r'[\w:]+)\])?(\s+linked to (?P<link>[\w/]+))?') parents = [odml.Document(), None] for line in lines: line = line[offset:] while len(parents) > 1: parp_ref = (len(parents) - 2) * 2 if line.startswith(" " * parp_ref): line = line[parp_ref:] break parents.pop() if line.startswith('- '): line = line[2:] else: parents.pop() try: match_line = pat.match(line).groupdict() except: print("error parsing", repr(line)) raise if match_line['type'] is None: obj = odml.Property(name=match_line['name'], value="[val]") else: obj = odml.Section(name=match_line['name'], type=match_line['type']) if match_line['dst'] is not None: obj.mapping = 'map#%s' % match_line['dst'] if match_line['link'] is not None: obj._link = match_line['link'] parents[-1].append(obj) parents.append(obj) return parents[0]