@@ -45,7 +45,7 @@ For a complete guide to writing models in the TRAC framework, see the
45
45
46
46
## 📦 Requirements
47
47
48
- * ** Python :** 3.10 or later
48
+ * ** Python :** 3.9 or later
49
49
* ** Tools :** Any popular IDE (PyCharm, VS Code, etc.)
50
50
* ** Pandas :** Version 1.2 and later are supported
51
51
* ** Polars :** Version 1.0 and later are supported
@@ -72,35 +72,35 @@ import tracdap.rt.api as trac
72
72
class QuickStartModel (trac .TracModel ):
73
73
74
74
def define_parameters (self ):
75
-
75
+
76
76
# Define any parameters the model will use
77
77
return trac.define_parameters(
78
78
trac.P(" exchange_rate" , trac.FLOAT , " EUR / GBP exchange rate" )
79
79
)
80
80
81
81
def define_inputs (self ):
82
-
82
+
83
83
# Define an input table with the columns and data types that the model needs
84
84
customer_loans = trac.define_input_table(
85
85
trac.F(" id" , trac.STRING , label = " Customer account ID" , business_key = True ),
86
86
trac.F(" region" , trac.STRING , label = " Customer home region" , categorical = True ),
87
- trac.F(" loan_amount" , trac.FLOAT , label = " Principal loan amount" ),
87
+ trac.F(" loan_amount" , trac.FLOAT , label = " Principal loan amount (EUR) " ),
88
88
label = " Customer loans data" )
89
89
90
90
return {" customer_loans" : customer_loans}
91
91
92
92
def define_outputs (self ):
93
-
93
+
94
94
# Define an output table with the columns and data types that the model will produce
95
95
loans_by_region = trac.define_output_table(
96
96
trac.F(" region" , trac.STRING , label = " Customer home region" , categorical = True ),
97
- trac.F(" loan_amount " , trac.FLOAT , label = " Principal loan amount " ),
97
+ trac.F(" total_lending " , trac.FLOAT , label = " Total lending (GBP) " ),
98
98
label = " Loans by region" )
99
99
100
100
return {" loans_by_region" : loans_by_region}
101
101
102
102
def run_model (self , ctx : trac.TracContext):
103
-
103
+
104
104
# Parameters and inputs are loaded and validated by TRAC
105
105
exchange_rate = ctx.get_parameter(" exchange_rate" )
106
106
customer_loans = ctx.get_pandas_table(" customer_loans" )
@@ -109,11 +109,11 @@ class QuickStartModel(trac.TracModel):
109
109
customer_loans[" loan_amount_gbp" ] = customer_loans[" loan_amount" ] * exchange_rate
110
110
111
111
loans_by_region = customer_loans \
112
- .groupby(" region" , as_index = False ) \
113
- .aggregate({ " loan_amount " : " sum" } )
112
+ .groupby(" region" , observed = True , as_index = False ) \
113
+ .aggregate(total_lending = ( " loan_amount_gbp " , " sum" ) )
114
114
115
115
# Logs written to ctx.log are captured by the platform
116
- ctx.log().info(" Aggregated loans for {} regions" , len (loans_by_region))
116
+ ctx.log().info(" Aggregated loans for %d regions" , len (loans_by_region))
117
117
118
118
# Outputs are handed back to TRAC for validation and saving
119
119
ctx.put_pandas_table(" loans_by_region" , loans_by_region)
0 commit comments