/
matrix.py
314 lines (265 loc) · 10.4 KB
/
matrix.py
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import csv
import json
from functools import total_ordering
from io import StringIO
from typing import Union
import pandas
from pandas import DataFrame
from canvasxpress.data.base import CXDataProfile, CXMatrixData
from canvasxpress.data.profile import CXStandardProfile
@total_ordering
class CXDataframeData(CXMatrixData):
"""
A CXData class dedicated to processing Python DataFrame, matrix-structured
data.
"""
__data: DataFrame = DataFrame()
"""
The data managed by an object of this class.
"""
@property
def dataframe(self) -> DataFrame:
"""
Provides the data managed by the object.
:returns: `DataFrame` The managed data.
"""
return self.__data
@dataframe.setter
def dataframe(self, value: Union[DataFrame, None] = None) -> None:
"""
Sets the dataframe managed by the object.
:param value: `Union[DataFrame, None]`
`None` results in an empty `DataFrame`. A deepcopy will be made of
`DataFrame` values.
"""
self.data = value
@property
def data(self) -> dict:
"""
Provides the data managed by the object.
:returns: `DataFrame` The managed data.
"""
return self.dataframe.to_dict(orient="list")
@data.setter
def data(
self, value: Union["CXDataframeData", DataFrame, dict, str, None] = None
) -> None:
"""
Sets the dataframe managed by the object.
:param value: `Union['CXDataframeData', DataFrame, dict, str, None]`
`None` results in an empty `DataFrame`. A deepcopy will be made of
`DataFrame` or equivalent values.
"""
if value is None:
self.__data = DataFrame()
elif not type(value) in [CXDataframeData, DataFrame, dict, str]:
raise TypeError("value must be type DataFrame or compatible.")
elif isinstance(value, CXDataframeData):
self.__data = value.dataframe.copy(deep=True)
elif isinstance(value, DataFrame):
self.__data = value.copy(deep=True)
elif isinstance(value, dict):
self.__data = DataFrame.from_dict(value)
else:
# Try a JSON edition
try:
candidate_json = json.loads(value)
candidate = DataFrame.from_dict(candidate_json)
except:
# Try to load a CSV or read it from memory
try:
candidate = pandas.read_csv(value)
except:
if value.strip().startswith(","):
candidate = pandas.read_csv(StringIO(value), index_col=0)
else:
candidate = pandas.read_csv(StringIO(value))
self.__data = DataFrame(candidate)
def get_raw_dict_form(self) -> dict:
""" "
Provides a simple dict perspective of the data with no metadata or other
contextual transformations performed. For example, if the data is
natively in `dict` form then it would be passed-through with no
modification or enhancement.
This implementation provides matrix data formatted in a `dict` object
with `DataFrame.to_dict('split')` behaviour.
:returns: `dict`
The `dict` perspective of the data with as little modification or
interpretation as is reasonable.
"""
return self.__data.to_dict(orient="split")
def render_to_dict(self, **kwargs) -> dict:
"""
Provides a dict representation of the data.
:returns: `dict`
The data in `dict` form.
"""
if self.profile:
candidate = self.profile.render_to_profiled_dict(self)
else:
candidate = self.get_raw_dict_form()
return candidate
def __init__(
self,
data: Union["CXDataframeData", DataFrame, dict, str, None] = None,
profile: Union[CXDataProfile, None] = None,
) -> None:
"""
Initializes the CXData object with data. Only `DataFrame` or compatible
data types are accepted.
:param data: `Union['CXDataframeData', DataFrame, dict, str, None]`
`None` to initialize with an empty `DataFrame`, or a `DataFrame`
like object to assign mapped data.
:param profile: `Union[CXDataProfile, None]`
Specify the desired profile object to facilitate transformation of
data into a CanvasXpress JSON data object. `None` will default to
the CXStandardProfile.
"""
super().__init__(data, profile if profile is not None else CXStandardProfile())
self.data = data
def __copy__(self) -> "CXDataframeData":
"""
*copy constructor* that returns a copy of the CXDataframeData object.
:returns: `CXDataframeData`
A copy of the wrapping object.
"""
return self.__class__(self.data)
def __deepcopy__(self, memo) -> "CXDataframeData":
"""
*deepcopy constructor* that returns a copy of the CXDataframeData object.
:returns: `CXDataframeData` A copy of the wrapping object and deepcopy of
the tracked data.
"""
return self.__class__(self.data)
def __lt__(self, other: "CXDataframeData") -> bool:
"""
*less than* comparison. Also see `@total_ordering` in `functools`.
:param other:
`CXDataframeData` The object to compare.
:returns: `bool`
<ul>
<li> If `other` is `None` then `False`
<li> If `other` is not a `CXDataframeData` object then False
<li> If `other` is a `CXDataframeData` object then True of all
`CXDataframeData` aspects are also less than the data tracked by
`self`.
</ul>
"""
if other is None:
return False
if not isinstance(other, CXDataframeData):
return False
else:
self_c = self.dataframe.columns.unique()
other_c = other.dataframe.columns.unique()
if len(self_c) < len(other_c):
return True
elif len(self_c) > len(other_c):
return False
else:
for i in [s for s in self_c if s not in other_c]:
if any([i < o for o in other_c]):
return True
return self.dataframe.lt(other.dataframe).all(axis=None)
def __eq__(self, other: "CXDataframeData") -> bool:
"""
*equals* comparison. Also see `@total_ordering` in `functools`.
:param other:
`CXDataframeData` The object to compare.
:returns: `bool`
<ul>
<li> If `other` is `None` then `False`
<li> If `other` is not a `CXDataframeData` object then False
<li> If `other` is a `CXDataframeData` object then True of all
`CXDataframeData` aspects are also less than the data tracked by
`self`.
</ul>
"""
if other is None:
return False
if not isinstance(other, CXDataframeData):
return False
else:
self_c = self.dataframe.columns.unique()
other_c = other.dataframe.columns.unique()
if len(self_c) != len(other_c):
return False
if any([s not in other_c for s in self_c]):
return False
return self.dataframe.eq(other.dataframe).all(axis=None)
def __str__(self) -> str:
"""
*str* function. Converts the CXDataframeData object into a JSON
representation.
:returns" `str` JSON form of the `CXDataframeData`.
"""
return json.dumps(self.render_to_dict())
def __repr__(self) -> str:
"""
*repr* function. Converts the CXDataframeData object into a pickle
string that can be used with `eval` to establish a copy of the object.
:returns: `str` An evaluatable representation of the object.
"""
candidate = (
f"CXDataframeData("
f"data=pandas.read_csv("
f'StringIO("""{self.dataframe.to_csv(index=True)}"""),'
f"index_col=0))"
)
candidate = candidate.replace("Infinity", "float('inf')")
candidate = candidate.replace("NaN", "float('nan')")
return candidate
class CXCSVData(CXDataframeData):
"""
A CXData class dedicated to processing Python CSV-based, matrix-structured
data.
"""
@property
def csv(self) -> str:
"""
Provides the data managed by the object.
:returns: `str` The managed data.
"""
candidate = self.dataframe.to_csv(index=False, quoting=csv.QUOTE_NONNUMERIC)
candidate = candidate.replace("nan", "")
return candidate
@csv.setter
def csv(self, value: str = None) -> None:
"""
Sets the CSV data managed by the object.
:param value: `str`
`None` results in an empty CSV. A deepcopy will be made of
valid CSV `str` values.
"""
self.data = value
def __init__(
self,
data: Union["CXCSVData", DataFrame, dict, str, None] = None,
profile: Union[CXDataProfile, None] = None,
) -> None:
"""
Initializes the CXData object with data. Only CSV `str` or compatible
data types are accepted.
:param data: `Union['CXCSVData', DataFrame, dict, str, None]`
`None` to initialize with an empty CSV, or a CSV `str`
like object to assign mapped data.
:param profile: `Union[CXDataProfile, None]`
Specify the desired profile object to facilitate transformation of
data into a CanvasXpress JSON data object. `None` to avoid use of
a profile.
"""
super().__init__(data, profile)
def __str__(self) -> str:
"""
*str* function. Converts the CXCSVData object into a JSON
representation.
:returns" `str` JSON form of the `CXCSVData`.
"""
return self.csv
def __repr__(self) -> str:
"""
*repr* function. Converts the CXCSVData object into a pickle
string that can be used with `eval` to establish a copy of the object.
:returns: `str` An evaluatable representation of the object.
"""
return f'CXCSVData(data="""{self.dataframe.to_csv(index=True)}""")'