|
| 1 | +import datetime |
| 2 | +from unittest import TestCase |
| 3 | + |
| 4 | +import nyoka.base.constants as nyoka_constants |
| 5 | +import xmltodict |
| 6 | + |
| 7 | +import aix360.algorithms.rule_induction.trxf.pmml_export.models as models |
| 8 | +import aix360.algorithms.rule_induction.trxf.pmml_export.serializer as serializer |
| 9 | + |
| 10 | + |
| 11 | +class TestNyokaSerializer(TestCase): |
| 12 | + now = datetime.datetime.now() |
| 13 | + nyokaSerializer = serializer.NyokaSerializer(timestamp=now) |
| 14 | + |
| 15 | + def test_serialize_model_with_version_and_header(self): |
| 16 | + # arrange |
| 17 | + model = models.SimplePMMLRuleSetModel(dataDictionary=None, ruleSetModel=None) # noqa |
| 18 | + |
| 19 | + # when |
| 20 | + serialized = self.nyokaSerializer.serialize(model) |
| 21 | + |
| 22 | + # assert |
| 23 | + expected = ''' |
| 24 | + <PMML xmlns="http://www.dmg.org/PMML-4_4" version="{version}"> |
| 25 | + <Header copyright="Copyright IBM Corp, exported to PMML by Nyoka (c) 2022 Software AG" |
| 26 | + description="Default description"> |
| 27 | + <Application name="SimpleRuleSetExport" version="0.0.1"/> |
| 28 | + <Timestamp>{time}</Timestamp> |
| 29 | + </Header> |
| 30 | + </PMML> |
| 31 | + '''.format(version=nyoka_constants.PMML_SCHEMA.VERSION, time=self.now) |
| 32 | + self.assertEqual(xmltodict.parse(xml_input=serialized), xmltodict.parse(xml_input=expected)) |
| 33 | + |
| 34 | + def test_serialize_data_dictionary(self): |
| 35 | + # arrange |
| 36 | + srz = serializer.NyokaSerializer() |
| 37 | + data_dictionary = models.DataDictionary( |
| 38 | + dataFields=[ |
| 39 | + models.DataField(name='toto0', optype=models.OpType.continuous, dataType=models.DataType.float), |
| 40 | + models.DataField(name='toto1', optype=models.OpType.ordinal, dataType=models.DataType.string), |
| 41 | + models.DataField(name='toto2', optype=models.OpType.categorical, dataType=models.DataType.boolean), |
| 42 | + models.DataField(name='toto3', optype=models.OpType.categorical, dataType=models.DataType.integer)]) |
| 43 | + model = models.SimplePMMLRuleSetModel(dataDictionary=data_dictionary, ruleSetModel=None) # noqa |
| 44 | + |
| 45 | + # when |
| 46 | + serialized = self.nyokaSerializer.serialize(model) |
| 47 | + res_data_dictionary_dict = xmltodict.parse(xml_input=serialized)['PMML']['DataDictionary'] |
| 48 | + |
| 49 | + # assert |
| 50 | + expected = ''' |
| 51 | + <DataDictionary numberOfFields="4"> |
| 52 | + <DataField name="toto0" optype="continuous" dataType="float"/> |
| 53 | + <DataField name="toto1" optype="ordinal" dataType="string"/> |
| 54 | + <DataField name="toto2" optype="categorical" dataType="boolean"/> |
| 55 | + <DataField name="toto3" optype="categorical" dataType="integer"/> |
| 56 | + </DataDictionary> |
| 57 | + ''' |
| 58 | + self.assertEqual(res_data_dictionary_dict, xmltodict.parse(xml_input=expected)['DataDictionary']) |
| 59 | + |
| 60 | + def test_should_raise_error_if_categorical_value_is_not_string(self): |
| 61 | + with self.assertRaises(ValueError): |
| 62 | + models.DataDictionary( |
| 63 | + dataFields=[ |
| 64 | + models.DataField( |
| 65 | + name='toto0', |
| 66 | + optype=models.OpType.categorical, |
| 67 | + dataType=models.DataType.float, |
| 68 | + values=[models.Value(value='unexpected')])]) |
| 69 | + with self.assertRaises(ValueError): |
| 70 | + models.DataDictionary( |
| 71 | + dataFields=[ |
| 72 | + models.DataField( |
| 73 | + name='toto0', |
| 74 | + optype=models.OpType.continuous, |
| 75 | + dataType=models.DataType.string, |
| 76 | + values=[models.Value(value='unexpected')])]) |
| 77 | + |
| 78 | + def test_serialize_data_dictionary_with_categorical_value(self): |
| 79 | + # arrange |
| 80 | + data_dictionary = models.DataDictionary( |
| 81 | + dataFields=[ |
| 82 | + models.DataField( |
| 83 | + name='toto0', |
| 84 | + optype=models.OpType.categorical, |
| 85 | + dataType=models.DataType.string, |
| 86 | + values=[ |
| 87 | + models.Value(value='val1'), |
| 88 | + models.Value(value='val2', property=models.Restriction.valid), |
| 89 | + models.Value(value='unknown', property=models.Restriction.invalid), |
| 90 | + models.Value(value='unknown', property=models.Restriction.missing)])]) |
| 91 | + model = models.SimplePMMLRuleSetModel(dataDictionary=data_dictionary, ruleSetModel=None) # noqa |
| 92 | + |
| 93 | + # when |
| 94 | + serialized = self.nyokaSerializer.serialize(model) |
| 95 | + res_data_dictionary_dict = xmltodict.parse(xml_input=serialized)['PMML']['DataDictionary'] |
| 96 | + |
| 97 | + # assert |
| 98 | + expected = ''' |
| 99 | + <DataDictionary numberOfFields="1"> |
| 100 | + <DataField name="toto0" optype="categorical" dataType="string"> |
| 101 | + <Value value="val1"/> |
| 102 | + <Value value="val2"/> |
| 103 | + <Value value="unknown" property="invalid"/> |
| 104 | + <Value value="unknown" property="missing"/> |
| 105 | + </DataField> |
| 106 | + </DataDictionary> |
| 107 | + ''' |
| 108 | + self.assertEqual(res_data_dictionary_dict, xmltodict.parse(xml_input=expected)['DataDictionary']) |
| 109 | + |
| 110 | + def test_serialize_mining_schema(self): |
| 111 | + # arrange |
| 112 | + mining_schema = models.MiningSchema( |
| 113 | + miningFields=[ |
| 114 | + models.MiningField(name='toto0', usageType=models.MiningFieldUsageType.active), |
| 115 | + models.MiningField(name='toto1', usageType=models.MiningFieldUsageType.target)]) |
| 116 | + model = models.SimplePMMLRuleSetModel( |
| 117 | + dataDictionary=models.DataDictionary(dataFields=None), # noqa |
| 118 | + ruleSetModel=models.RuleSetModel(miningSchema=mining_schema, ruleSet=None)) # noqa |
| 119 | + |
| 120 | + # when |
| 121 | + serialized = self.nyokaSerializer.serialize(model) |
| 122 | + res_data_dictionary_dict = xmltodict.parse(xml_input=serialized)['PMML']['RuleSetModel']['MiningSchema'] |
| 123 | + |
| 124 | + # assert |
| 125 | + expected = ''' |
| 126 | + <MiningSchema> |
| 127 | + <MiningField name="toto0" usageType="active"/> |
| 128 | + <MiningField name="toto1" usageType="target"/> |
| 129 | + </MiningSchema> |
| 130 | + ''' |
| 131 | + self.assertEqual(res_data_dictionary_dict, xmltodict.parse(xml_input=expected)['MiningSchema']) |
0 commit comments