-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
85 lines (69 loc) · 2.03 KB
/
Copy pathrun.py
File metadata and controls
85 lines (69 loc) · 2.03 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
#!/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 io
import json
import matplotlib.pyplot as plt
import numpy as np
def read_np_array(key):
'''
Reads a numpy array from stdin
Args:
key: the key to read the array from
Returns:
the numpy array
'''
os_data = os.environ.get(key)
if os_data is None:
return None
return np.array(json.loads(os_data))
def main(command):
'''
Main function for running the application.
Args:
command: the command to run
Prints:
The output of the command
'''
# reading input data
data = read_np_array("INPUT")
cmap_data = read_np_array("CMAP_INPUT")
cmap_name = os.environ.get("CMAP")
edge_color = os.environ.get("EDGE_COLOR")
if os.environ.get("SCATTER", False):
plt.scatter(data[:, 0], data[:, 1], c=cmap_data,
cmap=cmap_name, edgecolor=edge_color)
else:
plt.plot(data)
# setting labels and title
plt.xlabel(os.environ.get("X_LABEL", ""))
plt.ylabel(os.environ.get("Y_LABEL", ""))
plt.title(os.environ.get("TITLE", ""))
if command == "plot_base64":
temp_file = io.BytesIO()
plt.savefig(temp_file)
output = temp_file.getvalue()
# write image encoded to stdout
print(yaml.dump({"output": output}))
elif command == "plot_file":
file_path = os.environ.get("FILE_PATH", "/data/output.png")
plt.savefig(file_path)
print(yaml.dump({"output": file_path}))
elif command == "plot_show":
plt.show()
print(yaml.dump({"output": "show"}))
else:
print("Unknown command")
if __name__ == "__main__":
main(sys.argv[1])