def test_FormDef(self): formdef = ODM.FormDef(OID="ODM.F.VS", Name="Vital Signs Form", Repeating="Yes") tt1 = ODM.TranslatedText(_content="this is the first test description", lang="en") formdef.Description = ODM.Description() formdef.Description.TranslatedText = [tt1] igr = ODM.ItemGroupRef(ItemGroupOID="ODM.IG.COMMON", Mandatory="Yes", OrderNumber="1") formdef.ItemGroupRef.append(igr) igr = ODM.ItemGroupRef(ItemGroupOID="ODM.IG.VS_GENERAL", Mandatory="Yes", OrderNumber="2") formdef.ItemGroupRef.append(igr) igr = ODM.ItemGroupRef(ItemGroupOID="ODM.IG.VS", Mandatory="Yes", OrderNumber="3") formdef.ItemGroupRef.append(igr) formdef.Alias.append(ODM.Alias(Context="SDTMIG", Name="VS")) fd_dict = formdef.to_dict() print(fd_dict) is_valid = self.validator.verify_conformance(fd_dict, "FormDef") self.assertTrue(is_valid)
def test_add_item_group_ref(self): igr = ODM.ItemGroupRef(ItemGroupOID="ODM.IG.COMMON", Mandatory="Yes", OrderNumber=1) self.formdef.ItemGroupRef.append(igr) igr = ODM.ItemGroupRef(ItemGroupOID="ODM.IG.VS_GENERAL", Mandatory="Yes", OrderNumber=2) self.formdef.ItemGroupRef.append(igr) igr = ODM.ItemGroupRef(ItemGroupOID="ODM.IG.VS", Mandatory="Yes", OrderNumber=3) self.formdef.ItemGroupRef.append(igr) self.assertEqual(self.formdef.ItemGroupRef[0].ItemGroupOID, "ODM.IG.COMMON") self.assertEqual(self.formdef.ItemGroupRef[2].OrderNumber, 3)
def test_append_item_group_ref(self): fd = ODM.FormDef(OID="ODM.F.VS", Name="Vital Signs Form", Repeating="Yes") fd.Description = ODM.Description() fd.Description.TranslatedText.append(ODM.TranslatedText(_content="this is the first test description", lang="en")) fd.ItemGroupRef.append(ODM.ItemGroupRef(ItemGroupOID="ODM.IG.COMMON", Mandatory="Yes", OrderNumber=1)) fd.ItemGroupRef.append(ODM.ItemGroupRef(ItemGroupOID="ODM.IG.VS_GENERAL", Mandatory="Yes", OrderNumber=2)) fd.ItemGroupRef.append(ODM.ItemGroupRef(ItemGroupOID="ODM.IG.VS", Mandatory="Yes", OrderNumber=3)) self.assertEqual(fd.ItemGroupRef[0].ItemGroupOID, "ODM.IG.COMMON") self.assertEqual(fd.ItemGroupRef[2].OrderNumber, 3) self.assertEqual(fd.Description.TranslatedText[0]._content, "this is the first test description")
def test_to_dict(self): attrs = self.set_formdef_attributes() fd = ODM.FormDef(**attrs) fd.Description = ODM.Description() fd.Description.TranslatedText.append(ODM.TranslatedText(_content="this is the first test description", lang="en")) fd.ItemGroupRef.append(ODM.ItemGroupRef(ItemGroupOID="ODM.IG.COMMON", Mandatory="Yes", OrderNumber=1)) fd.ItemGroupRef.append(ODM.ItemGroupRef(ItemGroupOID="ODM.IG.VS_GENERAL", Mandatory="Yes", OrderNumber=2)) fd.ItemGroupRef.append(ODM.ItemGroupRef(ItemGroupOID="ODM.IG.VS", Mandatory="Yes", OrderNumber=3)) fd.Alias.append(ODM.Alias(Context="SDTMIG", Name="VS")) fd_dict = fd.to_dict() self.assertEqual(fd_dict["OID"], "ODM.F.VS") self.assertDictEqual(fd_dict, self.expected_dict())
def test_to_xml(self): attrs = self.set_formdef_attributes() fd = ODM.FormDef(**attrs) fd.Description = ODM.Description() fd.Description.TranslatedText.append(ODM.TranslatedText(_content="this is the first test description", lang="en")) fd.ItemGroupRef.append(ODM.ItemGroupRef(ItemGroupOID="ODM.IG.COMMON", Mandatory="Yes", OrderNumber=1)) fd.ItemGroupRef.append(ODM.ItemGroupRef(ItemGroupOID="ODM.IG.VS_GENERAL", Mandatory="Yes", OrderNumber=2)) fd.ItemGroupRef.append(ODM.ItemGroupRef(ItemGroupOID="ODM.IG.VS", Mandatory="Yes", OrderNumber=3)) fd.Alias.append(ODM.Alias(Context="SDTMIG", Name="VS")) fd_xml = fd.to_xml() self.assertEqual(fd_xml.attrib["OID"], "ODM.F.VS") igr = fd_xml.findall("ItemGroupRef") self.assertEqual(len(igr), 3) self.assertEqual(igr[0].attrib, {"ItemGroupOID": "ODM.IG.COMMON", "Mandatory": "Yes", "OrderNumber": "1"})
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_FD(self): igr1 = ODM.ItemGroupRef(ItemGroupOID="ODM.IG.Common", Mandatory="Yes", OrderNumber=1) igr2 = ODM.ItemGroupRef(ItemGroupOID="ODM.IG.VS_GENERAL", Mandatory="Yes", OrderNumber=2) igr3 = ODM.ItemGroupRef(ItemGroupOID="ODM.IG.VS", Mandatory="Yes", OrderNumber=3) fd1 = ODM.FormDef(OID="ODM.F.VS", Name="Vital Signs", Repeating="No") fd1.ItemGroupRef = [igr1, igr2, igr3] igr4 = ODM.ItemGroupRef(ItemGroupOID="ODM.IG.Common", Mandatory="Yes", OrderNumber=1) igr5 = ODM.ItemGroupRef(ItemGroupOID="ODM.IG.DM", Mandatory="Yes", OrderNumber=2) fd2 = ODM.FormDef(OID="ODM.F.DM", Name="Demographics", Repeating="No") fd2.ItemGroupRef = [igr4, igr5] return [fd1, fd2]
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")