aimodelshare 0.3.7__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (171) hide show
  1. aimodelshare/README.md +26 -0
  2. aimodelshare/__init__.py +100 -0
  3. aimodelshare/aimsonnx.py +2381 -0
  4. aimodelshare/api.py +836 -0
  5. aimodelshare/auth.py +163 -0
  6. aimodelshare/aws.py +511 -0
  7. aimodelshare/aws_client.py +173 -0
  8. aimodelshare/base_image.py +154 -0
  9. aimodelshare/bucketpolicy.py +106 -0
  10. aimodelshare/color_mappings/color_mapping_keras.csv +121 -0
  11. aimodelshare/color_mappings/color_mapping_pytorch.csv +117 -0
  12. aimodelshare/containerisation.py +244 -0
  13. aimodelshare/containerization.py +712 -0
  14. aimodelshare/containerization_templates/Dockerfile.txt +8 -0
  15. aimodelshare/containerization_templates/Dockerfile_PySpark.txt +23 -0
  16. aimodelshare/containerization_templates/buildspec.txt +14 -0
  17. aimodelshare/containerization_templates/lambda_function.txt +40 -0
  18. aimodelshare/custom_approach/__init__.py +1 -0
  19. aimodelshare/custom_approach/lambda_function.py +17 -0
  20. aimodelshare/custom_eval_metrics.py +103 -0
  21. aimodelshare/data_sharing/__init__.py +0 -0
  22. aimodelshare/data_sharing/data_sharing_templates/Dockerfile.txt +3 -0
  23. aimodelshare/data_sharing/data_sharing_templates/__init__.py +1 -0
  24. aimodelshare/data_sharing/data_sharing_templates/buildspec.txt +15 -0
  25. aimodelshare/data_sharing/data_sharing_templates/codebuild_policies.txt +129 -0
  26. aimodelshare/data_sharing/data_sharing_templates/codebuild_trust_relationship.txt +12 -0
  27. aimodelshare/data_sharing/download_data.py +620 -0
  28. aimodelshare/data_sharing/share_data.py +373 -0
  29. aimodelshare/data_sharing/utils.py +8 -0
  30. aimodelshare/deploy_custom_lambda.py +246 -0
  31. aimodelshare/documentation/Makefile +20 -0
  32. aimodelshare/documentation/karma_sphinx_theme/__init__.py +28 -0
  33. aimodelshare/documentation/karma_sphinx_theme/_version.py +2 -0
  34. aimodelshare/documentation/karma_sphinx_theme/breadcrumbs.html +70 -0
  35. aimodelshare/documentation/karma_sphinx_theme/layout.html +172 -0
  36. aimodelshare/documentation/karma_sphinx_theme/search.html +50 -0
  37. aimodelshare/documentation/karma_sphinx_theme/searchbox.html +14 -0
  38. aimodelshare/documentation/karma_sphinx_theme/static/css/custom.css +2 -0
  39. aimodelshare/documentation/karma_sphinx_theme/static/css/custom.css.map +1 -0
  40. aimodelshare/documentation/karma_sphinx_theme/static/css/theme.css +2751 -0
  41. aimodelshare/documentation/karma_sphinx_theme/static/css/theme.css.map +1 -0
  42. aimodelshare/documentation/karma_sphinx_theme/static/css/theme.min.css +2 -0
  43. aimodelshare/documentation/karma_sphinx_theme/static/css/theme.min.css.map +1 -0
  44. aimodelshare/documentation/karma_sphinx_theme/static/font/fontello.eot +0 -0
  45. aimodelshare/documentation/karma_sphinx_theme/static/font/fontello.svg +32 -0
  46. aimodelshare/documentation/karma_sphinx_theme/static/font/fontello.ttf +0 -0
  47. aimodelshare/documentation/karma_sphinx_theme/static/font/fontello.woff +0 -0
  48. aimodelshare/documentation/karma_sphinx_theme/static/font/fontello.woff2 +0 -0
  49. aimodelshare/documentation/karma_sphinx_theme/static/js/theme.js +68 -0
  50. aimodelshare/documentation/karma_sphinx_theme/theme.conf +9 -0
  51. aimodelshare/documentation/make.bat +35 -0
  52. aimodelshare/documentation/requirements.txt +2 -0
  53. aimodelshare/documentation/source/about.rst +18 -0
  54. aimodelshare/documentation/source/advanced_features.rst +137 -0
  55. aimodelshare/documentation/source/competition.rst +218 -0
  56. aimodelshare/documentation/source/conf.py +58 -0
  57. aimodelshare/documentation/source/create_credentials.rst +86 -0
  58. aimodelshare/documentation/source/example_notebooks.rst +132 -0
  59. aimodelshare/documentation/source/functions.rst +151 -0
  60. aimodelshare/documentation/source/gettingstarted.rst +390 -0
  61. aimodelshare/documentation/source/images/creds1.png +0 -0
  62. aimodelshare/documentation/source/images/creds2.png +0 -0
  63. aimodelshare/documentation/source/images/creds3.png +0 -0
  64. aimodelshare/documentation/source/images/creds4.png +0 -0
  65. aimodelshare/documentation/source/images/creds5.png +0 -0
  66. aimodelshare/documentation/source/images/creds_file_example.png +0 -0
  67. aimodelshare/documentation/source/images/predict_tab.png +0 -0
  68. aimodelshare/documentation/source/index.rst +110 -0
  69. aimodelshare/documentation/source/modelplayground.rst +132 -0
  70. aimodelshare/exceptions.py +11 -0
  71. aimodelshare/generatemodelapi.py +1270 -0
  72. aimodelshare/iam/codebuild_policy.txt +129 -0
  73. aimodelshare/iam/codebuild_trust_relationship.txt +12 -0
  74. aimodelshare/iam/lambda_policy.txt +15 -0
  75. aimodelshare/iam/lambda_trust_relationship.txt +12 -0
  76. aimodelshare/json_templates/__init__.py +1 -0
  77. aimodelshare/json_templates/api_json.txt +155 -0
  78. aimodelshare/json_templates/auth/policy.txt +1 -0
  79. aimodelshare/json_templates/auth/role.txt +1 -0
  80. aimodelshare/json_templates/eval/policy.txt +1 -0
  81. aimodelshare/json_templates/eval/role.txt +1 -0
  82. aimodelshare/json_templates/function/policy.txt +1 -0
  83. aimodelshare/json_templates/function/role.txt +1 -0
  84. aimodelshare/json_templates/integration_response.txt +5 -0
  85. aimodelshare/json_templates/lambda_policy_1.txt +15 -0
  86. aimodelshare/json_templates/lambda_policy_2.txt +8 -0
  87. aimodelshare/json_templates/lambda_role_1.txt +12 -0
  88. aimodelshare/json_templates/lambda_role_2.txt +16 -0
  89. aimodelshare/leaderboard.py +174 -0
  90. aimodelshare/main/1.txt +132 -0
  91. aimodelshare/main/1B.txt +112 -0
  92. aimodelshare/main/2.txt +153 -0
  93. aimodelshare/main/3.txt +134 -0
  94. aimodelshare/main/4.txt +128 -0
  95. aimodelshare/main/5.txt +109 -0
  96. aimodelshare/main/6.txt +105 -0
  97. aimodelshare/main/7.txt +144 -0
  98. aimodelshare/main/8.txt +142 -0
  99. aimodelshare/main/__init__.py +1 -0
  100. aimodelshare/main/authorization.txt +275 -0
  101. aimodelshare/main/eval_classification.txt +79 -0
  102. aimodelshare/main/eval_lambda.txt +1709 -0
  103. aimodelshare/main/eval_regression.txt +80 -0
  104. aimodelshare/main/lambda_function.txt +8 -0
  105. aimodelshare/main/nst.txt +149 -0
  106. aimodelshare/model.py +1543 -0
  107. aimodelshare/modeluser.py +215 -0
  108. aimodelshare/moral_compass/README.md +408 -0
  109. aimodelshare/moral_compass/__init__.py +65 -0
  110. aimodelshare/moral_compass/_version.py +3 -0
  111. aimodelshare/moral_compass/api_client.py +601 -0
  112. aimodelshare/moral_compass/apps/__init__.py +69 -0
  113. aimodelshare/moral_compass/apps/ai_consequences.py +540 -0
  114. aimodelshare/moral_compass/apps/bias_detective.py +714 -0
  115. aimodelshare/moral_compass/apps/ethical_revelation.py +898 -0
  116. aimodelshare/moral_compass/apps/fairness_fixer.py +889 -0
  117. aimodelshare/moral_compass/apps/judge.py +888 -0
  118. aimodelshare/moral_compass/apps/justice_equity_upgrade.py +853 -0
  119. aimodelshare/moral_compass/apps/mc_integration_helpers.py +820 -0
  120. aimodelshare/moral_compass/apps/model_building_game.py +1104 -0
  121. aimodelshare/moral_compass/apps/model_building_game_beginner.py +687 -0
  122. aimodelshare/moral_compass/apps/moral_compass_challenge.py +858 -0
  123. aimodelshare/moral_compass/apps/session_auth.py +254 -0
  124. aimodelshare/moral_compass/apps/shared_activity_styles.css +349 -0
  125. aimodelshare/moral_compass/apps/tutorial.py +481 -0
  126. aimodelshare/moral_compass/apps/what_is_ai.py +853 -0
  127. aimodelshare/moral_compass/challenge.py +365 -0
  128. aimodelshare/moral_compass/config.py +187 -0
  129. aimodelshare/placeholders/model.onnx +0 -0
  130. aimodelshare/placeholders/preprocessor.zip +0 -0
  131. aimodelshare/playground.py +1968 -0
  132. aimodelshare/postprocessormodules.py +157 -0
  133. aimodelshare/preprocessormodules.py +373 -0
  134. aimodelshare/pyspark/1.txt +195 -0
  135. aimodelshare/pyspark/1B.txt +181 -0
  136. aimodelshare/pyspark/2.txt +220 -0
  137. aimodelshare/pyspark/3.txt +204 -0
  138. aimodelshare/pyspark/4.txt +187 -0
  139. aimodelshare/pyspark/5.txt +178 -0
  140. aimodelshare/pyspark/6.txt +174 -0
  141. aimodelshare/pyspark/7.txt +211 -0
  142. aimodelshare/pyspark/8.txt +206 -0
  143. aimodelshare/pyspark/__init__.py +1 -0
  144. aimodelshare/pyspark/authorization.txt +258 -0
  145. aimodelshare/pyspark/eval_classification.txt +79 -0
  146. aimodelshare/pyspark/eval_lambda.txt +1441 -0
  147. aimodelshare/pyspark/eval_regression.txt +80 -0
  148. aimodelshare/pyspark/lambda_function.txt +8 -0
  149. aimodelshare/pyspark/nst.txt +213 -0
  150. aimodelshare/python/my_preprocessor.py +58 -0
  151. aimodelshare/readme.md +26 -0
  152. aimodelshare/reproducibility.py +181 -0
  153. aimodelshare/sam/Dockerfile.txt +8 -0
  154. aimodelshare/sam/Dockerfile_PySpark.txt +24 -0
  155. aimodelshare/sam/__init__.py +1 -0
  156. aimodelshare/sam/buildspec.txt +11 -0
  157. aimodelshare/sam/codebuild_policies.txt +129 -0
  158. aimodelshare/sam/codebuild_trust_relationship.txt +12 -0
  159. aimodelshare/sam/codepipeline_policies.txt +173 -0
  160. aimodelshare/sam/codepipeline_trust_relationship.txt +12 -0
  161. aimodelshare/sam/spark-class.txt +2 -0
  162. aimodelshare/sam/template.txt +54 -0
  163. aimodelshare/tools.py +103 -0
  164. aimodelshare/utils/__init__.py +78 -0
  165. aimodelshare/utils/optional_deps.py +38 -0
  166. aimodelshare/utils.py +57 -0
  167. aimodelshare-0.3.7.dist-info/METADATA +298 -0
  168. aimodelshare-0.3.7.dist-info/RECORD +171 -0
  169. aimodelshare-0.3.7.dist-info/WHEEL +5 -0
  170. aimodelshare-0.3.7.dist-info/licenses/LICENSE +5 -0
  171. aimodelshare-0.3.7.dist-info/top_level.txt +1 -0
