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 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 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 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 test_msec(self): # Test case for odml_msec section_bulls = odml.Section( name="Chicago_Bulls", definition="NBA team based in Chicago, IL", type="team" ) section_bulls0 = odml.Section( name="Chicago_Bulls", definition="NBA team based in Chicago, IL", type="team" ) section_bulls2 = odml.Section( name="Chicago_City_Pub", definition="NBA team pub", type="pub" ) section_bull3 = odml.Section( name="Bill_Clinton", definition="President supporter", type="fan" ) prop1 = odml.Property( name="Coach", value="Phil Jackson", definition="Run the team." ) prop2 = odml.Property( name="Euro_player", value="Tony Kukoc", definition="European Player" ) section_bulls.append( section_bulls2, section_bull3, prop1, prop2 ) section_bulls0.append(section_bulls2) section_bulls0.append(section_bull3) section_bulls0.append(prop1) section_bulls0.append(prop2) self.assertTrue(section_bulls.sections[0].name == section_bulls0.sections[0].name) self.assertTrue(section_bulls.sections[1].name == section_bulls0.sections[1].name) self.assertTrue(section_bulls.properties[0].name == section_bulls0.properties[0].name) self.assertTrue(section_bulls.properties[1].name == section_bulls0.properties[1].name)
def test_property_unique_ids(self): """ Test if identical ids in properties raise a validation error """ doc = odml.Document() sec_one = odml.Section("sec1", parent=doc) sec_two = odml.Section("sec2", parent=doc) prop = odml.Property("prop", parent=sec_one) cprop = prop.clone(keep_id=True) sec_two.append(cprop) res = validate(doc) self.assertError(res, "Duplicate id in Property")
def test_section_events(self): s = odml.Section("sec1") p = odml.Property(name="p", value="1") s.append(p) event_log = [] def record(context): event_log.append((context.obj, context.action, context.val)) s.add_change_handler(record) p.value = "2" p.value = "1" log1 = event_log event_log = [] s.remove_change_handler(record) ps = proxy.NonexistantSection("psec") pp = proxy.PropertyProxy(p) ps.append(pp) ps.add_change_handler(record) p.value = "2" p.value = "1" self.assertEqual(log1, event_log)
def test_nix_to_odml_none(self): file_name = 'tmp' nix_path = os.path.join(self.test_dir, file_name + '.nix') odml_path = os.path.join(self.test_dir, file_name + '.xml') odml.Section(name=None, oid=None, definition=None, parent=self.odml_doc, type="MustBeSet", reference=None, repository=None, link=None, include=None) convert.nixwrite(self.odml_doc, nix_path, nix.FileMode.Overwrite) nix_file_r = nix.File.open(nix_path, nix.FileMode.ReadOnly) convert.odmlwrite(nix_file_r, odml_path) odml_doc = odml.load(odml_path) self.assertEqual(len(odml_doc.sections), 1) odml_sec = odml_doc.sections[0] self.assertGreater(len(getattr(odml_sec, "id")), 0) self.assertEqual(getattr(odml_sec, "name"), getattr(odml_sec, "id")) self.assertEqual(getattr(odml_sec, "type"), "MustBeSet") self.assertEqual(getattr(odml_sec, "definition"), None) self.assertEqual(getattr(odml_sec, "reference"), None) self.assertEqual(getattr(odml_sec, "repository"), None) self.assertEqual(getattr(odml_sec, "link"), None) self.assertEqual(getattr(odml_sec, "include"), None) nix_file_r.close()
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 nix_to_odml_recurse(nix_section_list, odml_section): for nix_sec in nix_section_list: info["sections read"] += 1 # extract and convert section attributes from nix # TODO: add 'include' here as soon as available in nix attributes = ['name', 'type', 'definition', 'reference', 'repository', 'link', 'id'] nix_attributes = {attr: getattr(nix_sec, attr) for attr in attributes if hasattr(nix_sec, attr)} nix_attributes['parent'] = odml_section if 'id' in nix_attributes: nix_attributes['oid'] = nix_attributes.pop('id') odml_sec = odml.Section(**nix_attributes) info["sections written"] += 1 for nixprop in nix_sec.props: info["properties read"] += 1 # extract and convert property attributes from nix prop_attributes = ['name', 'values', 'unit', 'uncertainty', 'reference', 'definition', 'dependency', 'dependency_value', 'odml_type', 'value_origin', 'id'] nix_prop_attributes = {attr: getattr(nixprop, attr) for attr in prop_attributes if hasattr(nixprop, attr)} if 'id' in nix_prop_attributes: nix_prop_attributes['oid'] = nix_prop_attributes.pop('id') if 'odml_type' in nix_prop_attributes: nix_prop_attributes['dtype'] = nix_prop_attributes.pop('odml_type').value nix_prop_attributes['parent'] = odml_sec nix_prop_attributes['value'] = list(nix_prop_attributes.pop('values')) odml.Property(**nix_prop_attributes) info["properties written"] += 1 nix_to_odml_recurse(nix_sec.sections, odml_sec)
def test_odml_to_nix_none(self): file_name = 'tmp' nix_path = os.path.join(self.test_dir, file_name + '.nix') odml.Section(name=None, oid=None, definition=None, parent=self.odml_doc, type="MustBeSet", reference=None, repository=None, link=None, include=None) convert.nixwrite(self.odml_doc, nix_path, 'overwrite') nix_file = nix.File.open(nix_path) self.assertEqual(len(nix_file.sections[0].sections), 1) nix_sec = nix_file.sections[0].sections[0] self.assertGreater(len(getattr(nix_sec, "id")), 0) self.assertEqual(getattr(nix_sec, "name"), getattr(nix_sec, "id")) self.assertEqual(getattr(nix_sec, "type"), "MustBeSet") self.assertEqual(getattr(nix_sec, "definition"), None) self.assertEqual(getattr(nix_sec, "reference"), None) self.assertEqual(getattr(nix_sec, "repository"), None) self.assertEqual(getattr(nix_sec, "link"), None) #self.assertEqual(getattr(nix_sec, "include"), None) nix_file.close()
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 add_section(self, widget, obj_section_pair): """ popup menu action: add section add a section to the selected section (or document if None selected) """ (obj, section) = obj_section_pair if section is None: name = self.get_new_obj_name(obj.sections, prefix='Unnamed Section') section = odml.Section(name=name) else: prefix = section.name name = self.get_new_obj_name(obj.sections, prefix=prefix) section = section.clone() section.name = name # It is a terminology section. By default, pull in all its properties. section.merge() cmd = commands.AppendValue(obj=obj, val=section) self.execute(cmd) # Expand tree if the parent object is a section if isinstance(obj, odml.section.Section): tv = self._treeview (model, tree_iter) = tv.get_selection().get_selected() tv.expand_row(model.get_path(tree_iter), False)
def test_paths(self): sec = odml.Section("bar") self.assertEqual(sec._get_relative_path("/a", "/b"), "/b") self.assertEqual(sec._get_relative_path("/a", "/a/b"), "b") self.assertEqual(sec._get_relative_path("/a/b", "/a/c"), "../c") self.assertEqual(sec._get_relative_path("/a/b/c", "/a"), "../..") self.assertEqual(sec._get_relative_path("/a/b", "/a"), "..")
def clicked_record(self): self.record_timestamp = str(datetime.now()).split('.')[0] self.create_and_start_new_videorecordings() self.button_record.setDisabled(True) self.button_cancel.setEnabled(True) self.button_tag.setEnabled(True) self.button_stop.setDisabled(False) self.event_list = odml.Section('events', 'event_list')
def create_2samerows_test_odml(): """ creates an odml-document in which there is a property with two same values to create a table with two rows that are the same """ doc = odml.Document() doc.append(odml.Section(name='section1')) doc.append(odml.Section(name='section2')) parent = doc['section1'] parent.append(odml.Property(name='property1', value=[1, 2, 2])) parent = doc['section2'] parent.append(odml.Property(name='property1', value=2)) return doc
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_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 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 test_standalone_section(self): """ Test if standalone section does not return errors if required attributes are correct. If type is not specified, check error message. """ sec_one = odml.Section("sec1") res = Validate(sec_one) self.assertError(res, "Section type not specified")
def test_proxy_equality(self): p = odml.Property(name="p", value="1") pp = proxy.PropertyProxy(p) self.assertTrue(p == pp) s = odml.Section(name="sec1") ps = proxy.MappedSection(s) s.append(p) self.assertEqual(s, ps)
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') odml.Section(name='first section', parent=self.odml_doc)
def create_electrode_example(): """ """ electrode_type = 'Utah Array' count = 10 electrode_id = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] impedance = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # 40-50 length = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # 2mm-2cm suaids = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # between 1 and 4 doc = odml.Document() doc.append(odml.Section(name='Multielectrode Array')) parent = doc['Multielectrode Array'] parent.append(odml.Property(name='Type', value=electrode_type)) parent.append(odml.Section(name='Electrodes')) parent = doc['Multielectrode Array']['Electrodes'] parent.append(odml.Property(name='count', value=count)) for i in list(range(count)): parent = doc['Multielectrode Array'] sec_name = 'Electrode' + str(i + 1) parent.append(odml.Section(name=sec_name)) parent = doc['Multielectrode Array'][sec_name] parent.append( odml.Property(name='ID', definition='Electrode ID', value=electrode_id[i])) parent.append( odml.Property(name='Impedance', definition='Pre-implantation impedance', value=impedance[i])) parent.append( odml.Property(name='Length', definition='Length', value=length[i])) parent.append( odml.Property(name='SUAIDs', definition='ID of single units', value=suaids[i])) 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 clicked_tag(self): ts = str(datetime.now()).split('.')[0] text, ok = QtGui.QInputDialog.getText(self, 'Tag data with Event', 'Enter tag comment:') if ok: tag_name = 'event_{0:02d}'.format(len(self.tags) + 1) e = odml.Section(tag_name, 'event') e.append(odml.Property('timestamp', ts, dtype='datetime')) e.append(odml.Property('comment', text, dtype='string')) self.event_list.append(e)
def test_event_passing(self): s = odml.Section(name="section") v = odml.Value(1) p = odml.Property(name="prop", value=v) s.append(p) v.data = 4 self.assertEqual(v._modified, [v]) self.assertEqual(p._modified, [v, p]) self.assertEqual(s._modified, [v, p, s]) self.assertEqual(v.data, 4)
def create_section(self, name, depth=0): s = odml.Section(name=name, type=name.replace("sec", "type")) if depth < 1: for i in range(2): s.append(self.create_section("%s,%d" % (name, i), depth=depth+1)) if name.endswith("1"): for i in range(3): s.append(self.create_property("%s:%d" % (name, i))) return s
def test_sec_cardinality(self): """ Test saving and loading of Section sections cardinality variants to and from all supported file formats. """ doc = odml.Document() sec_empty = "card_empty" sec_max = "card_max" sec_min = "card_min" sec_full = "card_full" card_dict = { sec_empty: None, sec_max: (None, 10), sec_min: (2, None), sec_full: (1, 5) } _ = odml.Section(name=sec_empty, type="test", parent=doc) _ = odml.Section(name=sec_max, sec_cardinality=card_dict[sec_max], type="test", parent=doc) _ = odml.Section(name=sec_min, sec_cardinality=card_dict[sec_min], type="test", parent=doc) _ = odml.Section(name=sec_full, sec_cardinality=card_dict[sec_full], type="test", parent=doc) # Test saving to and loading from an XML file odml.save(doc, self.xml_file) xml_doc = odml.load(self.xml_file) self._test_cardinality_load("sec_cardinality", xml_doc, card_dict, sec_empty, sec_max, sec_min, sec_full) # Test saving to and loading from a JSON file odml.save(doc, self.json_file, "JSON") json_doc = odml.load(self.json_file, "JSON") self._test_cardinality_load("sec_cardinality", json_doc, card_dict, sec_empty, sec_max, sec_min, sec_full) # Test saving to and loading from a YAML file odml.save(doc, self.yaml_file, "YAML") yaml_doc = odml.load(self.yaml_file, "YAML") self._test_cardinality_load("sec_cardinality", yaml_doc, card_dict, sec_empty, sec_max, sec_min, sec_full)
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