def test_refdef_orphans(self): attrs = { "OID": "MDV.TRACE-XML-ODM-01", "Name": "TRACE-XML MDV", "Description": "Trace-XML Example" } self.mdv = ODM.MetaDataVersion(**attrs) self.mdv.Protocol = self.add_protocol() self.mdv.StudyEventDef = self.add_SED() self.mdv.FormDef = self.add_FD() self.mdv.ItemGroupDef = self.add_IGD() self.mdv.ItemDef = self.add_ITD() self.mdv.CodeList = self.add_CL() self.mdv.MethodDef = self.add_MD() self.mdv.ConditionDef = self.add_CD() # oid uniqueness check and ref/def init self.mdv.verify_oids(self.oid_checker) orphans = self.mdv.unreferenced_oids(self.oid_checker) expected_orphans = { 'MDV.TRACE-XML-ODM-01': 'MetaDataVersionOID', 'ODM.IT.VS.BP.VSORRESU': 'ItemOID', 'ODM.CL.NY_SUB_Y_N': 'CodeListOID', 'ODM.MT.DOB': 'MethodOID', 'ODM.CD.BRTHMO': 'CollectionExceptionConditionOID' } self.assertDictEqual(orphans, expected_orphans)
def test_refdef_orphans_unitialized(self): attrs = { "OID": "MDV.TRACE-XML-ODM-01", "Name": "TRACE-XML MDV", "Description": "Trace-XML Example" } self.mdv = ODM.MetaDataVersion(**attrs) self.mdv.Protocol = self.add_protocol() self.mdv.StudyEventDef = self.add_SED() self.mdv.FormDef = self.add_FD() self.mdv.ItemGroupDef = self.add_IGD() self.mdv.ItemDef = self.add_ITD() self.mdv.CodeList = self.add_CL() self.mdv.MethodDef = self.add_MD() self.mdv.ConditionDef = self.add_CD() oid_checker = OID.OIDRef() # use unitialized oid_checker orphans = self.mdv.unreferenced_oids(oid_checker) # returns an empty dictionary since oid index has not been created expected_orphans = { 'MDV.TRACE-XML-ODM-01': 'MetaDataVersionOID', 'ODM.IT.VS.BP.VSORRESU': 'ItemOID', 'ODM.CL.NY_SUB_Y_N': 'CodeListOID', 'ODM.MT.DOB': 'MethodOID', 'ODM.CD.BRTHMO': 'CollectionExceptionConditionOID' } self.assertDictEqual(orphans, expected_orphans)
def test_OID_creation_dirty(self): attrs = { "OID": "MDV.TRACE-XML-ODM-01", "Description": "Trace-XML Example" } with self.assertRaises(ValueError): # MDV creation should fail due to missing Name attribute self.mdv = ODM.MetaDataVersion(**attrs)
def setUp(self) -> None: current_datetime = datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc).isoformat() root = ODM.ODM(FileOID="ODM.DEMO.001", Granularity="Metadata", AsOfDateTime=current_datetime, CreationDateTime=current_datetime, ODMVersion="1.3.2", FileType="Snapshot", Originator="swhume", SourceSystem="odmlib", SourceSystemVersion="0.1") # create Study and add to ODM root.Study.append(ODM.Study(OID="ODM.GET.STARTED")) # create the global variables root.Study[0].GlobalVariables = ODM.GlobalVariables() root.Study[0].GlobalVariables.StudyName = ODM.StudyName(_content="Get Started with ODM XML") root.Study[0].GlobalVariables.StudyDescription = ODM.StudyDescription( _content="Demo to get started with odmlib") root.Study[0].GlobalVariables.ProtocolName = ODM.ProtocolName(_content="ODM XML Get Started") # create the MetaDataVersion root.Study[0].MetaDataVersion.append(ODM.MetaDataVersion(OID="MDV.DEMO-ODM-01", Name="Get Started MDV", Description="Get Started Demo")) # create Protocol p = ODM.Protocol() p.Description = ODM.Description() p.Description.TranslatedText.append(ODM.TranslatedText(_content="Get Started Protocol", lang="en")) p.StudyEventRef.append(ODM.StudyEventRef(StudyEventOID="BASELINE", OrderNumber=1, Mandatory="Yes")) root.Study[0].MetaDataVersion[0].Protocol = p # create a StudyEventDef sed = ODM.StudyEventDef(OID="BASELINE", Name="Baseline Visit", Repeating="No", Type="Scheduled") sed.FormRef.append(ODM.FormRef(FormOID="ODM.F.DM", Mandatory="Yes", OrderNumber=1)) root.Study[0].MetaDataVersion[0].StudyEventDef.append(sed) # create a FormDef fd = ODM.FormDef(OID="ODM.F.DM", Name="Demographics", Repeating="No") fd.ItemGroupRef.append(ODM.ItemGroupRef(ItemGroupOID="ODM.IG.DM", Mandatory="Yes", OrderNumber=2)) root.Study[0].MetaDataVersion[0].FormDef.append(fd) # create an ItemGroupDef igd = ODM.ItemGroupDef(OID="ODM.IG.DM", Name="Demographics", Repeating="No") igd.ItemRef.append(ODM.ItemRef(ItemOID="ODM.IT.DM.BRTHYR", Mandatory="Yes")) root.Study[0].MetaDataVersion[0].ItemGroupDef.append(igd) # create an ItemDef itd = ODM.ItemDef(OID="ODM.IT.DM.BRTHYR", Name="Birth Year", DataType="integer") itd.Description = ODM.Description() itd.Description.TranslatedText.append(ODM.TranslatedText(_content="Year of the subject's birth", lang="en")) itd.Question = ODM.Question() itd.Question.TranslatedText.append(ODM.TranslatedText(_content="Birth Year", lang="en")) itd.Alias.append(ODM.Alias(Context="CDASH", Name="BRTHYR")) itd.Alias.append(ODM.Alias(Context="SDTM", Name="BRTHDTC")) root.Study[0].MetaDataVersion[0].ItemDef.append(itd) # save the new ODM document to an ODM XML file root.write_xml(ODM_XML_FILE) # save the same ODM document to a JSON file root.write_json(ODM_JSON_FILE)
def add_mdv(self): attrs = self.set_mdv_attributes() mdv = ODM.MetaDataVersion(**attrs) mdv.Protocol = self.add_protocol() mdv.StudyEventDef = self.add_SED() mdv.FormDef = self.add_FD() mdv.ItemGroupDef = self.add_IGD() mdv.ItemDef = self.add_ITD() mdv.CodeList = self.add_CL() mdv.ConditionDef = self.add_CD() mdv.MethodDef = self.add_MD() return mdv
def test_OID_unique(self): attrs = { "OID": "MDV.TRACE-XML-ODM-01", "Name": "TRACE-XML MDV", "Description": "Trace-XML Example" } self.mdv = ODM.MetaDataVersion(**attrs) self.mdv.Protocol = self.add_protocol() self.mdv.StudyEventDef = self.add_SED() self.mdv.FormDef = self.add_FD() self.mdv.ItemGroupDef = self.add_IGD() self.mdv.ItemDef = self.add_ITD() self.mdv.CodeList = self.add_CL() self.mdv.MethodDef = self.add_MD() self.mdv.ConditionDef = self.add_CD() oid_checker = OID.OIDRef() self.mdv.verify_oids(oid_checker) self.assertTrue(oid_checker.check_oid_refs())
def test_OID_unique_dirty(self): attrs = { "OID": "MDV.TRACE-XML-ODM-01", "Name": "TRACE-XML MDV", "Description": "Trace-XML Example" } self.mdv = ODM.MetaDataVersion(**attrs) self.mdv.Protocol = self.add_protocol() self.mdv.StudyEventDef = self.add_SED() self.mdv.FormDef = self.add_FD() self.mdv.ItemGroupDef = self.add_IGD() self.mdv.ItemDef = self.add_ITD_nonunique() self.mdv.CodeList = self.add_CL() self.mdv.MethodDef = self.add_MD() self.mdv.ConditionDef = self.add_CD() oid_checker = OID.OIDRef() with self.assertRaises(ValueError): # checks for non-unique OIDs and runs the ref/def check self.mdv.verify_oids(oid_checker)
def test_MetaDataVersion(self): attrs = { "OID": "MDV.TRACE-XML-ODM-01", "Name": "TRACE-XML MDV", "Description": "Trace-XML Example" } self.mdv = ODM.MetaDataVersion(**attrs) self.mdv.Protocol = self.add_protocol() self.mdv.StudyEventDef = self.add_SED() self.mdv.FormDef = self.add_FD() self.mdv.ItemGroupDef = self.add_IGD() self.mdv.ItemDef = self.add_ITD() self.mdv.CodeList = self.add_CL() self.mdv.MethodDef = self.add_MD() self.mdv.ConditionDef = self.add_CD() mdv_dict = self.mdv.to_dict() print(mdv_dict) is_valid = self.validator.verify_conformance(mdv_dict, "MetaDataVersion") self.assertTrue(is_valid)
def test_MethodDef_missing_Description(self): attrs = { "OID": "MDV.TRACE-XML-ODM-01", "Name": "TRACE-XML MDV", "Description": "Trace-XML Example" } self.mdv = ODM.MetaDataVersion(**attrs) tt1 = ODM.TranslatedText( _content="Age at Screening Date (Screening Date - Birth date)", lang="en") desc1 = ODM.Description() desc1.TranslatedText.append(tt1) # MethodDef creation that includes the Description object md1 = ODM.MethodDef(OID="ODM.MT.AGE", Name="Algorithm to dervive AGE", Type="Computation", Description=desc1) self.mdv.MethodDef.append(md1) # MethodDef creation succeeds without Description object md2 = ODM.MethodDef(OID="ODM.MT.DOB", Name="Create BRTHDTC from date ELEMENTS", Type="Computation") self.mdv.MethodDef.append(md2) # test that both MethodDefs were added to MDV self.assertEqual(len(self.mdv.MethodDef), 2) # test that the content of Description exists for the first MethodDef self.assertEqual( self.mdv.MethodDef[0].Description.TranslatedText[0]._content, "Age at Screening Date (Screening Date - Birth date)") validator = METADATA.MetadataSchema() # this is how you check conformance that includes required elements, not just attributes is_valid = self.mdv.MethodDef[0].verify_conformance(validator) # Test shows that MethodDef conforms to schema self.assertTrue(is_valid) print(self.mdv.MethodDef[0].to_dict()) print(self.mdv.MethodDef[1].to_dict()) with self.assertRaises(ValueError): # test shows that MethodDef two fails the schema check because it is missing the required Description object self.mdv.MethodDef[1].verify_conformance(validator)
def setUp(self) -> None: attrs = self.set_attributes() self.mdv = ODM.MetaDataVersion(**attrs) self.odm_test_file = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'data', 'test_mdv_001.xml')
def test_xml_to_string(self): current_datetime = datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc).isoformat() root = ODM.ODM(FileOID="ODM.DEMO.001", Granularity="Metadata", AsOfDateTime=current_datetime, CreationDateTime=current_datetime, ODMVersion="1.3.2", FileType="Snapshot", Originator="swhume", SourceSystem="odmlib", SourceSystemVersion="0.1") # create Study and add to ODM root.Study.append(ODM.Study(OID="ODM.GET.STARTED")) # create the global variables root.Study[0].GlobalVariables = ODM.GlobalVariables() root.Study[0].GlobalVariables.StudyName = ODM.StudyName(_content="Get Started with ODM XML") root.Study[0].GlobalVariables.StudyDescription = ODM.StudyDescription( _content="Demo to get started with odmlib") root.Study[0].GlobalVariables.ProtocolName = ODM.ProtocolName(_content="ODM XML Get Started") # create the MetaDataVersion root.Study[0].MetaDataVersion.append(ODM.MetaDataVersion(OID="MDV.DEMO-ODM-01", Name="Get Started MDV", Description="Get Started Demo")) # create Protocol p = ODM.Protocol() p.Description = ODM.Description() p.Description.TranslatedText.append(ODM.TranslatedText(_content="Get Started Protocol", lang="en")) p.StudyEventRef.append(ODM.StudyEventRef(StudyEventOID="BASELINE", OrderNumber=1, Mandatory="Yes")) root.Study[0].MetaDataVersion[0].Protocol = p # create a StudyEventDef sed = ODM.StudyEventDef(OID="BASELINE", Name="Baseline Visit", Repeating="No", Type="Scheduled") sed.FormRef.append(ODM.FormRef(FormOID="ODM.F.DM", Mandatory="Yes", OrderNumber=1)) root.Study[0].MetaDataVersion[0].StudyEventDef.append(sed) # create a FormDef fd = ODM.FormDef(OID="ODM.F.DM", Name="Demographics", Repeating="No") fd.ItemGroupRef.append(ODM.ItemGroupRef(ItemGroupOID="ODM.IG.DM", Mandatory="Yes", OrderNumber=2)) root.Study[0].MetaDataVersion[0].FormDef.append(fd) # create an ItemGroupDef igd = ODM.ItemGroupDef(OID="ODM.IG.DM", Name="Demographics", Repeating="No") igd.ItemRef.append(ODM.ItemRef(ItemOID="ODM.IT.DM.BRTHYR", Mandatory="Yes")) root.Study[0].MetaDataVersion[0].ItemGroupDef.append(igd) # create an ItemDef itd = ODM.ItemDef(OID="ODM.IT.DM.BRTHYR", Name="Birth Year", DataType="integer") itd.Description = ODM.Description() itd.Description.TranslatedText.append(ODM.TranslatedText(_content="Year of the subject's birth", lang="en")) itd.Question = ODM.Question() itd.Question.TranslatedText.append(ODM.TranslatedText(_content="Birth Year", lang="en")) itd.Alias.append(ODM.Alias(Context="CDASH", Name="BRTHYR")) itd.Alias.append(ODM.Alias(Context="SDTM", Name="BRTHDTC")) root.Study[0].MetaDataVersion[0].ItemDef.append(itd) odm_xml_string = root.to_xml_string() # add namespaces nsr = NS.NamespaceRegistry() odm_str = nsr.set_odm_namespace_attributes_string(odm_xml_string) with open("./data/simple_create_from_string.xml", "w") as xml_file: xml_file.write(odm_str) loader = LD.ODMLoader(OL.XMLODMLoader(model_package="odm_1_3_2", ns_uri="http://www.cdisc.org/ns/odm/v1.3")) loader.open_odm_document("./data/simple_create_from_string.xml") mdv = loader.MetaDataVersion() item_list = mdv.ItemDef item = item_list[0] self.assertEqual(item.OID, "ODM.IT.DM.BRTHYR")