@@ -0,0 +1,1709 @@
1
+ import boto3
2
+ import pandas as pd
3
+ import os
4
+ import json
5
+ import pickle
6
+ import six
7
+ import onnx
8
+ import json
9
+ import argparse
10
+ import logging
11
+ from botocore.exceptions import ClientError
12
+ import requests
13
+ import jwt
14
+ import sys
15
+
16
+ logger = logging.getLogger(__name__)
17
+
18
+
19
+ # from aimslambda import analyze_ytest, evaluate_model, inspect_model, compare_models
20
+ # from s3connect import get_ytestdata, get_onnx_mem, get_onnx_temp
21
+
22
+
23
+ ####################################################################
24
+ ##################### Helper Functions #############################
25
+
26
+ def _ensure_df(obj):
27
+ """
28
+ Safely convert various input types to pandas DataFrame.
29
+ Handles dict, list, and DataFrame inputs with robust error handling.
30
+
31
+ For dict inputs, always treats them as a single row to maintain consistent
32
+ behavior and avoid unexpected flattening of nested structures.
33
+
34
+ Args:
35
+ obj: Input object (DataFrame, dict, list, or other)
36
+
37
+ Returns:
38
+ pandas.DataFrame
39
+ """
40
+ import pandas as pd
41
+
42
+ if isinstance(obj, pd.DataFrame):
43
+ return obj
44
+
45
+ if isinstance(obj, dict):
46
+ # Always treat dict as single row to maintain consistent structure
47
+ return pd.DataFrame([obj])
48
+
49
+ if isinstance(obj, list):
50
+ if len(obj) == 0:
51
+ return pd.DataFrame()
52
+ # If all elements are dicts, create DataFrame from list of dicts
53
+ if all(isinstance(x, dict) for x in obj):
54
+ return pd.DataFrame(obj)
55
+ # Otherwise, create single-column DataFrame
56
+ return pd.DataFrame({'value': obj})
57
+
58
+ # For any other type, return empty DataFrame
59
+ return pd.DataFrame()
60
+
61
+
62
+ ####################################################################
63
+ ########################### main handler ###########################
64
+
65
+ def handler(event, context):
66
+
67
+ body = event["body"]
68
+ if isinstance(body, six.string_types):
69
+ body = json.loads(body)
70
+
71
+ for key, value in body.items():
72
+ if value == "None":
73
+ body[key]=None
74
+
75
+
76
+ if body.get("return_task_type","ALL") == "True" or body.get("return_task_type", "ALL") == "TRUE":
77
+ task_type="$task_type"
78
+ task_type_dict = {"statusCode": 200,
79
+ "headers": {
80
+ "Access-Control-Allow-Origin" : "*",
81
+ "Access-Control-Allow-Credentials": True,
82
+ "Allow" : "GET, OPTIONS, POST",
83
+ "Access-Control-Allow-Methods" : "GET, OPTIONS, POST",
84
+ "Access-Control-Allow-Headers" : "*"},
85
+ "body": json.dumps({"task_type":task_type})
86
+ }
87
+ return task_type_dict
88
+
89
+ if body.get("exampledata", "ALL") == "True" or body.get("exampledata", "ALL") == "TRUE":
90
+
91
+ exampledata=get_exampledata(example_data_filename = "exampledata.json")
92
+
93
+ exdata_dict = {"statusCode": 200,
94
+ "headers": {
95
+ "Access-Control-Allow-Origin" : "*",
96
+ "Access-Control-Allow-Credentials": True,
97
+ "Allow" : "GET, OPTIONS, POST",
98
+ "Access-Control-Allow-Methods" : "GET, OPTIONS, POST",
99
+ "Access-Control-Allow-Headers" : "*"},
100
+ "body": json.dumps(exampledata)
101
+ }
102
+ return exdata_dict
103
+
104
+ idtoken=event['requestContext']['authorizer']['principalId']
105
+ decoded = jwt.decode(idtoken,options={"verify_signature": False,"verify_aud": False}) # works in PyJWT < v2.0
106
+ email=decoded['email']
107
+ print(email)
108
+ submission_type = body.get("submission_type", None)
109
+ authorized_competitionusers={"emaillist":"data"}
110
+ try:
111
+ authorized_competitionusers=get_authorizedcompetitionuserdata(example_data_filename = submission_type+"/competitionuserdata.json")
112
+ print(authorized_competitionusers)
113
+ except:
114
+ pass
115
+
116
+ authorized_emails=authorized_competitionusers.get("emaillist","no data")
117
+ print(authorized_emails)
118
+ public=authorized_competitionusers.get("public","FALSE")
119
+ print(public)
120
+ #TODO: check email agains s3 list and resolve public to true or false from same file
121
+ #Or check if emails on list and if so, check against them, if not assume public project.
122
+ if any([public=="FALSE",public=="TRUE",body.get("return_apikey","ALL")=="True" ]):
123
+ if body.get("return_apikey","ALL") == "True":
124
+ apikey="$apikey"
125
+ api_dict = {"statusCode": 200,
126
+ "headers": {
127
+ "Access-Control-Allow-Origin" : "*",
128
+ "Access-Control-Allow-Credentials": True,
129
+ "Allow" : "GET, OPTIONS, POST",
130
+ "Access-Control-Allow-Methods" : "GET, OPTIONS, POST",
131
+ "Access-Control-Allow-Headers" : "*"},
132
+ "body": json.dumps({"apikey":apikey})
133
+ }
134
+ return api_dict
135
+ else:
136
+ pass
137
+
138
+ if any([email in authorized_emails,public=="TRUE"]):
139
+ if body.get("return_apikey","ALL") == "True":
140
+ apikey="$apikey"
141
+ api_dict = {"statusCode": 200,
142
+ "headers": {
143
+ "Access-Control-Allow-Origin" : "*",
144
+ "Access-Control-Allow-Credentials": True,
145
+ "Allow" : "GET, OPTIONS, POST",
146
+ "Access-Control-Allow-Methods" : "GET, OPTIONS, POST",
147
+ "Access-Control-Allow-Headers" : "*"},
148
+ "body": json.dumps({"apikey":apikey})
149
+ }
150
+ return api_dict
151
+
152
+ if body.get("return_eval","ALL") == "True":
153
+
154
+ submission_type = body.get("submission_type","competition")
155
+ print(submission_type)
156
+
157
+ idempotentmodel_version=json.loads(event['requestContext']['authorizer']['uniquemodversion'])
158
+
159
+ ytestdata=get_ytestdata(ytest_s3_filename=submission_type+"/"+"ytest.pkl")
160
+
161
+ eval_result = evaluate_model(body, ytestdata, submission_type=submission_type)
162
+
163
+ bucket="$bucket_name"
164
+ model_id="$unique_model_id"
165
+
166
+ s3_client=boto3.client("s3")
167
+ model_files, err = _get_file_list(s3_client, bucket, model_id+"/"+submission_type+"/model_eval_data_mastertable")
168
+
169
+ print(model_files)
170
+ if model_files==None:
171
+ model_files, err = _get_file_list(s3_client, bucket, model_id+"/"+submission_type)
172
+
173
+
174
+ mversions=[]
175
+ musers=[]
176
+ mtimestamp=[]
177
+ print(idempotentmodel_version)
178
+ for i in idempotentmodel_version:
179
+ idemresult=i.split("||||")
180
+ mversions.append(idemresult[0])
181
+ musers.append(idemresult[1])
182
+ mtimestamp.append(idemresult[1])
183
+ print(mversions)
184
+ mversions=[int(i) for i in mversions]
185
+
186
+
187
+ newleaderboarddata=[]
188
+ for i in model_files:
189
+ if i.find("mastertable_v")>0:
190
+ newleaderboarddata.append(i)
191
+
192
+ try:
193
+ leaderboard = get_leaderboard("classification", submission_type=submission_type) #task_type variable used here
194
+ currentversions=leaderboard['version']
195
+ except:
196
+ currentversions=[]
197
+ print("current versions:")
198
+ print(list(currentversions))
199
+ allversions = [sub.split('_v')[1].split('.')[0] for sub in newleaderboarddata]
200
+ print("Named versions in csv files:")
201
+ allversions=[int(i) for i in allversions]
202
+ missingincurrent_leaderboard=list(set(allversions)-set(currentversions))
203
+
204
+ missingingmodelversions=list(set(mversions)-set(currentversions))
205
+
206
+ print("missing model versions for idemp check")
207
+ print(missingingmodelversions)
208
+
209
+ if len(missingingmodelversions)>1:
210
+ idempotentmodel_version=min(missingingmodelversions)
211
+ elif len(missingingmodelversions)==1:
212
+ idempotentmodel_version=missingingmodelversions[0]
213
+ else:
214
+ idempotentmodel_version=0
215
+
216
+
217
+ finalfiles=[]
218
+
219
+ if "model_eval_data_mastertable.csv" not in model_files:
220
+ finalfiles.append("model_eval_data_mastertable.csv")
221
+ finalfiles.append("model_eval_data_mastertable_private.csv")
222
+ finalfiles.append("onnx_model_mostrecent.onnx")
223
+ finalfiles.append("onnx_model_v1.onnx")
224
+ finalfiles.append("predictionmodel_1.onnx")
225
+ finalfiles.append("preprocessor_v1.zip")
226
+ finalfiles.append("reproducibility_v1.json")
227
+ finalfiles.append("model_metadata_v1.json")
228
+ else:
229
+ finalfiles.append("model_eval_data_mastertable_v"+str(idempotentmodel_version)+".csv")
230
+ finalfiles.append("model_eval_data_mastertable_private_v"+str(idempotentmodel_version)+".csv")
231
+ finalfiles.append("onnx_model_mostrecent.onnx")
232
+ finalfiles.append("onnx_model_v"+str(idempotentmodel_version)+".onnx")
233
+ finalfiles.append("preprocessor_v"+str(idempotentmodel_version)+".zip")
234
+ finalfiles.append("reproducibility_v"+str(idempotentmodel_version)+".json")
235
+ finalfiles.append("model_metadata_v"+str(idempotentmodel_version)+".json")
236
+
237
+ finalfiles.append("inspect_pd_"+str(idempotentmodel_version)+".json")
238
+ finalfiles.append("model_graph_"+str(idempotentmodel_version)+".json")
239
+ print("finalfiles:"+str(finalfiles))
240
+
241
+ #TODO: Change method params with string template params in eval_lambda / also need to change bucket and prefix for file lists
242
+ expires_in = 6000
243
+
244
+ getdict={}
245
+ putdict={}
246
+
247
+ print("idempotentmodel_version: "+str(idempotentmodel_version))
248
+ finalfilesget=finalfiles
249
+ finalfilespost=finalfiles
250
+
251
+ for i in finalfilespost:
252
+ putresult= create_presigned_post(bucket, model_id+"/"+submission_type+"/"+i, expiration=expires_in)
253
+ putdict.update({str(i):str(putresult)})
254
+
255
+ print(finalfilespost)
256
+ for i in finalfilesget:
257
+ if i.find("mastertable_v")>0:
258
+ indexvalue=finalfilesget.index(i)
259
+ finalfilesget[indexvalue]="model_eval_data_mastertable.csv"
260
+ if i.find("mastertable_private_v")>0:
261
+ indexvalue=finalfilesget.index(i)
262
+ finalfilesget[indexvalue]="model_eval_data_mastertable_private.csv"
263
+
264
+ print(finalfilesget)
265
+
266
+ for i in finalfilesget:
267
+ method_parameters = {'Bucket': bucket, 'Key': model_id+"/"+submission_type+"/"+i} #repeat for all necessary keys and return dict with nec. end user artifacts.
268
+ getresult= generate_presigned_url(s3_client, 'get_object', method_parameters, expires_in)
269
+ getdict.update({str(i):str(getresult)})
270
+
271
+ print(finalfilesget)
272
+
273
+
274
+ eval_dict = {"statusCode": 200,
275
+ "headers": {
276
+ "Access-Control-Allow-Origin" : "*",
277
+ "Access-Control-Allow-Credentials": True,
278
+ "Allow" : "GET, OPTIONS, POST",
279
+ "Access-Control-Allow-Methods" : "GET, OPTIONS, POST",
280
+ "Access-Control-Allow-Headers" : "*"},
281
+ "body": json.dumps({"eval":eval_result,"get":getdict,"put": putdict,"idempotentmodel_version":idempotentmodel_version})
282
+ }
283
+ return eval_dict
284
+
285
+
286
+
287
+ if body.get("return_y","ALL") == "True":
288
+
289
+ submission_type = body.get("submission_type")
290
+
291
+ ytestdata=get_ytestdata(ytest_s3_filename=submission_type+"/ytest.pkl")
292
+ y_stats = analyze_ytest(ytestdata)
293
+
294
+ ytest_dict = {"statusCode": 200,
295
+ "headers": {
296
+ "Access-Control-Allow-Origin" : "*",
297
+ "Access-Control-Allow-Credentials": True,
298
+ "Allow" : "GET, OPTIONS, POST",
299
+ "Access-Control-Allow-Methods" : "GET, OPTIONS, POST",
300
+ "Access-Control-Allow-Headers" : "*"},
301
+ "body": json.dumps(y_stats)
302
+ }
303
+ return ytest_dict
304
+
305
+ if body.get("inspect_model","ALL") == "True":
306
+
307
+ version = body["version"]
308
+ submission_type = body.get("submission_type", None)
309
+
310
+ inspect_pd = inspect_model(version, submission_type=submission_type)
311
+
312
+ inspect_dict = {"statusCode": 200,
313
+ "headers": {
314
+ "Access-Control-Allow-Origin" : "*",
315
+ "Access-Control-Allow-Credentials": True,
316
+ "Allow" : "GET, OPTIONS, POST",
317
+ "Access-Control-Allow-Methods" : "GET, OPTIONS, POST",
318
+ "Access-Control-Allow-Headers" : "*"},
319
+ "body": json.dumps(inspect_pd.to_dict())
320
+ }
321
+ return inspect_dict
322
+
323
+
324
+ if body.get("compare_models","ALL") == "True":
325
+
326
+ version_list = body["version_list"]
327
+ verbose = body.get("verbose", 1)
328
+ naming_convention = body.get("naming_convention", None)
329
+ submission_type = body.get("submission_type", None)
330
+
331
+ comp_dict_out = compare_models(version_list, verbose=verbose, naming_convention=naming_convention, submission_type=submission_type)
332
+
333
+ compare_dict = {"statusCode": 200,
334
+ "headers": {
335
+ "Access-Control-Allow-Origin" : "*",
336
+ "Access-Control-Allow-Credentials": True,
337
+ "Allow" : "GET, OPTIONS, POST",
338
+ "Access-Control-Allow-Methods" : "GET, OPTIONS, POST",
339
+ "Access-Control-Allow-Headers" : "*"},
340
+ "body": json.dumps(comp_dict_out)
341
+ }
342
+ return compare_dict
343
+
344
+
345
+ if body.get("get_leaderboard","ALL") == "True":
346
+
347
+ submission_type = body.get("submission_type","ALL")
348
+ print("submission_type ", submission_type)
349
+
350
+ verbose=body["verbose"]
351
+ columns=body["columns"]
352
+
353
+ if body.get("private", 'FALSE') == "TRUE":
354
+ private = True
355
+ else:
356
+ private = False
357
+
358
+ leaderboard = get_leaderboard("$task_type", verbose, columns, private, submission_type)
359
+
360
+ leaderboard_dict = {"statusCode": 200,
361
+ "headers": {
362
+ "Access-Control-Allow-Origin" : "*",
363
+ "Access-Control-Allow-Credentials": True,
364
+ "Allow" : "GET, OPTIONS, POST",
365
+ "Access-Control-Allow-Methods" : "GET, OPTIONS, POST",
366
+ "Access-Control-Allow-Headers" : "*"},
367
+ "body": json.dumps(leaderboard.to_dict())
368
+ }
369
+ return leaderboard_dict
370
+
371
+ if body.get("reproduce") == "True" and body.get("instantiate_model") == "False":
372
+ version = body["model_version"]
373
+ submission_type = body.get("submission_type")
374
+ reproducibility_env_json = None
375
+ reproducibility_env_json = get_reproducibility_env(version=version,submission_type = submission_type)
376
+
377
+ data = {
378
+ "reproducibility_env": reproducibility_env_json
379
+ }
380
+
381
+ model_dict = {
382
+ "statusCode": 200,
383
+ "headers": {
384
+ "Access-Control-Allow-Origin" : "*",
385
+ "Access-Control-Allow-Credentials": True,
386
+ "Allow" : "GET, OPTIONS, POST",
387
+ "Access-Control-Allow-Methods" : "GET, OPTIONS, POST",
388
+ "Access-Control-Allow-Headers" : "*"
389
+ },
390
+ "body": json.dumps(data)
391
+ }
392
+ return model_dict
393
+
394
+ if body.get("instantiate_model","ALL") == "True":
395
+ version = body["model_version"]
396
+ reproduce = body["reproduce"]
397
+ trained = body["trained"]
398
+ submission_type = body.get("submission_type")
399
+
400
+ reproducibility_env_json = None
401
+ model_weight_url = None
402
+ model_metadata_json = get_model_metadata(version, submission_type)
403
+
404
+ # the model version is found (users didn't only submit prediction for this version)
405
+ if model_metadata_json:
406
+ if reproduce:
407
+ reproducibility_env_json = get_reproducibility_env(version=version,submission_type = submission_type)
408
+ elif trained:
409
+ # Get the presigned url.
410
+ s3_client=boto3.client("s3")
411
+
412
+ bucket = "$bucket_name"
413
+ model_id = "$unique_model_id"
414
+ onnx_model_name = "onnx_model_v{}.onnx".format(version)
415
+
416
+ method_parameters = {
417
+ "Bucket": bucket,
418
+ "Key": model_id + "/" + submission_type + "/" + onnx_model_name,
419
+ }
420
+
421
+ expires_in = 900 # 15 mins
422
+
423
+ presigned_url = generate_presigned_url(s3_client, "get_object", method_parameters, expires_in)
424
+ model_weight_url = str(presigned_url)
425
+ print("Presigned url: {}".format(str(presigned_url)))
426
+
427
+ data = {
428
+ "model_weight_url": model_weight_url,
429
+ "model_metadata": model_metadata_json,
430
+ "reproducibility_env": reproducibility_env_json
431
+ }
432
+ model_dict = {
433
+ "statusCode": 200,
434
+ "headers": {
435
+ "Access-Control-Allow-Origin" : "*",
436
+ "Access-Control-Allow-Credentials": True,
437
+ "Allow" : "GET, OPTIONS, POST",
438
+ "Access-Control-Allow-Methods" : "GET, OPTIONS, POST",
439
+ "Access-Control-Allow-Headers" : "*"
440
+ },
441
+ "body": json.dumps(data)
442
+ }
443
+ return model_dict
444
+
445
+ if body.get("leaderboard","ALL") == "TRUE":
446
+
447
+ submission_type = body.get("submission_type","ALL")
448
+
449
+
450
+ if body.get("private", 'FALSE') == "TRUE":
451
+ private = True
452
+ else:
453
+ private = False
454
+
455
+ leaderboard = get_leaderboard("$task_type", private=private, submission_type=submission_type)
456
+
457
+ leaderboard_dict = {"statusCode": 200,
458
+ "headers": {
459
+ "Access-Control-Allow-Origin" : "*",
460
+ "Access-Control-Allow-Credentials": True,
461
+ "Allow" : "GET, OPTIONS, POST",
462
+ "Access-Control-Allow-Methods" : "GET, OPTIONS, POST",
463
+ "Access-Control-Allow-Headers" : "*"},
464
+ "body": leaderboard.to_json(orient="table")
465
+ }
466
+ return leaderboard_dict
467
+ else:
468
+
469
+ if body.get("leaderboard","ALL") == "TRUE":
470
+
471
+ submission_type = body.get("submission_type","ALL")
472
+
473
+ if body.get("private", 'FALSE') == "TRUE":
474
+ private = True
475
+ else:
476
+ private = False
477
+
478
+ leaderboard = get_leaderboard("$task_type", private=private, submission_type=submission_type)
479
+
480
+ leaderboard_dict = {"statusCode": 200,
481
+ "headers": {
482
+ "Access-Control-Allow-Origin" : "*",
483
+ "Access-Control-Allow-Credentials": True,
484
+ "Allow" : "GET, OPTIONS, POST",
485
+ "Access-Control-Allow-Methods" : "GET, OPTIONS, POST",
486
+ "Access-Control-Allow-Headers" : "*"},
487
+ "body": leaderboard.to_json(orient="table")
488
+ }
489
+ return leaderboard_dict
490
+
491
+ if body.get("compare_models","ALL") == "True":
492
+
493
+ version_list = body["version_list"]
494
+ verbose = body.get("verbose", 1)
495
+ naming_convention = body.get("naming_convention", None)
496
+ submission_type = body.get("submission_type", None)
497
+
498
+ comp_dict_out = compare_models(version_list, verbose=verbose, naming_convention=naming_convention, submission_type=submission_type)
499
+
500
+ compare_dict = {"statusCode": 200,
501
+ "headers": {
502
+ "Access-Control-Allow-Origin" : "*",
503
+ "Access-Control-Allow-Credentials": True,
504
+ "Allow" : "GET, OPTIONS, POST",
505
+ "Access-Control-Allow-Methods" : "GET, OPTIONS, POST",
506
+ "Access-Control-Allow-Headers" : "*"},
507
+ "body": json.dumps(comp_dict_out)
508
+ }
509
+ return compare_dict
510
+
511
+ else:
512
+ unauthorized_user_dict = {"statusCode": 200,
513
+ "headers": {
514
+ "Access-Control-Allow-Origin" : "*",
515
+ "Access-Control-Allow-Credentials": True,
516
+ "Allow" : "GET, OPTIONS, POST",
517
+ "Access-Control-Allow-Methods" : "GET, OPTIONS, POST",
518
+ "Access-Control-Allow-Headers" : "*"},
519
+ "body": ["Unauthorized user: You do not have access to submit models to, or request data from, this competition."]
520
+ }
521
+ return unauthorized_user_dict
522
+
523
+ ####################################################################
524
+ ######################### aimsonnx lambda ##########################
525
+
526
+
527
+ import sklearn
528
+ from sklearn.metrics import accuracy_score
529
+ from sklearn.metrics import f1_score
530
+ from sklearn.metrics import precision_score
531
+ from sklearn.metrics import recall_score
532
+ from sklearn.metrics import roc_auc_score
533
+ from sklearn.metrics import mean_squared_error
534
+ from sklearn.metrics import r2_score
535
+ from sklearn.metrics import mean_absolute_error
536
+ from sklearn.model_selection import train_test_split
537
+ from collections import Counter
538
+ from math import sqrt
539
+ import json
540
+ import pandas as pd
541
+ import numpy as np
542
+ import ast
543
+ import six
544
+ import gc
545
+ import importlib
546
+ import botocore
547
+
548
+ #from s3connect import get_onnx_mem
549
+
550
+ def analyze_ytest(ytestdata, task_type="$task_type"):
551
+
552
+ if task_type=="classification":
553
+
554
+ class_labels = list(set(ytestdata))
555
+ class_balance = Counter(ytestdata)
556
+ label_dtypes = Counter([str(type(i)) for i in ytestdata])
557
+
558
+ y_stats = {"ytest_example": ytestdata[0:5],
559
+ "y_length": len(ytestdata),
560
+ "class_labels": class_labels,
561
+ "class_balance": class_balance,
562
+ "label_dtypes": label_dtypes}
563
+
564
+ else:
565
+ y_mean = np.mean(ytestdata)
566
+ y_min = np.min(ytestdata)
567
+ y_max = np.max(ytestdata)
568
+ y_sd = np.std(ytestdata)
569
+
570
+ y_stats = {"ytest_example": ytestdata[0:5],
571
+ "y_length": len(ytestdata),
572
+ "y_mean": y_mean,
573
+ "y_min": y_min,
574
+ "y_max": y_max,
575
+ "y_sd": y_sd}
576
+
577
+ return y_stats
578
+
579
+
580
+
581
+ def public_private_split(y_true, y_pred, task_type="$task_type", submission_type="competition"):
582
+
583
+ if task_type == "classification":
584
+ strat = y_true
585
+ elif task_type == "regression":
586
+ strat = None
587
+
588
+ private_size = get_public_private_split(submission_type=submission_type)
589
+
590
+ if private_size == 1:
591
+
592
+ eval_result_public = model_eval_metrics(y_true, y_pred, task_type=task_type, void=True, submission_type=submission_type)
593
+ eval_result_private = model_eval_metrics(y_true, y_pred, task_type=task_type, submission_type=submission_type)
594
+
595
+ elif private_size == 0:
596
+
597
+ eval_result_public = model_eval_metrics(y_true, y_pred, task_type=task_type, submission_type=submission_type)
598
+ eval_result_private = model_eval_metrics(y_true, y_pred, task_type=task_type, void=True, submission_type=submission_type)
599
+
600
+ else:
601
+
602
+ y_true_private, y_true_public, y_pred_private, y_pred_public = train_test_split(y_true, y_pred, test_size=1-private_size, shuffle=True, stratify=strat, random_state=1)
603
+
604
+ eval_result_public = model_eval_metrics(y_true_public, y_pred_public, task_type=task_type, submission_type=submission_type)
605
+ eval_result_private = model_eval_metrics(y_true_private, y_pred_private, task_type=task_type, submission_type=submission_type)
606
+
607
+ return eval_result_public, eval_result_private
608
+
609
+
610
+ def model_eval_metrics(y_true, y_pred, task_type="$task_type", void = False, submission_type="competition"):
611
+
612
+ if void == True:
613
+
614
+ metricdata = {'accuracy': [None], 'f1_score': [None], 'precision': [None], 'recall': [None], 'mse': [None], 'rmse': [None], 'mae': [None], 'r2': [None]}
615
+
616
+ else:
617
+
618
+ if task_type=="classification":
619
+
620
+ try:
621
+ accuracy_eval = accuracy_score(y_true, y_pred)
622
+ except:
623
+ accuracy_eval = None
624
+
625
+ try:
626
+ f1_score_eval = f1_score(y_true, y_pred,average="macro",zero_division=0)
627
+ except:
628
+ f1_score_eval = None
629
+
630
+ try:
631
+ precision_eval = precision_score(y_true, y_pred,average="macro",zero_division=0)
632
+ except:
633
+ precision_eval = None
634
+
635
+ try:
636
+ recall_eval = recall_score(y_true, y_pred,average="macro",zero_division=0)
637
+ except:
638
+ recall_eval = None
639
+
640
+ mse_eval = None
641
+ rmse_eval = None
642
+ mae_eval = None
643
+ r2_eval = None
644
+
645
+ metricdata = {'accuracy': [accuracy_eval], 'f1_score': [f1_score_eval], 'precision': [precision_eval], 'recall': [recall_eval], 'mse': [mse_eval], 'rmse': [rmse_eval], 'mae': [mae_eval], 'r2': [r2_eval]}
646
+
647
+ else:
648
+
649
+ try:
650
+ mse_eval = mean_squared_error(y_true, y_pred)
651
+ except:
652
+ mse_eval = None
653
+
654
+ try:
655
+ rmse_eval = sqrt(mean_squared_error(y_true, y_pred))
656
+ except:
657
+ rmse_eval = None
658
+
659
+ try:
660
+ mae_eval = mean_absolute_error(y_true, y_pred)
661
+ except:
662
+ mae_eval = None
663
+
664
+ try:
665
+ r2_eval = r2_score(y_true, y_pred)
666
+ except:
667
+ r2_eval = None
668
+
669
+ accuracy_eval = None
670
+ f1_score_eval = None
671
+ precision_eval = None
672
+ recall_eval = None
673
+
674
+ metricdata = {'accuracy': [accuracy_eval], 'f1_score': [f1_score_eval], 'precision': [precision_eval], 'recall': [recall_eval], 'mse': [mse_eval], 'rmse': [rmse_eval], 'mae': [mae_eval], 'r2': [r2_eval]}
675
+
676
+
677
+
678
+ s3 = boto3.resource("s3")
679
+ bucket = s3.Bucket("$bucket_name")
680
+
681
+ metrics_files = bucket.objects.filter(Prefix= "$unique_model_id/"+submission_type+"/metrics_")
682
+
683
+
684
+ if metrics_files:
685
+
686
+ for i in metrics_files:
687
+
688
+ file = i.key.split('/')[-1]
689
+
690
+ eval_metric = get_eval_metric(eval_metric_s3_filename=file, submission_type=submission_type)
691
+ custom_eval = eval_metric(y_true, y_pred)
692
+
693
+ if isinstance(custom_eval, dict):
694
+
695
+ for i in custom_eval.keys():
696
+
697
+ if void == True:
698
+
699
+ metricdata[i] = [None]
700
+
701
+ else:
702
+
703
+ metricdata[i] = [custom_eval[i]]
704
+
705
+ else:
706
+
707
+ if void == True:
708
+
709
+ metricdata[file.replace("metrics_","").replace(".zip", "")] = [None]
710
+
711
+ else:
712
+
713
+ metricdata[file.replace("metrics_","").replace(".zip", "")] = [custom_eval]
714
+
715
+ finalmetricdata = pd.DataFrame.from_dict(metricdata)
716
+
717
+ return finalmetricdata.to_dict('records')[0]
718
+
719
+
720
+ def evaluate_model(body, ytestdata, submission_type="competition"):
721
+
722
+ if isinstance(body["y_pred"], six.string_types):
723
+ prediction_list = json.loads(body["y_pred"])
724
+ else:
725
+ prediction_list = body["y_pred"]
726
+
727
+ result=public_private_split(ytestdata, prediction_list, task_type="$task_type", submission_type=submission_type)
728
+
729
+ return result
730
+
731
+
732
+ def inspect_model(version, submission_type):
733
+
734
+ s3 = boto3.resource('s3')
735
+ obj = s3.Object("$bucket_name", "$unique_model_id/"+submission_type+"/inspect_pd_"+str(version)+".json")
736
+ data = obj.get()['Body'].read()
737
+ model_dict = json.loads(data)
738
+
739
+ ml_framework = model_dict.get(str(version))['ml_framework']
740
+ model_type = model_dict.get(str(version))['model_type']
741
+ inspect_pd = pd.DataFrame(model_dict.get(str(version))['model_dict'])
742
+
743
+ return inspect_pd
744
+
745
+
746
+
747
+ def _model_summary(meta_dict, from_onnx=False):
748
+ '''Creates model summary table from model metadata dict.'''
749
+
750
+ assert(isinstance(meta_dict, dict)), \
751
+ "Please pass valid metadata dict."
752
+
753
+ assert('model_architecture' in meta_dict.keys()), \
754
+ "Please make sure model architecture data is included."
755
+
756
+ if from_onnx == True:
757
+ architecture = meta_dict['metadata_onnx']["model_architecture"]
758
+ else:
759
+ architecture = meta_dict["model_architecture"]
760
+
761
+
762
+ model_summary = pd.DataFrame({'Layer':architecture['layers_sequence'],
763
+ #'Activation':architecture['activations_sequence'],
764
+ 'Shape':architecture['layers_shapes'],
765
+ 'Params':architecture['layers_n_params']})
766
+
767
+ return model_summary
768
+
769
+
770
+ def _get_metadata(onnx_model):
771
+ '''Fetches previously extracted model metadata from ONNX object
772
+ and returns model metadata dict.'''
773
+
774
+ # double check this
775
+ #assert(isinstance(onnx_model, onnx.onnx_ml_pb2.ModelProto)), \
776
+ #"Please pass a onnx model object."
777
+
778
+ try:
779
+ onnx_meta = onnx_model.metadata_props
780
+
781
+ onnx_meta_dict = {'model_metadata': ''}
782
+
783
+ for i in onnx_meta:
784
+ onnx_meta_dict[i.key] = i.value
785
+
786
+ onnx_meta_dict = ast.literal_eval(onnx_meta_dict['model_metadata'])
787
+
788
+ #if onnx_meta_dict['model_config'] != None and \
789
+ #onnx_meta_dict['ml_framework'] != 'pytorch':
790
+ # onnx_meta_dict['model_config'] = ast.literal_eval(onnx_meta_dict['model_config'])
791
+
792
+ if onnx_meta_dict['model_architecture'] != None:
793
+ onnx_meta_dict['model_architecture'] = ast.literal_eval(onnx_meta_dict['model_architecture'])
794
+
795
+ if onnx_meta_dict['metadata_onnx'] != None:
796
+ onnx_meta_dict['metadata_onnx'] = ast.literal_eval(onnx_meta_dict['metadata_onnx'])
797
+
798
+ # onnx_meta_dict['model_image'] = onnx_to_image(onnx_model) # didnt want to include image dependencies in lambda
799
+
800
+ except Exception as e:
801
+
802
+ print(e)
803
+
804
+ onnx_meta_dict = ast.literal_eval(onnx_meta_dict)
805
+
806
+ return onnx_meta_dict
807
+
808
+
809
+ def compare_models(version_list, by_model_type=None, best_model=None, verbose=1, naming_convention=None, submission_type='competition'):
810
+
811
+ ml_framework_list = []
812
+ model_type_list = []
813
+ model_dict_list = []
814
+ model_dict = {}
815
+
816
+ for i in version_list:
817
+
818
+ s3 = boto3.resource('s3')
819
+ obj = s3.Object("$bucket_name", "$unique_model_id/"+submission_type+"/inspect_pd_"+str(i)+".json")
820
+ data = obj.get()['Body'].read()
821
+ model_dict_temp = json.loads(data)
822
+
823
+ ml_framework_list.append(model_dict_temp[str(i)]['ml_framework'])
824
+ model_type_list.append(model_dict_temp[str(i)]['model_type'])
825
+ model_dict_list.append(model_dict_temp[str(i)]['model_dict'])
826
+
827
+ model_dict[str(i)] = model_dict_temp[str(i)]
828
+
829
+
830
+ comp_dict_out = {}
831
+ comp_pd_nn = pd.DataFrame()
832
+
833
+
834
+ for i, j in zip(version_list, ml_framework_list):
835
+
836
+ if j == "sklearn" or j == "pyspark":
837
+
838
+ temp_pd = pd.DataFrame(model_dict[str(i)]['model_dict'])
839
+ temp_pd.columns = ['param_name', 'default_value', "model_version_"+str(i)]
840
+
841
+ if model_dict[str(i)]['model_type'] in comp_dict_out.keys():
842
+
843
+ comp_pd = pd.read_json(comp_dict_out[model_dict[str(i)]['model_type']])
844
+ comp_pd = comp_pd.merge(temp_pd.drop('default_value', axis=1), on='param_name')
845
+
846
+ comp_dict_out[model_dict[str(i)]['model_type']] = comp_pd.to_json()
847
+
848
+ else:
849
+ comp_dict_out[model_dict[str(i)]['model_type']] = temp_pd.to_json()
850
+
851
+
852
+ elif j == "keras" or j == 'pytorch':
853
+
854
+ temp_pd_nn = pd.DataFrame(model_dict[str(i)]['model_dict'])
855
+
856
+ temp_pd_nn.iloc[:,2] = temp_pd_nn.iloc[:,2].astype(str)
857
+
858
+ if verbose == 0:
859
+ temp_pd_nn = temp_pd_nn[['Layer']]
860
+ elif verbose == 1:
861
+ temp_pd_nn = temp_pd_nn[['Layer', 'Shape', 'Params']]
862
+ elif verbose == 2:
863
+ temp_pd_nn = temp_pd_nn[['Name', 'Layer', 'Shape', 'Params', 'Connect']]
864
+ elif verbose == 3:
865
+ temp_pd_nn = temp_pd_nn[['Name', 'Layer', 'Shape', 'Params', 'Connect', 'Activation']]
866
+
867
+ if naming_convention == 'pytorch':
868
+ temp_pd_nn['Layer'] = rename_layers(temp_pd_nn['Layer'], direction="keras_to_torch", activation=False)
869
+
870
+ if naming_convention == 'keras':
871
+ temp_pd_nn['Layer'] = rename_layers(temp_pd_nn['Layer'], direction="torch_to_keras", activation=False)
872
+
873
+ temp_pd_nn = temp_pd_nn.add_prefix('Model_'+str(i)+'_')
874
+
875
+ comp_pd_nn = pd.concat([comp_pd_nn, temp_pd_nn], axis=1)
876
+
877
+ comp_dict_out["nn"] = comp_pd_nn.to_json()
878
+
879
+ elif j == "undefined":
880
+
881
+ comp_dict_out["undefined_"+str(i)] = pd.DataFrame({'param_name':[], 'default_value':[], 'model_version_'+str(i):[]}).to_json()
882
+
883
+ return comp_dict_out
884
+
885
+
886
+ def model_from_string(model_type):
887
+ models_modules_dict = {'ABCMeta': 'sklearn.naive_bayes',
888
+ 'ARDRegression': 'sklearn.linear_model',
889
+ 'AdaBoostClassifier': 'sklearn.ensemble',
890
+ 'AdaBoostRegressor': 'sklearn.ensemble',
891
+ 'BaggingClassifier': 'sklearn.ensemble',
892
+ 'BaggingRegressor': 'sklearn.ensemble',
893
+ 'BallTree': 'sklearn.neighbors',
894
+ 'BaseDecisionTree': 'sklearn.tree',
895
+ 'BaseEnsemble': 'sklearn.ensemble',
896
+ 'BaseEstimator': 'sklearn.naive_bayes',
897
+ 'BayesianGaussianMixture': 'sklearn.mixture',
898
+ 'BayesianRidge': 'sklearn.linear_model',
899
+ 'BernoulliNB': 'sklearn.naive_bayes',
900
+ 'BernoulliRBM': 'sklearn.neural_network',
901
+ 'CalibratedClassifierCV': 'sklearn.calibration',
902
+ 'CategoricalNB': 'sklearn.naive_bayes',
903
+ 'ClassifierMixin': 'sklearn.naive_bayes',
904
+ 'ComplementNB': 'sklearn.naive_bayes',
905
+ 'DecisionTreeClassifier': 'sklearn.tree',
906
+ 'DecisionTreeRegressor': 'sklearn.tree',
907
+ 'DistanceMetric': 'sklearn.neighbors',
908
+ 'ElasticNet': 'sklearn.linear_model',
909
+ 'ElasticNetCV': 'sklearn.linear_model',
910
+ 'ExtraTreeClassifier': 'sklearn.tree',
911
+ 'ExtraTreeRegressor': 'sklearn.tree',
912
+ 'ExtraTreesClassifier': 'sklearn.ensemble',
913
+ 'ExtraTreesRegressor': 'sklearn.ensemble',
914
+ 'GammaRegressor': 'sklearn.linear_model',
915
+ 'GaussianMixture': 'sklearn.mixture',
916
+ 'GaussianNB': 'sklearn.naive_bayes',
917
+ 'GaussianProcessClassifier': 'sklearn.gaussian_process',
918
+ 'GaussianProcessRegressor': 'sklearn.gaussian_process',
919
+ 'GradientBoostingClassifier': 'sklearn.ensemble',
920
+ 'GradientBoostingRegressor': 'sklearn.ensemble',
921
+ 'Hinge': 'sklearn.linear_model',
922
+ 'Huber': 'sklearn.linear_model',
923
+ 'HuberRegressor': 'sklearn.linear_model',
924
+ 'IsolationForest': 'sklearn.ensemble',
925
+ 'IsotonicRegression': 'sklearn.isotonic',
926
+ 'KDTree': 'sklearn.neighbors',
927
+ 'KNeighborsClassifier': 'sklearn.neighbors',
928
+ 'KNeighborsRegressor': 'sklearn.neighbors',
929
+ 'KNeighborsTransformer': 'sklearn.neighbors',
930
+ 'KernelDensity': 'sklearn.neighbors',
931
+ 'LabelBinarizer': 'sklearn.naive_bayes',
932
+ 'Lars': 'sklearn.linear_model',
933
+ 'LarsCV': 'sklearn.linear_model',
934
+ 'Lasso': 'sklearn.linear_model',
935
+ 'LassoCV': 'sklearn.linear_model',
936
+ 'LassoLars': 'sklearn.linear_model',
937
+ 'LassoLarsCV': 'sklearn.linear_model',
938
+ 'LassoLarsIC': 'sklearn.linear_model',
939
+ 'LinearDiscriminantAnalysis': 'sklearn.discriminant_analysis',
940
+ 'LinearRegression': 'sklearn.linear_model',
941
+ 'LinearSVC': 'sklearn.svm',
942
+ 'LinearSVR': 'sklearn.svm',
943
+ 'LocalOutlierFactor': 'sklearn.neighbors',
944
+ 'Log': 'sklearn.linear_model',
945
+ 'LogisticRegression': 'sklearn.linear_model',
946
+ 'LogisticRegressionCV': 'sklearn.linear_model',
947
+ 'MLPClassifier': 'sklearn.neural_network',
948
+ 'MLPRegressor': 'sklearn.neural_network',
949
+ 'MetaEstimatorMixin': 'sklearn.multiclass',
950
+ 'ModifiedHuber': 'sklearn.linear_model',
951
+ 'MultiOutputMixin': 'sklearn.multiclass',
952
+ 'MultiTaskElasticNet': 'sklearn.linear_model',
953
+ 'MultiTaskElasticNetCV': 'sklearn.linear_model',
954
+ 'MultiTaskLasso': 'sklearn.linear_model',
955
+ 'MultiTaskLassoCV': 'sklearn.linear_model',
956
+ 'MultinomialNB': 'sklearn.naive_bayes',
957
+ 'NearestCentroid': 'sklearn.neighbors',
958
+ 'NearestNeighbors': 'sklearn.neighbors',
959
+ 'NeighborhoodComponentsAnalysis': 'sklearn.neighbors',
960
+ 'NotFittedError': 'sklearn.multiclass',
961
+ 'NuSVC': 'sklearn.svm',
962
+ 'NuSVR': 'sklearn.svm',
963
+ 'OneClassSVM': 'sklearn.svm',
964
+ 'OneVsOneClassifier': 'sklearn.multiclass',
965
+ 'OneVsRestClassifier': 'sklearn.multiclass',
966
+ 'OrthogonalMatchingPursuit': 'sklearn.linear_model',
967
+ 'OrthogonalMatchingPursuitCV': 'sklearn.linear_model',
968
+ 'OutputCodeClassifier': 'sklearn.multiclass',
969
+ 'Parallel': 'sklearn.multiclass',
970
+ 'PassiveAggressiveClassifier': 'sklearn.linear_model',
971
+ 'PassiveAggressiveRegressor': 'sklearn.linear_model',
972
+ 'Perceptron': 'sklearn.linear_model',
973
+ 'PoissonRegressor': 'sklearn.linear_model',
974
+ 'QuadraticDiscriminantAnalysis': 'sklearn.discriminant_analysis',
975
+ 'RANSACRegressor': 'sklearn.linear_model',
976
+ 'RadiusNeighborsClassifier': 'sklearn.neighbors',
977
+ 'RadiusNeighborsRegressor': 'sklearn.neighbors',
978
+ 'RadiusNeighborsTransformer': 'sklearn.neighbors',
979
+ 'RandomForestClassifier': 'sklearn.ensemble',
980
+ 'RandomForestRegressor': 'sklearn.ensemble',
981
+ 'RandomTreesEmbedding': 'sklearn.ensemble',
982
+ 'RegressorMixin': 'sklearn.isotonic',
983
+ 'Ridge': 'sklearn.linear_model',
984
+ 'RidgeCV': 'sklearn.linear_model',
985
+ 'RidgeClassifier': 'sklearn.linear_model',
986
+ 'RidgeClassifierCV': 'sklearn.linear_model',
987
+ 'SGDClassifier': 'sklearn.linear_model',
988
+ 'SGDRegressor': 'sklearn.linear_model',
989
+ 'SVC': 'sklearn.svm',
990
+ 'SVR': 'sklearn.svm',
991
+ 'SquaredLoss': 'sklearn.linear_model',
992
+ 'StackingClassifier': 'sklearn.ensemble',
993
+ 'StackingRegressor': 'sklearn.ensemble',
994
+ 'TheilSenRegressor': 'sklearn.linear_model',
995
+ 'TransformerMixin': 'sklearn.isotonic',
996
+ 'TweedieRegressor': 'sklearn.linear_model',
997
+ 'VotingClassifier': 'sklearn.ensemble',
998
+ 'VotingRegressor': 'sklearn.ensemble'}
999
+
1000
+ module = models_modules_dict[model_type]
1001
+ model_class = getattr(importlib.import_module(module), model_type)
1002
+ return model_class
1003
+
1004
+
1005
+ def get_leaderboard(task_type="classification", verbose=3, columns=None, private=False, submission_type='competition'):
1006
+
1007
+ if private==True:
1008
+ mastertable_path = submission_type+"/"+'model_eval_data_mastertable_private'
1009
+ else:
1010
+ mastertable_path = submission_type+"/"+'model_eval_data_mastertable'
1011
+
1012
+ bucket="$bucket_name"
1013
+ model_id="$unique_model_id"
1014
+
1015
+ s3_client=boto3.client("s3")
1016
+ model_files, err = _get_file_list(s3_client, bucket, model_id+"/"+submission_type)
1017
+ print(model_files)
1018
+
1019
+ newleaderboarddata=[]
1020
+ for i in model_files:
1021
+ if private and i.find("mastertable_private_v")>0:
1022
+ newleaderboarddata.append(i)
1023
+ if (not private) and i.find("mastertable_v")>0:
1024
+ newleaderboarddata.append(i)
1025
+
1026
+
1027
+ s3 = boto3.resource("s3")
1028
+ bucketres = s3.Bucket("$bucket_name")
1029
+
1030
+ # Defensive read of master leaderboard file
1031
+ try:
1032
+ with open("/tmp/"+mastertable_path.split("/")[-1]+".csv", "wb") as lbfo:
1033
+ bucketres.download_fileobj("$unique_model_id/"+mastertable_path+".csv", lbfo)
1034
+ leaderboard = pd.read_csv("/tmp/"+mastertable_path.split("/")[-1]+".csv", sep="\t")
1035
+ except Exception as e:
1036
+ print(f"Warning: Could not read master leaderboard file: {e}")
1037
+ leaderboard = pd.DataFrame()
1038
+
1039
+ # Ensure leaderboard is a DataFrame
1040
+ leaderboard = _ensure_df(leaderboard)
1041
+
1042
+ # Get current versions if available
1043
+ try:
1044
+ currentversions=leaderboard['version']
1045
+ except KeyError:
1046
+ currentversions=[]
1047
+
1048
+ print("current versions:")
1049
+ print(list(currentversions))
1050
+ allversions = [sub.split('_v')[1].split('.')[0] for sub in newleaderboarddata]
1051
+ print("Named versions in csv files:")
1052
+ allversions=[int(i) for i in allversions]
1053
+ missingincurrent_leaderboard=list(set(allversions)-set(currentversions))
1054
+ print(missingincurrent_leaderboard)
1055
+
1056
+ #TODO: check if items in leaderboard, if so, then do following
1057
+ if len(missingincurrent_leaderboard)>0:
1058
+ for i in missingincurrent_leaderboard:
1059
+ try:
1060
+ with open("/tmp/"+mastertable_path.split("/")[-1]+"_v"+str(i)+".csv", "wb") as lbfo:
1061
+ bucketres.download_fileobj("$unique_model_id/"+mastertable_path+"_v"+str(i)+".csv", lbfo)
1062
+ newleaderboard = pd.read_csv("/tmp/"+mastertable_path.split("/")[-1]+"_v"+str(i)+".csv", sep="\t")
1063
+ newleaderboard.drop(newleaderboard.filter(regex="Unname"),axis=1, inplace=True)
1064
+ except Exception as e:
1065
+ print(f"Warning: Could not read leaderboard for version {i}: {e}")
1066
+ continue
1067
+
1068
+ # Ensure newleaderboard is a DataFrame before concat
1069
+ newleaderboard = _ensure_df(newleaderboard)
1070
+
1071
+ # Use pd.concat instead of deprecated append
1072
+ leaderboard = pd.concat([leaderboard, newleaderboard], ignore_index=True)
1073
+
1074
+ # Deduplicate once after all concatenations, keeping last occurrence
1075
+ if 'version' in leaderboard.columns:
1076
+ leaderboard = leaderboard.drop_duplicates(subset=['version'], keep='last')
1077
+ else:
1078
+ leaderboard = leaderboard.drop_duplicates(keep='last')
1079
+
1080
+ leaderboard.drop(leaderboard.filter(regex="Unname"),axis=1, inplace=True)
1081
+ #save new leaderboard here
1082
+ leaderboard.to_csv("/tmp/"+mastertable_path.split("/")[-1]+".csv",sep="\t",index=False)
1083
+ s3_client.upload_file("/tmp/"+mastertable_path.split("/")[-1]+".csv", bucket, model_id + "/"+mastertable_path+".csv")
1084
+
1085
+
1086
+ else:
1087
+ pass
1088
+ s3 = boto3.resource("s3")
1089
+ bucket = s3.Bucket("$bucket_name")
1090
+ with open("/tmp/"+mastertable_path.split("/")[-1]+".csv", "wb") as lbfo:
1091
+ bucket.download_fileobj("$unique_model_id/"+mastertable_path+".csv", lbfo)
1092
+ leaderboard = pd.read_csv("/tmp/"+mastertable_path.split("/")[-1]+".csv", sep="\t")
1093
+
1094
+
1095
+ clf =["accuracy", "f1_score", "precision", "recall"]
1096
+ reg = ['mse', 'rmse', 'mae', 'r2']
1097
+ other = ['timestamp']
1098
+
1099
+ if columns:
1100
+ leaderboard = leaderboard.filter(clf+reg+columns+other)
1101
+
1102
+
1103
+ if task_type == "classification":
1104
+ leaderboard_eval_metrics = leaderboard[clf]
1105
+ else:
1106
+ leaderboard_eval_metrics = leaderboard[reg]
1107
+
1108
+ leaderboard_model_meta = leaderboard.drop(clf+reg, axis=1).replace(0,np.nan).dropna(axis=1,how="all")
1109
+
1110
+ leaderboard = pd.concat([leaderboard_eval_metrics, leaderboard_model_meta], axis=1, ignore_index=False)
1111
+
1112
+ if verbose == 1:
1113
+ leaderboard = leaderboard.filter(regex=("^(?!.*(_layers|_act))"))
1114
+ elif verbose == 2:
1115
+ leaderboard = leaderboard.filter(regex=("^(?!.*_act)"))
1116
+
1117
+
1118
+ if task_type == "classification":
1119
+ sort_cols = ["accuracy", "f1_score", "precision", "recall"]
1120
+ #leaderboard = leaderboard.drop(columns = ['mse', 'rmse', 'mae', 'r2'])
1121
+
1122
+ else:
1123
+ sort_cols = ["-mae", "r2"]
1124
+
1125
+ ranks = []
1126
+ for col in sort_cols:
1127
+ ascending = False
1128
+ if col[0] == "-":
1129
+ col = col[1:]
1130
+ ascending = True
1131
+
1132
+ ranks.append(leaderboard[col].rank(method="dense", ascending=ascending))
1133
+
1134
+ ranks = np.mean(ranks, axis=0)
1135
+ order = np.argsort(ranks)
1136
+
1137
+ leaderboard = leaderboard.loc[order].reset_index().drop("index", axis=1).drop_duplicates(subset=['version', 'username'], keep='last')
1138
+ leaderboard.drop(leaderboard.filter(regex="Unname"),axis=1, inplace=True)
1139
+ # }}}
1140
+
1141
+ leaderboard['username']=leaderboard.pop("username")
1142
+ leaderboard['timestamp'] = leaderboard.pop("timestamp")
1143
+ leaderboard['version'] = leaderboard.pop("version")
1144
+
1145
+ try:
1146
+ leaderboard = leaderboard.drop(columns="model_config")
1147
+ except:
1148
+ pass
1149
+
1150
+ return leaderboard
1151
+
1152
+
1153
+
1154
+ def layer_mapping(direction='torch_to_keras', activation=False):
1155
+
1156
+ torch_keras = {'AdaptiveAvgPool1d': 'AvgPool1D',
1157
+ 'AdaptiveAvgPool2d': 'AvgPool2D',
1158
+ 'AdaptiveAvgPool3d': 'AvgPool3D',
1159
+ 'AdaptiveMaxPool1d': 'MaxPool1D',
1160
+ 'AdaptiveMaxPool2d': 'MaxPool2D',
1161
+ 'AdaptiveMaxPool3d': 'MaxPool3D',
1162
+ 'AlphaDropout': None,
1163
+ 'AvgPool1d': 'AvgPool1D',
1164
+ 'AvgPool2d': 'AvgPool2D',
1165
+ 'AvgPool3d': 'AvgPool3D',
1166
+ 'BatchNorm1d': 'BatchNormalization',
1167
+ 'BatchNorm2d': 'BatchNormalization',
1168
+ 'BatchNorm3d': 'BatchNormalization',
1169
+ 'Bilinear': None,
1170
+ 'ConstantPad1d': None,
1171
+ 'ConstantPad2d': None,
1172
+ 'ConstantPad3d': None,
1173
+ 'Container': None,
1174
+ 'Conv1d': 'Conv1D',
1175
+ 'Conv2d': 'Conv2D',
1176
+ 'Conv3d': 'Conv3D',
1177
+ 'ConvTranspose1d': 'Conv1DTranspose',
1178
+ 'ConvTranspose2d': 'Conv2DTranspose',
1179
+ 'ConvTranspose3d': 'Conv3DTranspose',
1180
+ 'CosineSimilarity': None,
1181
+ 'CrossMapLRN2d': None,
1182
+ 'DataParallel': None,
1183
+ 'Dropout': 'Dropout',
1184
+ 'Dropout2d': 'Dropout',
1185
+ 'Dropout3d': 'Dropout',
1186
+ 'Embedding': 'Embedding',
1187
+ 'EmbeddingBag': 'Embedding',
1188
+ 'FeatureAlphaDropout': None,
1189
+ 'Flatten': 'Flatten',
1190
+ 'Fold': None,
1191
+ 'FractionalMaxPool2d': "MaxPool2D",
1192
+ 'FractionalMaxPool3d': "MaxPool3D",
1193
+ 'GRU': 'GRU',
1194
+ 'GRUCell': 'GRUCell',
1195
+ 'GroupNorm': None,
1196
+ 'Identity': None,
1197
+ 'InstanceNorm1d': None,
1198
+ 'InstanceNorm2d': None,
1199
+ 'InstanceNorm3d': None,
1200
+ 'LPPool1d': None,
1201
+ 'LPPool2d': None,
1202
+ 'LSTM': 'LSTM',
1203
+ 'LSTMCell': 'LSTMCell',
1204
+ 'LayerNorm': None,
1205
+ 'Linear': 'Dense',
1206
+ 'LocalResponseNorm': None,
1207
+ 'MaxPool1d': 'MaxPool1D',
1208
+ 'MaxPool2d': 'MaxPool2D',
1209
+ 'MaxPool3d': 'MaxPool3D',
1210
+ 'MaxUnpool1d': None,
1211
+ 'MaxUnpool2d': None,
1212
+ 'MaxUnpool3d': None,
1213
+ 'Module': None,
1214
+ 'ModuleDict': None,
1215
+ 'ModuleList': None,
1216
+ 'PairwiseDistance': None,
1217
+ 'Parameter': None,
1218
+ 'ParameterDict': None,
1219
+ 'ParameterList': None,
1220
+ 'PixelShuffle': None,
1221
+ 'RNN': 'RNN',
1222
+ 'RNNBase': None,
1223
+ 'RNNCell': None,
1224
+ 'RNNCellBase': None,
1225
+ 'ReflectionPad1d': None,
1226
+ 'ReflectionPad2d': None,
1227
+ 'ReplicationPad1d': None,
1228
+ 'ReplicationPad2d': None,
1229
+ 'ReplicationPad3d': None,
1230
+ 'Sequential': None,
1231
+ 'SyncBatchNorm': None,
1232
+ 'Transformer': None,
1233
+ 'TransformerDecoder': None,
1234
+ 'TransformerDecoderLayer': None,
1235
+ 'TransformerEncoder': None,
1236
+ 'TransformerEncoderLayer': None,
1237
+ 'Unfold': None,
1238
+ 'Upsample': 'UpSampling1D',
1239
+ 'UpsamplingBilinear2d': 'UpSampling2D',
1240
+ 'UpsamplingNearest2d': 'UpSampling2D',
1241
+ 'ZeroPad2d': 'ZeroPadding2D'}
1242
+
1243
+ keras_torch = {'AbstractRNNCell': None,
1244
+ 'Activation': None,
1245
+ 'ActivityRegularization': None,
1246
+ 'Add': None,
1247
+ 'AdditiveAttention': None,
1248
+ 'AlphaDropout': None,
1249
+ 'Attention': None,
1250
+ 'Average': None,
1251
+ 'AveragePooling1D': 'AvgPool1d',
1252
+ 'AveragePooling2D': 'AvgPool2d',
1253
+ 'AveragePooling3D': 'AvgPool3d',
1254
+ 'AvgPool1D': 'AvgPool1d',
1255
+ 'AvgPool2D': 'AvgPool2d',
1256
+ 'AvgPool3D': 'AvgPool3d',
1257
+ 'BatchNormalization': None,
1258
+ 'Bidirectional': None,
1259
+ 'Concatenate': None,
1260
+ 'Conv1D': 'Conv1d',
1261
+ 'Conv1DTranspose': 'ConvTranspose1d',
1262
+ 'Conv2D': 'Conv2d',
1263
+ 'Conv2DTranspose': 'ConvTranspose2d',
1264
+ 'Conv3D': 'Conv3d',
1265
+ 'Conv3DTranspose': 'ConvTranspose3d',
1266
+ 'ConvLSTM2D': None,
1267
+ 'Convolution1D': None,
1268
+ 'Convolution1DTranspose': None,
1269
+ 'Convolution2D': None,
1270
+ 'Convolution2DTranspose': None,
1271
+ 'Convolution3D': None,
1272
+ 'Convolution3DTranspose': None,
1273
+ 'Cropping1D': None,
1274
+ 'Cropping2D': None,
1275
+ 'Cropping3D': None,
1276
+ 'Dense': 'Linear',
1277
+ 'DenseFeatures': None,
1278
+ 'DepthwiseConv2D': None,
1279
+ 'Dot': None,
1280
+ 'Dropout': 'Dropout',
1281
+ 'Embedding': 'Embedding',
1282
+ 'Flatten': 'Flatten',
1283
+ 'GRU': 'GRU',
1284
+ 'GRUCell': 'GRUCell',
1285
+ 'GaussianDropout': None,
1286
+ 'GaussianNoise': None,
1287
+ 'GlobalAveragePooling1D': None,
1288
+ 'GlobalAveragePooling2D': None,
1289
+ 'GlobalAveragePooling3D': None,
1290
+ 'GlobalAvgPool1D': None,
1291
+ 'GlobalAvgPool2D': None,
1292
+ 'GlobalAvgPool3D': None,
1293
+ 'GlobalMaxPool1D': None,
1294
+ 'GlobalMaxPool2D': None,
1295
+ 'GlobalMaxPool3D': None,
1296
+ 'GlobalMaxPooling1D': None,
1297
+ 'GlobalMaxPooling2D': None,
1298
+ 'GlobalMaxPooling3D': None,
1299
+ 'Input': None,
1300
+ 'InputLayer': None,
1301
+ 'InputSpec': None,
1302
+ 'LSTM': 'LSTM',
1303
+ 'LSTMCell': 'LSTMCell',
1304
+ 'Lambda': None,
1305
+ 'Layer': None,
1306
+ 'LayerNormalization': None,
1307
+ 'LocallyConnected1D': None,
1308
+ 'LocallyConnected2D': None,
1309
+ 'Masking': None,
1310
+ 'MaxPool1D': 'MaxPool1d',
1311
+ 'MaxPool2D': 'MaxPool2d',
1312
+ 'MaxPool3D': 'MaxPool3d',
1313
+ 'MaxPooling1D': 'MaxPool1d',
1314
+ 'MaxPooling2D': 'MaxPool2d',
1315
+ 'MaxPooling3D': 'MaxPool3d',
1316
+ 'Maximum': None,
1317
+ 'Minimum': None,
1318
+ 'MultiHeadAttention': None,
1319
+ 'Multiply': None,
1320
+ 'Permute': None,
1321
+ 'RNN': 'RNN',
1322
+ 'RepeatVector': None,
1323
+ 'Reshape': None,
1324
+ 'SeparableConv1D': None,
1325
+ 'SeparableConv2D': None,
1326
+ 'SeparableConvolution1D': None,
1327
+ 'SeparableConvolution2D': None,
1328
+ 'SimpleRNN': None,
1329
+ 'SimpleRNNCell': None,
1330
+ 'SpatialDropout1D': None,
1331
+ 'SpatialDropout2D': None,
1332
+ 'SpatialDropout3D': None,
1333
+ 'StackedRNNCells': None,
1334
+ 'Subtract': None,
1335
+ 'TimeDistributed': None,
1336
+ 'UpSampling1D': 'Upsample',
1337
+ 'UpSampling2D': None,
1338
+ 'UpSampling3D': None,
1339
+ 'Wrapper': None,
1340
+ 'ZeroPadding1D': None,
1341
+ 'ZeroPadding2D': 'ZeroPad2d',
1342
+ 'ZeroPadding3D': None}
1343
+
1344
+ torch_keras_act = {
1345
+ 'AdaptiveLogSoftmaxWithLoss': None,
1346
+ 'CELU': None,
1347
+ 'ELU': 'elu',
1348
+ 'GELU': 'gelu',
1349
+ 'GLU': None,
1350
+ 'Hardshrink': None,
1351
+ 'Hardsigmoid': 'hard_sigmoid',
1352
+ 'Hardswish': None,
1353
+ 'Hardtanh': None,
1354
+ 'LeakyReLU': 'LeakyReLU',
1355
+ 'LogSigmoid': None,
1356
+ 'LogSoftmax': None,
1357
+ 'Mish': None,
1358
+ 'MultiheadAttention': None,
1359
+ 'PReLU': 'PReLU',
1360
+ 'RReLU': None,
1361
+ 'ReLU': 'relu',
1362
+ 'ReLU6': 'relu',
1363
+ 'SELU': 'selu',
1364
+ 'SiLU': 'swish',
1365
+ 'Sigmoid': 'sigmoid',
1366
+ 'Softmax': 'softmax',
1367
+ 'Softmax2d': None,
1368
+ 'Softmin': None,
1369
+ 'Softplus': 'softplus',
1370
+ 'Softshrink': None,
1371
+ 'Softsign': 'softsign',
1372
+ 'Tanh': 'tanh',
1373
+ 'Tanhshrink': None,
1374
+ 'Threshold': None}
1375
+
1376
+ keras_torch_act = {
1377
+ 'ELU': 'ELU',
1378
+ 'LeakyReLU': 'LeakyReLU',
1379
+ 'PReLU': 'PReLU',
1380
+ 'ReLU': 'ReLU',
1381
+ 'Softmax': 'Softmax',
1382
+ 'ThresholdedReLU': None,
1383
+ 'elu': 'ELU',
1384
+ 'exponential': None,
1385
+ 'gelu': 'GELU',
1386
+ 'hard_sigmoid': 'Hardsigmoid',
1387
+ 'relu': 'ReLU',
1388
+ 'selu': 'SELU',
1389
+ 'serialize': None,
1390
+ 'sigmoid': 'Sigmoid',
1391
+ 'softmax': 'Softmax',
1392
+ 'softplus': 'Softplus',
1393
+ 'softsign': 'Softsign',
1394
+ 'swish': 'SiLU',
1395
+ 'tanh': 'Tanh'}
1396
+
1397
+
1398
+ if direction == 'torch_to_keras' and activation:
1399
+
1400
+ return torch_keras_act
1401
+
1402
+ elif direction == 'kreas_to_torch' and not activation:
1403
+
1404
+ return keras_torch_act
1405
+
1406
+ elif direction == 'torch_to_keras':
1407
+
1408
+ return torch_keras
1409
+
1410
+ elif direction == 'keras_to_torch':
1411
+
1412
+ return keras_torch
1413
+
1414
+
1415
+ def rename_layers(in_layers, direction="torch_to_keras", activation=False):
1416
+
1417
+ mapping_dict = layer_mapping(direction=direction, activation=activation)
1418
+
1419
+ out_layers = []
1420
+
1421
+ for i in in_layers:
1422
+
1423
+ layer_name_temp = mapping_dict.get(i, None)
1424
+
1425
+ if layer_name_temp == None:
1426
+ out_layers.append(i)
1427
+ else:
1428
+ out_layers.append(layer_name_temp)
1429
+
1430
+ return out_layers
1431
+
1432
+
1433
+
1434
+
1435
+ ####################################################################
1436
+ ############################ S3 connect ############################
1437
+
1438
+
1439
+ import boto3
1440
+ import pandas as pd
1441
+ import os
1442
+ import json
1443
+ import pickle
1444
+ import six
1445
+ import onnx
1446
+ import logging
1447
+ from botocore.exceptions import ClientError
1448
+
1449
+ def get_exampledata(example_data_filename = "exampledata.json"):
1450
+ s3 = boto3.resource("s3")
1451
+ bucket = s3.Bucket("$bucket_name")
1452
+
1453
+ with open("/tmp/exampledata.json", "wb") as exampledatapath:
1454
+ bucket.download_fileobj("$unique_model_id/exampledata.json", exampledatapath)
1455
+ exampledatajson = json.load(open("/tmp/exampledata.json","rb") )
1456
+ return exampledatajson
1457
+
1458
+ def get_ytestdata(ytest_s3_filename="ytest.pkl"):
1459
+
1460
+ s3 = boto3.resource("s3")
1461
+ bucket = s3.Bucket("$bucket_name")
1462
+
1463
+ with open("/tmp/ytest.pkl", "wb") as ytestfo:
1464
+ bucket.download_fileobj("$unique_model_id/"+ytest_s3_filename, ytestfo)
1465
+ ytestdata = pickle.load(open("/tmp/ytest.pkl", "rb" ) )
1466
+ return ytestdata
1467
+
1468
+
1469
+ def get_onnx_temp(version):
1470
+
1471
+ onnx_model_name = "onnx_model_v{version}.onnx".format(version = version)
1472
+ s3 = boto3.resource("s3")
1473
+ bucket = s3.Bucket("$bucket_name")
1474
+ with open("/tmp/"+onnx_model_name, "wb") as onnxfo:
1475
+ bucket.download_fileobj("$unique_model_id/"+onnx_model_name, onnxfo)
1476
+ onnx_model = onnx.load("/tmp/"+onnx_model_name)
1477
+ return onnx_model
1478
+
1479
+ def get_onnx_string(version):
1480
+
1481
+ onnx_model_name = "onnx_model_v{version}.onnx".format(version = version)
1482
+ s3 = boto3.resource('s3')
1483
+ obj = s3.Object("$bucket_name", "$unique_model_id/"+onnx_model_name)
1484
+ onnx_string = obj.get()['Body'].read()
1485
+
1486
+ return onnx_string
1487
+
1488
+ def get_model_metadata(version, submission_type):
1489
+ s3 = boto3.resource("s3")
1490
+ bucket = s3.Bucket("$bucket_name")
1491
+
1492
+ try:
1493
+ if version == None:
1494
+ with open("/tmp/metadata.json", "wb") as temp_path:
1495
+ bucket.download_fileobj("$unique_model_id/"+submission_type+"/runtime_metadata.json", temp_path)
1496
+ else:
1497
+ with open("/tmp/metadata.json", "wb") as temp_path:
1498
+ bucket.download_fileobj("$unique_model_id/"+submission_type+"/model_metadata_v{}.json".format(version), temp_path)
1499
+
1500
+ model_metadata_json = json.load(open("/tmp/metadata.json","rb"))
1501
+ except botocore.exceptions.ClientError as e:
1502
+ if e.response['Error']['Code'] == "404":
1503
+ print("The object does not exist.")
1504
+ return None
1505
+ else:
1506
+ raise
1507
+ return model_metadata_json
1508
+
1509
+ def get_onnx_mem(version):
1510
+
1511
+ onnx_string = get_onnx_string(version)
1512
+ onnx_model = onnx.load_from_string(onnx_string)
1513
+
1514
+ return onnx_model
1515
+
1516
+
1517
+ def upload_file(file_name, bucket, object_name=None):
1518
+ """Upload a file to an S3 bucket
1519
+
1520
+ :param file_name: File to upload
1521
+ :param bucket: Bucket to upload to
1522
+ :param object_name: S3 object name. If not specified then file_name is used
1523
+ :return: True if file was uploaded, else False
1524
+ """
1525
+
1526
+ # If S3 object_name was not specified, use file_name
1527
+ if object_name is None:
1528
+ object_name = file_name
1529
+
1530
+ # Upload the file
1531
+ s3_client = boto3.client('s3')
1532
+ try:
1533
+ response = s3_client.upload_file(file_name, bucket, object_name)
1534
+ except ClientError as e:
1535
+ logging.error(e)
1536
+ return False
1537
+ return True
1538
+
1539
+
1540
+ #Objective: for public competitions: allow all end users to submit to competition as long as they have
1541
+ #...an aimodelshare end username and password, (no aws key / password necessary)
1542
+
1543
+ #TODOs: Use example starter code below plus any code necessary to get model version (i.e. leaderboard data)...
1544
+ #...to allow model submitters access to upload model onnx file + preprocessor (and eventually + post_processor.zip
1545
+ #...crucial thing is that all file uploads and any necessary downloads only allow end users to upload/download files with
1546
+ #...specific new name we desire.
1547
+
1548
+ def _get_file_list(client, bucket,keysubfolderid):
1549
+ # Reading file list {{{
1550
+ try:
1551
+ objectlist=[]
1552
+ paginator = client.get_paginator('list_objects')
1553
+ pages = paginator.paginate(Bucket=bucket, Prefix=keysubfolderid)
1554
+
1555
+ for page in pages:
1556
+ for obj in page['Contents']:
1557
+ objectlist.append(obj['Key'])
1558
+
1559
+ except Exception as err:
1560
+ return None, err
1561
+
1562
+ file_list = []
1563
+ for key in objectlist:
1564
+ file_list.append(key.split("/")[-1])
1565
+ # }}}
1566
+
1567
+ return file_list, None
1568
+
1569
+
1570
+ # STARTER CODE EXPLAINED: Starter code returns url to download (get_object) or upload (put_object) for a single file
1571
+ # need to repeat process for as many files as we allow uploads and downloads for.
1572
+
1573
+ def generate_presigned_url(s3_client, client_method, method_parameters, expires_in):
1574
+ """
1575
+ Generate a presigned Amazon S3 URL that can be used to perform an action.
1576
+
1577
+ :param s3_client: A Boto3 Amazon S3 client.
1578
+ :param client_method: The name of the client method that the URL performs.
1579
+ :param method_parameters: The parameters of the specified client method.
1580
+ :param expires_in: The number of seconds the presigned URL is valid for.
1581
+ :return: The presigned URL.
1582
+ """
1583
+ try:
1584
+ url = s3_client.generate_presigned_url(
1585
+ ClientMethod=client_method,
1586
+ Params=method_parameters,
1587
+ ExpiresIn=expires_in
1588
+ )
1589
+ logger.info("Got presigned URL: %s", url)
1590
+ except ClientError:
1591
+ logger.exception(
1592
+ "Couldn't get a presigned URL for client method '%s'.", client_method)
1593
+ raise
1594
+ return url
1595
+
1596
+ from botocore.exceptions import ClientError
1597
+
1598
+
1599
+ def create_presigned_post(bucket_name, object_name,
1600
+ fields=None, conditions=None, expiration=600):
1601
+ """Generate a presigned URL S3 POST request to upload a file
1602
+
1603
+ :param bucket_name: string
1604
+ :param object_name: string
1605
+ :param fields: Dictionary of prefilled form fields
1606
+ :param conditions: List of conditions to include in the policy
1607
+ :param expiration: Time in seconds for the presigned URL to remain valid
1608
+ :return: Dictionary with the following keys:
1609
+ url: URL to post to
1610
+ fields: Dictionary of form fields and values to submit with the POST
1611
+ :return: None if error.
1612
+ """
1613
+
1614
+ # Generate a presigned S3 POST URL
1615
+ s3_client = boto3.client('s3')
1616
+ try:
1617
+ response = s3_client.generate_presigned_post(bucket_name,
1618
+ object_name,
1619
+ Fields=fields,
1620
+ Conditions=conditions,
1621
+ ExpiresIn=expiration)
1622
+ except ClientError as e:
1623
+ logging.error(e)
1624
+ return None
1625
+
1626
+ # The response contains the presigned URL and required fields
1627
+ return response
1628
+
1629
+ def get_authorizedcompetitionuserdata(example_data_filename = "competitionuserdata.json"):
1630
+ s3 = boto3.resource("s3")
1631
+ bucket = s3.Bucket("$bucket_name")
1632
+
1633
+ with open("/tmp/competitionuserdata.json", "wb") as exampledatapath:
1634
+ bucket.download_fileobj("$unique_model_id/"+example_data_filename, exampledatapath)
1635
+ competitionuserdatajson = json.load(open("/tmp/competitionuserdata.json","rb") )
1636
+ return competitionuserdatajson
1637
+
1638
+ def get_public_private_split(submission_type="competition"):
1639
+ s3 = boto3.resource("s3")
1640
+ bucket = s3.Bucket("$bucket_name")
1641
+
1642
+ with open("/tmp/public_private_split.json", "wb") as exampledatapath:
1643
+ bucket.download_fileobj("$unique_model_id/"+submission_type+"/public_private_split.json", exampledatapath)
1644
+ public_private_split_dict = json.load(open("/tmp/public_private_split.json","rb"))
1645
+ return float(public_private_split_dict["public_private_split"])
1646
+
1647
+ def get_reproducibility_env(version=None,submission_type="competition"):
1648
+ s3 = boto3.resource("s3")
1649
+ bucket = s3.Bucket("$bucket_name")
1650
+
1651
+ try:
1652
+ if version == None:
1653
+ with open("/tmp/reproducibility.json", "wb") as temp_path:
1654
+ bucket.download_fileobj("$unique_model_id/runtime_reproducibility.json", temp_path)
1655
+ else:
1656
+ with open("/tmp/reproducibility.json", "wb") as temp_path:
1657
+ bucket.download_fileobj("$unique_model_id/" + submission_type + "/reproducibility_v{}.json".format(version), temp_path)
1658
+
1659
+ reproducibility_env_json = json.load(open("/tmp/reproducibility.json","rb"))
1660
+ except botocore.exceptions.ClientError as e:
1661
+ if e.response['Error']['Code'] == "404":
1662
+ print("The object does not exist.")
1663
+ return None
1664
+ else:
1665
+ raise
1666
+ return reproducibility_env_json
1667
+
1668
+ def get_eval_metric(eval_metric_s3_filename, submission_type):
1669
+
1670
+ import pickle
1671
+ from zipfile import ZipFile
1672
+ from io import BytesIO
1673
+ import os
1674
+
1675
+ s3 = boto3.resource("s3")
1676
+ bucket = s3.Bucket("$bucket_name")
1677
+
1678
+ zip_obj = s3.Object(bucket_name="$bucket_name",
1679
+ key="$unique_model_id/"+submission_type+"/"+eval_metric_s3_filename)
1680
+
1681
+ buffer = BytesIO(zip_obj.get()["Body"].read())
1682
+
1683
+ z = ZipFile(buffer)
1684
+ # Extract all the contents of zip file in current directory
1685
+ z.extractall("/tmp/")
1686
+
1687
+ metric_py = eval_metric_s3_filename.split('.')[-2] + '.py'
1688
+
1689
+ folderpath = os.path.dirname(os.path.abspath("/tmp/"+metric_py))
1690
+ file_name = os.path.basename("/tmp/"+metric_py)
1691
+
1692
+ pickle_file_list = []
1693
+ for file in os.listdir(folderpath):
1694
+ if file.endswith(".pkl"):
1695
+ pickle_file_list.append(os.path.join(folderpath, file))
1696
+
1697
+ for i in pickle_file_list:
1698
+ objectname = str(os.path.basename(i)).replace(".pkl", "")
1699
+ objects = {objectname: ""}
1700
+ globals()[objectname] = pickle.load(open(str(i), "rb"))
1701
+
1702
+ metric_py = metric_py.replace("metrics_", "")
1703
+
1704
+ exec(open(os.path.join(folderpath, metric_py)).read(), globals())
1705
+
1706
+ print(globals()['custom_eval_metric'])
1707
+ eval_metric = globals()['custom_eval_metric']
1708
+
1709
+ return eval_metric