-
Notifications
You must be signed in to change notification settings - Fork 0
/
plotdata_info.py
166 lines (137 loc) · 5.31 KB
/
plotdata_info.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
# Python 2/3 compatability
from __future__ import (absolute_import, division, print_function,
unicode_literals)
# add support for raw_input to Python 3
from builtins import input
try:
input = raw_input
except NameError:
pass
def plotdata_info(xdata=None, ydata=None,
xrange=None, yrange=None,
verbose=True):
"""
xdata is required for univariate analysis such as a histogram
xdata and ydata are required for bivariate analysis like a scatter plot
"""
import numpy as np
if xdata is None:
print('Returning since xdata=None')
return
# create Dict to store the info
info = {}
ndata = len(xdata)
info['ndata_all'] = ndata
info['ndata'] = ndata
info['nxdata'] = ndata
if ydata is not None:
info['nydata'] = len(ydata)
# x-axis data
print('xrange:', xrange)
# get full range of xdata
xdata_min = np.min(xdata)
xdata_max = np.max(xdata)
info['xdata_range'] = [xdata_min, xdata_max]
# get full range excluding NANs
xdata_min_notnan = np.nanmin(xdata)
xdata_max_notnan = np.nanmax(xdata)
info['xdata_min_notnan'] = xdata_min_notnan
info['xdata_max_notnan'] = xdata_max_notnan
# some convenience redundancy
info['xdata_range_notnan'] = [xdata_min_notnan,
xdata_max_notnan]
# count the NANs
info['ndata_xnan'] = np.isnan(xdata).sum()
# count the INFs
info['ndata_xinf'] = np.isinf(xdata).sum()
# y-axis data
if ydata is not None:
print('yrange:', yrange)
if yrange is not None:
iydata = (ydata > yrange[0]) & (ydata < yrange[1])
nydata = len(ydata[iydata])
ydata_min = np.min(ydata)
ydata_max = np.max(ydata)
info['ydata_range'] = [xdata_min, xdata_max]
ydata_min_notnan = np.nanmin(ydata)
ydata_max_notnan = np.nanmax(ydata)
info['ydata_range_notnan'] = [ydata_min_notnan,
ydata_max_notnan]
info['ndata_ynan'] = np.isnan(ydata).sum()
info['ndata_yinf'] = np.isinf(ydata).sum()
ixydata_nan = (np.isnan(xdata) & np.isnan(ydata))
ixydata_inf = (np.isinf(xdata) & np.isinf(ydata))
ndata_xynan = len(xdata[ixydata_nan])
ndata_xyinf = len(xdata[ixydata_inf])
info['ndata_xynan'] = ndata_xynan
info['ndata_xyinf'] = ndata_xyinf
# limit xdata to the xrange
if yrange is not None:
iydata = (ydata > yrange[0]) & (ydata < yrange[1])
nydata = len(ydata[iydata])
info['nydata'] = nydata
# ydata = ydata[iydata]
if xrange and yrange is not None:
idata = (xdata > xrange[0]) & (xdata < xrange[1]) & \
(ydata > yrange[0]) & (ydata < yrange[1])
info['ndata'] = len(xdata[idata])
# limit xdata to the xrange
if xrange is not None:
ixdata = (xdata > xrange[0]) & (xdata < xrange[1])
nxdata = len(xdata[ixdata])
info['nxdata'] = nxdata
if xrange is not None and ydata is None:
xdata = xdata[ixdata]
if xrange and yrange is not None:
xdata = xdata[idata]
ydata = ydata[idata]
if verbose:
xlabel = 'x-axis'
print()
print(xlabel)
print(xlabel + ' range:',
info['xdata_range'][0], info['xdata_range'][1],
info['ndata'])
print(xlabel + ' range (excluding NaNs):',
info['xdata_range_notnan'][0],
info['xdata_range_notnan'][1])
print(xlabel + ' (nan):', info['ndata_xnan'])
print(xlabel + ' (inf):', info['ndata_xinf'])
if verbose and ydata is not None:
ylabel = 'y-axis'
print()
print(xlabel)
print(xlabel + ' range:',
info['ydata_range'][0], info['ydata_range'][1],
info['ndata'])
print(xlabel + ' range (excluding NaNs):',
info['ydata_range_notnan'][0],
info['ydata_range_notnan'][1])
print(xlabel + ' (nan):', info['ndata_ynan'])
print(xlabel + ' (inf):', info['ndata_yinf'])
if xdata is not None and ydata is None:
label = 'label'
if xdata is not None and ydata is not None:
label = 'xy in range; ' + str(info['ndata']) + ' out of: ' + \
str(info['ndata_all']) + '\n' + \
'x, y in range; ' + str(info['nxdata']) + ': ' + \
str(info['nydata']) + '\n' + \
'x range (full); ' + \
str(info['xdata_range_notnan'][0]) + ': ' + \
str(info['xdata_range_notnan'][1]) + '\n' + \
'y range (full); ' + \
str(info['ydata_range_notnan'][0]) + ': ' + \
str(info['ydata_range_notnan'][1]) + '\n' + \
'x, y, xy (out of range): ' + \
str(info['ndata_all'] - info['nxdata']) + ', ' + \
str(info['ndata_all'] - info['nydata']) + ', ' + \
str(info['ndata_all'] - info['ndata']) + '\n' + \
'x, y, xy (nan); ' + \
str(info['ndata_xnan']) + ': ' + \
str(info['ndata_ynan']) + ': ' + \
str(info['ndata_xynan']) + '\n' + \
'x, y (inf); ' + \
str(info['ndata_xinf']) + ': ' + \
str(info['ndata_yinf']) + ': ' + \
str(info['ndata_xyinf'])
return info, label