1
+ import typing
1
2
import datetime
2
3
import io
3
4
@@ -16,36 +17,49 @@ class NyokaSerializer(AbstractSerializer):
16
17
def __init__ (self , timestamp : datetime = None ):
17
18
self ._timestamp = timestamp
18
19
19
- def serialize (self , simple_pmml_ruleset_model : models .SimplePMMLRuleSetModel , timestamp : datetime = None ) -> str :
20
- pmml_model = self ._nyoka_pmml_model (simple_pmml_ruleset_model )
20
+ def serialize (self , model : typing .Union [models .SimplePMMLRuleSetModel , models .Scorecard ]) -> str :
21
+ if isinstance (model , models .SimplePMMLRuleSetModel ):
22
+ pmml_model = self ._nyoka_pmml_model (
23
+ model ,
24
+ RuleSetModel = None if model .ruleSetModel is None else [self ._nyoka_rule_set_model (model .ruleSetModel )])
25
+ elif isinstance (model , models .Scorecard ):
26
+ pmml_model = self ._nyoka_pmml_model (
27
+ model ,
28
+ Scorecard = None if model is None else [self ._nyoka_scorecard_model (model )])
29
+ else :
30
+ raise NotImplemented
21
31
string_io = io .StringIO ()
22
32
pmml_model .export (outfile = string_io , level = 0 )
23
33
return string_io .getvalue ()
24
34
25
- def _nyoka_pmml_model (self , simple_pmml_ruleset_model : models .SimplePMMLRuleSetModel ) -> nyoka_pmml .PMML :
26
- timestamp = datetime .datetime .now () if self ._timestamp is None else self ._timestamp
35
+ def _nyoka_pmml_model (
36
+ self ,
37
+ model : typing .Union [models .SimplePMMLRuleSetModel , models .Scorecard ],
38
+ ** build_args
39
+ ) -> nyoka_pmml .PMML :
27
40
return nyoka_pmml .PMML (
28
41
version = nyoka_constants .PMML_SCHEMA .VERSION ,
29
42
Header = nyoka_pmml .Header (
30
43
copyright = NyokaSerializer .COPYRIGHT_STRING ,
31
44
description = nyoka_constants .HEADER_INFO .DEFAULT_DESCRIPTION ,
32
- Timestamp = nyoka_pmml .Timestamp (timestamp ),
33
- Application = nyoka_pmml .Application (
34
- name = NyokaSerializer .APPLICATION_NAME , version = version .version )),
35
- DataDictionary = None if simple_pmml_ruleset_model .dataDictionary is None else self ._nyoka_data_dictionary (
36
- simple_pmml_ruleset_model .dataDictionary ),
37
- RuleSetModel = None if simple_pmml_ruleset_model .ruleSetModel is None else [
38
- self ._nyoka_rule_set_model (simple_pmml_ruleset_model .ruleSetModel )])
45
+ Timestamp = nyoka_pmml .Timestamp (datetime .datetime .now () if self ._timestamp is None else self ._timestamp ),
46
+ Application = nyoka_pmml .Application (name = NyokaSerializer .APPLICATION_NAME , version = version .version )),
47
+ DataDictionary = None if model .dataDictionary is None else self ._nyoka_data_dictionary (model .dataDictionary ),
48
+ ** build_args )
39
49
40
50
def _nyoka_data_dictionary (self , data_dictionary : models .DataDictionary ) -> nyoka_pmml .DataDictionary :
41
51
return nyoka_pmml .DataDictionary (
42
- numberOfFields = 0 if data_dictionary .dataFields is None else len (data_dictionary .dataFields ),
43
- DataField = None if data_dictionary .dataFields is None else [
44
- nyoka_pmml .DataField (name = f .name , optype = f .optype .name , dataType = f .dataType .name ,
45
- Value = None if f .values is None else [
46
- nyoka_pmml .Value (value = v .value ) for v in f .values
47
- ])
48
- for f in data_dictionary .dataFields ])
52
+ numberOfFields = 0 if not data_dictionary .dataFields else len (data_dictionary .dataFields ),
53
+ DataField = None if not data_dictionary .dataFields else [
54
+ self ._nyoka_data_field (f ) for f in data_dictionary .dataFields ])
55
+
56
+ def _nyoka_data_field (self , data_field : models .DataField ) -> nyoka_pmml .DataField :
57
+ return nyoka_pmml .DataField (
58
+ name = data_field .name ,
59
+ optype = data_field .optype .name ,
60
+ dataType = data_field .dataType .name ,
61
+ Value = None if not data_field .values else [
62
+ nyoka_pmml .Value (value = val .value , property = val .property .name ) for val in data_field .values ])
49
63
50
64
def _nyoka_rule_set_model (self , rule_set_model : models .RuleSetModel ) -> nyoka_pmml .RuleSetModel :
51
65
return nyoka_pmml .RuleSetModel (
@@ -94,3 +108,55 @@ def _nyoka_rule(self, simple_rule: models.SimpleRule) -> nyoka_pmml.SimpleRule:
94
108
nbCorrect = simple_rule .nbCorrect ,
95
109
confidence = simple_rule .confidence ,
96
110
weight = simple_rule .weight )
111
+
112
+ def _nyoka_scorecard_model (self , scorecard : models .Scorecard ) -> nyoka_pmml .Scorecard :
113
+ return nyoka_pmml .Scorecard (
114
+ functionName = 'regression' ,
115
+ algorithmName = 'ScoreCard' ,
116
+ MiningSchema = None if scorecard .miningSchema is None else self ._nyoka_mining_schema (
117
+ scorecard .miningSchema ),
118
+ initialScore = scorecard .initialScore ,
119
+ useReasonCodes = "false" ,
120
+ Output = None if scorecard .output is None else nyoka_pmml .Output (
121
+ OutputField = [
122
+ nyoka_pmml .OutputField (
123
+ name = outputField .name ,
124
+ feature = outputField .feature ,
125
+ dataType = outputField .dataType .name ,
126
+ optype = outputField .optype .name ) for outputField in scorecard .output .outputFields ]),
127
+ Characteristics = None if scorecard .characteristics is None else self ._nyoka_pmml_characteristics (
128
+ scorecard .characteristics ))
129
+
130
+ def _nyoka_pmml_characteristics (self , characteristics : models .Characteristics ) -> nyoka_pmml .Characteristics :
131
+ return nyoka_pmml .Characteristics (
132
+ Characteristic = [
133
+ nyoka_pmml .Characteristic (
134
+ name = characteristic .name ,
135
+ Attribute = [self ._nyoka_pmml_attributes (attribute ) for attribute in characteristic .attributes ])
136
+ for characteristic in characteristics .characteristics ])
137
+
138
+ def _nyoka_pmml_attributes (self , attribute : models .Attribute ) -> nyoka_pmml .Attribute :
139
+ return nyoka_pmml .Attribute (
140
+ partialScore = attribute .score if not isinstance (attribute .score , models .ComplexPartialScore ) else None ,
141
+ ComplexPartialScore = nyoka_pmml .ComplexPartialScore (
142
+ Apply = nyoka_pmml .Apply (
143
+ function = '+' ,
144
+ Apply_member = [nyoka_pmml .Apply (
145
+ function = '*' ,
146
+ FieldRef = [nyoka_pmml .FieldRef (field = attribute .score .feature_name )],
147
+ Constant = [nyoka_pmml .Constant (valueOf_ = attribute .score .multiplier )])],
148
+ Constant = [nyoka_pmml .Constant (valueOf_ = attribute .score .constant )])) if isinstance (
149
+ attribute .score , models .ComplexPartialScore ) else None ,
150
+ SimplePredicate = None if (attribute .predicate is None or not isinstance (
151
+ attribute .predicate , models .SimplePredicate )) else nyoka_pmml .SimplePredicate (
152
+ field = attribute .predicate .field ,
153
+ operator = attribute .predicate .operator .name ,
154
+ value = attribute .predicate .value ),
155
+ CompoundPredicate = None if (attribute .predicate is None or not isinstance (
156
+ attribute .predicate , models .CompoundPredicate )) else nyoka_pmml .CompoundPredicate (
157
+ booleanOperator = attribute .predicate .booleanOperator .name ,
158
+ SimplePredicate = [
159
+ nyoka_pmml .SimplePredicate (field = sp .field , operator = sp .operator .name , value = sp .value )
160
+ for sp in attribute .predicate .simplePredicates ]),
161
+ True_ = None if (attribute .predicate is None or not isinstance (
162
+ attribute .predicate , models .TruePredicate )) else nyoka_pmml .True_ ())
0 commit comments