-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrender.py
More file actions
executable file
·135 lines (113 loc) · 4.06 KB
/
render.py
File metadata and controls
executable file
·135 lines (113 loc) · 4.06 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
# Purpose: Utility wrappers for visualisation
# This is needed for the projections:
# from mpl_toolkits.mplot3d import Axes3D
# Recommended list: python -m pip install --user numpy scipy matplotlib ipython jupyter pandas sympy nose
# >>> import matplotlib.pyplot as plt
# >>> plt.gcf().canvas.get_supported_filetypes()
# {
# 'ps': 'Postscript',
# 'eps': 'Encapsulated Postscript',
# 'pdf': 'Portable Document Format',
# 'pgf': 'PGF code for LaTeX',
# 'png': 'Portable Network Graphics',
# 'raw': 'Raw RGBA bitmap',
# 'rgba': 'Raw RGBA bitmap',
# 'svg': 'Scalable Vector Graphics',
# 'svgz': 'Scalable Vector Graphics'
# }
import sys
import matplotlib
import datetime
from pathlib import Path
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import config
import objects
# https://matplotlib.org/users/installing.html#macos
# xcode-select --install
# (for subprocess32)
# python3 -m pip install matplotlib
# Docs say...
# python -mpip install -U pip
# python -mpip install -U matplotlib
# x = [1,2,3]
# y = [1,2,3]
# da = {
# z_column: [1,2,3,2,3,2,1,0,1],
# x_column: [1,2,3,1,2,3,1,2,3],
# y_column: [1,1,1,2,2,2,3,3,3]
# }
logger = config.get_logger()
def to_logging(df, z_column, x_column, y_column):
da = to_dictarray(df, {z_column: [], x_column: [], y_column: []})
dump_dictarray_head(da)
dump_dictarray_tail(da)
def to_3dscatter(df, z_column, x_column, y_column, output_filepath):
logger.info('Saving 3D Scatter [{}]'.format(output_filepath))
if Axes3D is None:
logger.warning('Axes3D is None')
da = to_dictarray(df, {z_column: [], x_column: [], y_column: []})
dump_dictarray_head(da)
dump_dictarray_tail(da)
z = da[z_column]
x = da[x_column]
y = da[y_column]
fig = plt.figure()
ax = plt.subplot(111, projection='3d')
ax.scatter(x, y, zs=z)
ax.set_xlabel(x_column)
ax.set_ylabel(y_column)
fig.savefig(str(output_filepath))
plt.close(fig)
return output_filepath
def to_3dscatter_3dobject(df, z_column, x_column, y_column, output_filepath):
logger.info('Saving 3D Scatter Object[{}]'.format(output_filepath))
entry_point = Path(sys.argv[0])
model = list()
for index, row in df.iterrows():
po = objects.PlacedObject(o=objects.cube(), x=row[x_column], y=row[y_column], z=row[z_column], size=0.2)
model.append(po)
# model.append(objects.PlacedObject(o=objects.cube(), x=0, y=0, z=0, size=0.2))
# model.append(objects.PlacedObject(o=objects.cube(), x=0, y=0, z=1, size=0.2))
# model.append(objects.PlacedObject(o=objects.cube(), x=1, y=0, z=0, size=0.2))
# model.append(objects.PlacedObject(o=objects.cube(), x=1, y=0, z=1, size=0.2))
objects.save_as_obj(model, output_filepath, generated_by=entry_point.name, group=output_filepath.stem)
def to_dictarray(df, da):
for df_row in df.itertuples():
dfdict_row = df_row._asdict()
dr = {}
for column in da.keys():
try:
da[column].append(float(dfdict_row[column]))
except Exception as e:
da[column].append(dfdict_row[column])
return da
def dump_dictarray_head(da, columns=None):
if columns == None:
columns = da.keys()
logger.debug('head:')
dump_dictarray_sub(da, columns, 0, 5)
def dump_dictarray_tail(da, columns=None):
if columns == None:
columns = da.keys()
maxlen = dictarray_maxlen(da)
logger.debug('tail:')
dump_dictarray_sub(da, columns, max(0, maxlen - 5), maxlen)
def dump_dictarray_sub(da, columns, start, end):
if columns == None:
columns = da.keys()
for i in range(start, end):
row_str = '('
for column in columns:
if i < len(da[column]):
row_str = '{} {}'.format(row_str, da[column][i])
else:
row_str = '{} --'.format(row_str)
logger.debug('[{}]: {} )'.format(i, row_str))
def dictarray_maxlen(da):
columns = da.keys()
maxlen = 0
for column in columns:
maxlen = max(maxlen, len(da[column]))
return maxlen