-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
executable file
·147 lines (118 loc) · 3.45 KB
/
Copy pathrun.py
File metadata and controls
executable file
·147 lines (118 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env python3
##############################################################################
#
# Run
# This file is the entry point for running the application.
#
# Maintainers:
# - Jurre J. Brandsen (11808918)
# - Sander J. Misdorp (12151785)
# - Tom J. Wassing (12386716)
##############################################################################
import os
import sys
import yaml
import inspect
import ast
import numpy as np
import json
from sklearn.decomposition import PCA
from sklearn import datasets, preprocessing
from sklearn.impute import SimpleImputer
def get_dynamic_args(func):
'''
Get the arguments of a function as a dictionary.
This is used to dynamically set the arguments of a function.
Args:
func: The function to get the arguments of.
Returns:
A dictionary containing the arguments of the function.
'''
args = dict()
signature = inspect.signature(func)
for param in signature.parameters:
# ignore self from constructor
if param == "self":
continue
# try to parse the value from the environment with correct type
value = os.environ.get(param.upper())
if value is not None:
try:
args[param] = ast.literal_eval(value)
except:
args[param] = value
return args
def load_dataset(name, attr):
'''
Load a dataset from sklearn.datasets.
Args:
name: The name of the dataset to load.
attr: The attribute of the dataset to load.
Returns:
The loaded dataset.
'''
load_dataset_func = getattr(datasets, name)
dataset = load_dataset_func()
return getattr(dataset, attr)
def pca_fit_transform(data):
'''
Perform PCA on the data.
Args:
data: The data to perform PCA on.
Returns:
The transformed data.
'''
args = get_dynamic_args(PCA.__init__)
return PCA(**args).fit_transform(data)
def normalize(data):
'''
Normalize the data.
Args:
data: The data to normalize.
Returns:
The normalized data.
'''
return preprocessing.normalize(data)
def simple_imputer(data):
'''
Perform SimpleImputer on the data.
Args:
data: The data to perform SimpleImputer on.
Returns:
The transformed data.
'''
args = get_dynamic_args(SimpleImputer.__init__)
return SimpleImputer(**args).fit_transform(data)
def main(function_name):
'''
Main function.
Args:
function_name: The name of the function to run.
Returns:
The output of the function.
'''
# reading input data
input_data = os.environ.get("INPUT")
data = None
if input_data:
raw_data = json.loads(input_data)
data = np.array(raw_data)
if function_name.startswith("load_"):
data = os.getenv("DATA", "data")
result = load_dataset(function_name, data)
elif function_name == "pca_fit_transform":
result = pca_fit_transform(data)
elif function_name == "normalize":
result = normalize(data)
elif function_name == "simple_imputer":
result = simple_imputer(data)
else:
print("Unknown function: %s" % function_name)
sys.exit(1)
raw_output = json.dumps(result.tolist())
return yaml.dump({"output": raw_output})
if __name__ == "__main__":
function_name = sys.argv[1]
input_data = os.environ.get("INPUT")
output = main(function_name)
print(output)