def test_nix_to_odml_empty(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.Property(parent=self.odml_doc.sections[0]) 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[0].props), 1) odml_prop = odml_doc.sections[0].props[0] self.assertGreater(len(getattr(odml_prop, "id")), 0) self.assertEqual(getattr(odml_prop, "name"), getattr(odml_prop, "id")) self.assertEqual(getattr(odml_prop, "values"), []) self.assertEqual(getattr(odml_prop, "dtype"), None) self.assertEqual(getattr(odml_prop, "definition"), None) self.assertEqual(getattr(odml_prop, "unit"), None) self.assertEqual(getattr(odml_prop, "uncertainty"), None) self.assertEqual(getattr(odml_prop, "reference"), None) self.assertEqual(getattr(odml_prop, "dependency"), None) self.assertEqual(getattr(odml_prop, "dependency_value"), None) self.assertEqual(getattr(odml_prop, "value_origin"), None) nix_file_r.close()
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 test_nix_to_odml_datetime(self): file_name_1 = 'tmp' + str(uuid.uuid4()) nix_path_1 = os.path.join(self.test_dir, file_name_1 + '.nix') nix_file_1 = nix.File.open(nix_path_1, nix.FileMode.Overwrite) odml_path_1 = os.path.join(self.test_dir, file_name_1 + '.xml') sec_1 = nix_file_1.create_section(name="section") prop_1 = sec_1.create_property(name="datetime property", values_or_dtype="datetime") prop_1.values = [ '2011-11-01 11:11:11', '2012-12-02 02:02:02', '2012-12-03T03:03:03' ] convert.odmlwrite(nix_file_1, odml_path_1) odml_doc_1 = odml.load(odml_path_1) odml_prop_1 = odml_doc_1.sections[0].props[0] vals_1 = odml_prop_1.values self.assertEqual(getattr(odml_prop_1, "dtype"), odml.DType.datetime) self.assertEqual(len(vals_1), 3) self.assertEqual(vals_1, [ datetime.datetime(2011, 11, 1, 11, 11, 11), datetime.datetime(2012, 12, 2, 2, 2, 2), datetime.datetime(2012, 12, 3, 3, 3, 3) ]) nix_file_1.close() file_name_2 = 'tmp' + str(uuid.uuid4()) nix_path_2 = os.path.join(self.test_dir, file_name_2 + '.nix') nix_file_2 = nix.File.open(nix_path_2, nix.FileMode.Overwrite) odml_path_2 = os.path.join(self.test_dir, file_name_2 + '.xml') sec_2 = nix_file_2.create_section(name="section") prop_2 = sec_2.create_property(name="datetime property 2", values_or_dtype=np.str_) prop_2.values = [ '2012-12-02 12:12:12', '2013-01-01 01:01:01', '2013-01-02T02:02:02' ] setattr(prop_2, "odml_type", nix.OdmlType("datetime")) convert.odmlwrite(nix_file_2, odml_path_2) odml_doc_2 = odml.load(odml_path_2) odml_prop_2 = odml_doc_2.sections[0].props[0] vals = odml_prop_2.values self.assertEqual(getattr(odml_prop_2, "dtype"), odml.DType.datetime) self.assertEqual(len(vals), 3) self.assertEqual(vals, [ datetime.datetime(2012, 12, 2, 12, 12, 12), datetime.datetime(2013, 1, 1, 1, 1, 1), datetime.datetime(2013, 1, 2, 2, 2, 2) ]) nix_file_2.close()
def test_nix_to_odml_float(self): file_name_1 = 'tmp' + str(uuid.uuid4()) nix_path_1 = os.path.join(self.test_dir, file_name_1 + '.nix') nix_file_1 = nix.File.open(nix_path_1, nix.FileMode.Overwrite) odml_path_1 = os.path.join(self.test_dir, file_name_1 + '.xml') sec_1 = nix_file_1.create_section(name="section") prop_1 = sec_1.create_property(name="float property", values_or_dtype=np.float_) prop_1.values = [1.1, 2.2, 3.3] convert.odmlwrite(nix_file_1, odml_path_1) odml_doc_1 = odml.load(odml_path_1) odml_prop_1 = odml_doc_1.sections[0].props[0] vals_1 = odml_prop_1.values self.assertEqual(getattr(odml_prop_1, "dtype"), odml.DType.float) self.assertEqual(len(vals_1), 3) self.assertEqual(vals_1, [1.1, 2.2, 3.3]) nix_file_1.close() file_name_2 = 'tmp' + str(uuid.uuid4()) nix_path_2 = os.path.join(self.test_dir, file_name_2 + '.nix') nix_file_2 = nix.File.open(nix_path_2, nix.FileMode.Overwrite) odml_path_2 = os.path.join(self.test_dir, file_name_2 + '.xml') sec_2 = nix_file_2.create_section(name="section") prop_2 = sec_2.create_property(name="string float property", values_or_dtype=np.str_) prop_2.values = ["4.4", "5.5", "6.6"] convert.odmlwrite(nix_file_2, odml_path_2) odml_doc_2 = odml.load(odml_path_2) odml_prop_2 = odml_doc_2.sections[0].props[0] vals = odml_prop_2.values self.assertEqual(getattr(odml_prop_2, "dtype"), odml.DType.float) self.assertEqual(len(vals), 3) self.assertEqual(vals, [4.4, 5.5, 6.6]) nix_file_2.close() '''
def test_nix_to_odml_string(self): file_name_1 = 'tmp' + str(uuid.uuid4()) nix_path_1 = os.path.join(self.test_dir, file_name_1 + '.nix') nix_file_1 = nix.File.open(nix_path_1, nix.FileMode.Overwrite) odml_path_1 = os.path.join(self.test_dir, file_name_1 + '.xml') sec_1 = nix_file_1.create_section(name="section") prop_1 = sec_1.create_property(name="string property", values_or_dtype=np.str_) prop_1.values = ['a', 'b', 'c'] convert.odmlwrite(nix_file_1, odml_path_1) odml_doc_1 = odml.load(odml_path_1) odml_prop_1 = odml_doc_1.sections[0].props[0] vals_1 = odml_prop_1.values self.assertEqual(getattr(odml_prop_1, "dtype"), odml.DType.string) self.assertEqual(len(vals_1), 3) self.assertEqual(vals_1, ['a', 'b', 'c']) nix_file_1.close() file_name_2 = 'tmp' + str(uuid.uuid4()) nix_path_2 = os.path.join(self.test_dir, file_name_2 + '.nix') nix_file_2 = nix.File.open(nix_path_2, nix.FileMode.Overwrite) odml_path_2 = os.path.join(self.test_dir, file_name_2 + '.xml') sec_2 = nix_file_2.create_section(name="section") prop_2 = sec_2.create_property(name="string property 2", values_or_dtype=np.str_) prop_2.values = ['d', 'e', 'f'] setattr(prop_2, "odml_type", nix.OdmlType("string")) convert.odmlwrite(nix_file_2, odml_path_2) odml_doc_2 = odml.load(odml_path_2) odml_prop_2 = odml_doc_2.sections[0].props[0] vals = odml_prop_2.values self.assertEqual(getattr(odml_prop_2, "dtype"), odml.DType.string) self.assertEqual(len(vals), 3) self.assertEqual(vals, ["d", "e", "f"]) nix_file_2.close()
def test_nix_to_odml_tuple(self): file_name_1 = 'tmp' + str(uuid.uuid4()) nix_path_1 = os.path.join(self.test_dir, file_name_1 + '.nix') nix_file_1 = nix.File.open(nix_path_1, nix.FileMode.Overwrite) odml_path_1 = os.path.join(self.test_dir, file_name_1 + '.xml') sec_1 = nix_file_1.create_section(name="section") prop_1 = sec_1.create_property(name="2-tuple property", values_or_dtype=np.str_) prop_1.values = ["(1; 2)", "(3; 4)"] convert.odmlwrite(nix_file_1, odml_path_1) odml_doc_1 = odml.load(odml_path_1) odml_prop_1 = odml_doc_1.sections[0].props[0] vals_1 = odml_prop_1.values self.assertEqual(getattr(odml_prop_1, "dtype"), "2-tuple") self.assertEqual(len(vals_1), 2) self.assertEqual(vals_1, [["1", "2"], ["3", "4"]]) nix_file_1.close() file_name_2 = 'tmp' + str(uuid.uuid4()) nix_path_2 = os.path.join(self.test_dir, file_name_2 + '.nix') nix_file_2 = nix.File.open(nix_path_2, nix.FileMode.Overwrite) odml_path_2 = os.path.join(self.test_dir, file_name_2 + '.xml') sec_2 = nix_file_2.create_section(name="section") prop_2 = sec_2.create_property(name="3-tuple property", values_or_dtype=np.str_) prop_2.values = ["(1; 2; 3)", "(4; 5; 6)"] convert.odmlwrite(nix_file_2, odml_path_2) odml_doc_2 = odml.load(odml_path_2) odml_prop_2 = odml_doc_2.sections[0].props[0] vals = odml_prop_2.values self.assertEqual(getattr(odml_prop_2, "dtype"), "3-tuple") self.assertEqual(len(vals), 2) self.assertEqual(vals, [["1", "2", "3"], ["4", "5", "6"]]) nix_file_2.close()
def test_nix_to_odml_double(self): file_name_1 = 'tmp' + str(uuid.uuid4()) nix_path_1 = os.path.join(self.test_dir, file_name_1 + '.nix') nix_file_1 = nix.File.open(nix_path_1, nix.FileMode.Overwrite) odml_path_1 = os.path.join(self.test_dir, file_name_1 + '.xml') sec_1 = nix_file_1.create_section(name="section") prop_1 = sec_1.create_property(name="double property", values_or_dtype=np.double) prop_1.values = [1.1, 2.2, 3.3] convert.odmlwrite(nix_file_1, odml_path_1) odml_doc_1 = odml.load(odml_path_1) odml_prop_1 = odml_doc_1.sections[0].props[0] vals_1 = odml_prop_1.values self.assertEqual(getattr(odml_prop_1, "dtype"), odml.DType.float) self.assertEqual(len(vals_1), 3) self.assertEqual(vals_1, [1.1, 2.2, 3.3]) nix_file_1.close() '''
def test_nix_to_odml_text(self): file_name_1 = 'tmp' + str(uuid.uuid4()) nix_path_1 = os.path.join(self.test_dir, file_name_1 + '.nix') nix_file_1 = nix.File.open(nix_path_1, nix.FileMode.Overwrite) odml_path_1 = os.path.join(self.test_dir, file_name_1 + '.xml') sec_1 = nix_file_1.create_section(name="section") prop_1 = sec_1.create_property(name="text property", values_or_dtype=np.str_) prop_1.values = ['a\nb', 'c d', 'e\nix_path'] convert.odmlwrite(nix_file_1, odml_path_1) odml_doc_1 = odml.load(odml_path_1) odml_prop_1 = odml_doc_1.sections[0].props[0] vals_1 = odml_prop_1.values self.assertEqual(getattr(odml_prop_1, "dtype"), odml.DType.text) # this does currently not work as there seems to be a problem # in the odML core lib reading the file including a line break. # self.assertEqual(len(vals), 3) # self.assertEqual(vals, ['a\nb', 'c d', 'e\nix_path']) nix_file_1.close()
def test_double_conversion(self): nf = os.path.join(self.test_dir, 'tmp.nix') of = os.path.join(self.test_dir, 'tmp.odml') convert.nixwrite(self.odml_doc, nf, 'overwrite') nix_file = nix.File(nf) convert.odmlwrite(nix_file, of) odml_doc = odml.load(of) for attr in document_attributes: self.assertEqual(getattr(self.odml_doc, attr), getattr(odml_doc, attr)) for sec in self.odml_doc.sections: sec2 = odml_doc.sections[sec.name] for attr in section_attributes: self.assertEqual(getattr(sec, attr), getattr(sec2, attr)) for prop in sec.properties: prop2 = sec2.properties[prop.name] for attr in property_attributes: self.assertEqual(getattr(prop, attr), getattr(prop2, attr)) nix_file.close()
def test_nix_to_odml_boolean(self): file_name_1 = 'tmp' + str(uuid.uuid4()) nix_path_1 = os.path.join(self.test_dir, file_name_1 + '.nix') nix_file_1 = nix.File.open(nix_path_1, nix.FileMode.Overwrite) odml_path_1 = os.path.join(self.test_dir, file_name_1 + '.xml') sec_1 = nix_file_1.create_section(name="section") prop_1 = sec_1.create_property(name="boolean property", values_or_dtype=np.bool_) prop_1.values = [True, False, True] convert.odmlwrite(nix_file_1, odml_path_1) odml_doc_1 = odml.load(odml_path_1) odml_prop_1 = odml_doc_1.sections[0].props[0] vals_1 = odml_prop_1.values self.assertEqual(getattr(odml_prop_1, "dtype"), odml.DType.boolean) self.assertEqual(len(vals_1), 3) self.assertEqual(vals_1, [True, False, True]) nix_file_1.close() file_name_2 = 'tmp' + str(uuid.uuid4()) nix_path_2 = os.path.join(self.test_dir, file_name_2 + '.nix') nix_file_2 = nix.File.open(nix_path_2, nix.FileMode.Overwrite) odml_path_2 = os.path.join(self.test_dir, file_name_2 + '.xml') sec_2 = nix_file_2.create_section(name="section") prop_2 = sec_2.create_property(name="string boolean property", values_or_dtype=np.str_) prop_2.values = ["True", "False", "TRUE", "FALSE"] convert.odmlwrite(nix_file_2, odml_path_2) odml_doc_2 = odml.load(odml_path_2) odml_prop_2 = odml_doc_2.sections[0].props[0] vals = odml_prop_2.values self.assertEqual(getattr(odml_prop_2, "dtype"), odml.DType.boolean) self.assertEqual(len(vals), 4) self.assertEqual(vals, [True, False, True, False]) nix_file_2.close() file_name_3 = 'tmp' + str(uuid.uuid4()) nix_path_3 = os.path.join(self.test_dir, file_name_3 + '.nix') nix_file_3 = nix.File.open(nix_path_3, nix.FileMode.Overwrite) odml_path_3 = os.path.join(self.test_dir, file_name_3 + '.xml') sec_3 = nix_file_3.create_section(name="section") prop_3 = sec_3.create_property(name="boolean property 3", values_or_dtype=np.bool_) prop_3.values = [False, True, False] setattr(prop_3, "odml_type", nix.OdmlType("boolean")) convert.odmlwrite(nix_file_3, odml_path_3) odml_doc_3 = odml.load(odml_path_3) odml_prop_3 = odml_doc_3.sections[0].props[0] vals = odml_prop_3.values self.assertEqual(getattr(odml_prop_3, "dtype"), odml.DType.boolean) self.assertEqual(len(vals), 3) self.assertEqual(vals, [False, True, False]) nix_file_3.close()
def test_nix_to_odml_int(self): file_name_1 = 'tmp' + str(uuid.uuid4()) nix_path_1 = os.path.join(self.test_dir, file_name_1 + '.nix') nix_file_1 = nix.File.open(nix_path_1, nix.FileMode.Overwrite) odml_path_1 = os.path.join(self.test_dir, file_name_1 + '.xml') sec_1 = nix_file_1.create_section(name="section") prop_1 = sec_1.create_property(name="int property", values_or_dtype=np.int64) prop_1.values = [1, 2, 3] convert.odmlwrite(nix_file_1, odml_path_1) odml_doc_1 = odml.load(odml_path_1) odml_prop_1 = odml_doc_1.sections[0].props[0] vals_1 = odml_prop_1.values self.assertEqual(getattr(odml_prop_1, "dtype"), odml.DType.int) self.assertEqual(len(vals_1), 3) self.assertEqual(vals_1, [1, 2, 3]) nix_file_1.close() file_name_2 = 'tmp' + str(uuid.uuid4()) nix_path_2 = os.path.join(self.test_dir, file_name_2 + '.nix') nix_file_2 = nix.File.open(nix_path_2, nix.FileMode.Overwrite) odml_path_2 = os.path.join(self.test_dir, file_name_2 + '.xml') sec_2 = nix_file_2.create_section(name="section") prop_2 = sec_2.create_property(name="string int property", values_or_dtype=np.str_) prop_2.values = ["4", "5", "6"] convert.odmlwrite(nix_file_2, odml_path_2) odml_doc_2 = odml.load(odml_path_2) odml_prop_2 = odml_doc_2.sections[0].props[0] vals = odml_prop_2.values self.assertEqual(getattr(odml_prop_2, "dtype"), odml.DType.int) self.assertEqual(len(vals), 3) self.assertEqual(vals, [4, 5, 6]) nix_file_2.close() file_name_3 = 'tmp' + str(uuid.uuid4()) nix_path_3 = os.path.join(self.test_dir, file_name_3 + '.nix') nix_file_3 = nix.File.open(nix_path_3, nix.FileMode.Overwrite) odml_path_3 = os.path.join(self.test_dir, file_name_3 + '.xml') sec_3 = nix_file_3.create_section(name="section") prop_3 = sec_3.create_property(name="int property 3", values_or_dtype=np.int64) prop_3.values = [7, 8, 9] setattr(prop_3, "odml_type", nix.OdmlType("int")) convert.odmlwrite(nix_file_3, odml_path_3) odml_doc_3 = odml.load(odml_path_3) odml_prop_3 = odml_doc_3.sections[0].props[0] vals = odml_prop_3.values self.assertEqual(getattr(odml_prop_3, "dtype"), odml.DType.int) self.assertEqual(len(vals), 3) self.assertEqual(vals, [7, 8, 9]) nix_file_3.close()
def test_example_nix_file(self): file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), '../odmlfiles/test') nix_file = nix.File.open(file_path + '.nix', nix.FileMode.ReadOnly) convert.odmlwrite(nix_file, file_path + '.xml') nix_file.close()