-
Notifications
You must be signed in to change notification settings - Fork 90
/
inspectorscripts.ts
251 lines (218 loc) · 8.54 KB
/
inspectorscripts.ts
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
export
namespace Languages {
export
type LanguageModel = {
initScript: string;
queryCommand: string;
matrixQueryCommand: string;
}
}
export
abstract class Languages {
/**
* Init and query script for supported languages.
*/
static py_script: string = `import json
from sys import getsizeof
from IPython import get_ipython
from IPython.core.magics.namespace import NamespaceMagics
_jupyterlab_variableinspector_nms = NamespaceMagics()
_jupyterlab_variableinspector_Jupyter = get_ipython()
_jupyterlab_variableinspector_nms.shell = _jupyterlab_variableinspector_Jupyter.kernel.shell
try:
import numpy as np
except ImportError:
np = None
try:
import pandas as pd
except ImportError:
pd = None
try:
import pyspark
except ImportError:
pyspark = None
try:
import tensorflow as tf
import keras.backend as K
except ImportError:
tf = None
def _jupyterlab_variableinspector_getsizeof(x):
if type(x).__name__ in ['ndarray', 'Series']:
return x.nbytes
elif pyspark and isinstance(x, pyspark.sql.DataFrame):
return "?"
elif tf and isinstance(x, tf.Variable):
return "?"
elif pd and type(x).__name__ == 'DataFrame':
return x.memory_usage().sum()
else:
return getsizeof(x)
def _jupyterlab_variableinspector_getshapeof(x):
if pd and isinstance(x, pd.DataFrame):
return "DataFrame [%d rows x %d cols]" % x.shape
if pd and isinstance(x, pd.Series):
return "Series [%d rows]" % x.shape
if np and isinstance(x, np.ndarray):
shape = " x ".join([str(i) for i in x.shape])
return "Array [%s]" % shape
if pyspark and isinstance(x, pyspark.sql.DataFrame):
return "Spark DataFrame [? rows x %d cols]" % len(x.columns)
if tf and isinstance(x, tf.Variable):
shape = " x ".join([str(int(i)) for i in x.shape])
return "Tensorflow Variable [%s]" % shape
if tf and isinstance(x, tf.Tensor):
shape = " x ".join([str(int(i)) for i in x.shape])
return "Tensorflow Tensor [%s]" % shape
return None
def _jupyterlab_variableinspector_getcontentof(x):
# returns content in a friendly way for python variables
# pandas and numpy
if pd and isinstance(x, pd.DataFrame):
colnames = ', '.join(x.columns.map(str))
content = "Column names: %s" % colnames
elif pd and isinstance(x, pd.Series):
content = "Series [%d rows]" % x.shape
elif np and isinstance(x, np.ndarray):
content = x.__repr__()
else:
content = str(x)
if len(content) > 150:
return content[:150] + " ..."
else:
return content
def _jupyterlab_variableinspector_is_matrix(x):
# True if type(x).__name__ in ["DataFrame", "ndarray", "Series"] else False
if pd and isinstance(x, pd.DataFrame):
return True
if pd and isinstance(x, pd.Series):
return True
if np and isinstance(x, np.ndarray) and len(x.shape) <= 2:
return True
if pyspark and isinstance(x, pyspark.sql.DataFrame):
return True
if tf and isinstance(x, tf.Variable) and len(x.shape) <= 2:
return True
if tf and isinstance(x, tf.Tensor) and len(x.shape) <= 2:
return True
return False
def _jupyterlab_variableinspector_dict_list():
def keep_cond(v):
try:
obj = eval(v)
if isinstance(obj, str):
return True
if tf and isinstance(obj, tf.Variable):
return True
if pd and pd is not None and (
isinstance(obj, pd.core.frame.DataFrame)
or isinstance(obj, pd.core.series.Series)):
return True
if str(obj)[0] == "<":
return False
if v in ['np', 'pd', 'pyspark', 'tf']:
return obj is not None
if str(obj).startswith("_Feature"):
# removes tf/keras objects
return False
return True
except:
return False
values = _jupyterlab_variableinspector_nms.who_ls()
vardic = [{'varName': _v,
'varType': type(eval(_v)).__name__,
'varSize': str(_jupyterlab_variableinspector_getsizeof(eval(_v))),
'varShape': str(_jupyterlab_variableinspector_getshapeof(eval(_v))) if _jupyterlab_variableinspector_getshapeof(eval(_v)) else '',
'varContent': str(_jupyterlab_variableinspector_getcontentof(eval(_v))),
'isMatrix': _jupyterlab_variableinspector_is_matrix(eval(_v))}
for _v in values if keep_cond(_v)]
return json.dumps(vardic, ensure_ascii=False)
def _jupyterlab_variableinspector_getmatrixcontent(x, max_rows=10000):
# to do: add something to handle this in the future
threshold = max_rows
if pd and pyspark and isinstance(x, pyspark.sql.DataFrame):
df = x.limit(threshold).toPandas()
return _jupyterlab_variableinspector_getmatrixcontent(df.copy())
elif np and pd and type(x).__name__ in ["Series", "DataFrame"]:
if threshold is not None:
x = x.head(threshold)
x.columns = x.columns.map(str)
return x.to_json(orient="table", default_handler=_jupyterlab_variableinspector_default)
elif np and pd and type(x).__name__ in ["ndarray"]:
df = pd.DataFrame(x)
if threshold is not None:
df = df.head(threshold)
df.columns = df.columns.map(str)
return df.to_json(orient="table", default_handler=_jupyterlab_variableinspector_default)
elif tf and (isinstance(x, tf.Variable) or isinstance(x, tf.Tensor)):
df = K.get_value(x)
return _jupyterlab_variableinspector_getmatrixcontent(df)
def _jupyterlab_variableinspector_default(o):
if isinstance(o, np.number): return int(o)
raise TypeError
`;
static r_script: string = `
# improved list of objects
.ls.objects <- function (pos = 1, pattern, order.by,
decreasing=FALSE, head=FALSE, n=5) {
napply <- function(names, fn) sapply(names, function(x)
fn(get(x, pos = pos)))
names <- ls(pos = pos, pattern = pattern)
if (length(names) == 0){
return(jsonlite::toJSON(data.frame()))
}
obj.class <- napply(names, function(x) as.character(class(x))[1])
obj.mode <- napply(names, mode)
obj.type <- ifelse(is.na(obj.class), obj.mode, obj.class)
obj.size <- napply(names, object.size)
obj.dim <- t(napply(names, function(x)
as.numeric(dim(x))[1:2]))
vec <- is.na(obj.dim)[, 1] & (obj.type != "function")
obj.dim[vec, 1] <- napply(names, length)[vec]
out <- data.frame(obj.type, obj.size, obj.dim)
names(out) <- c("varType", "varSize", "Rows", "Columns")
out$varShape <- paste(out$Rows, " x ", out$Columns)
out$varContent <- sapply(names, function(x) toString(get(x))[1])
out$isMatrix <- FALSE
out$varName <- row.names(out)
# drop columns Rows and Columns
out <- out[, !(names(out) %in% c("Rows", "Columns"))]
rownames(out) <- NULL
if (!missing(order.by))
out <- out[order(out[[order.by]], decreasing=decreasing), ]
if (head)
out <- head(out, n)
jsonlite::toJSON(out)
}
`;
static scripts: { [index: string]: Languages.LanguageModel } = {
"python3": {
initScript: Languages.py_script,
queryCommand: "_jupyterlab_variableinspector_dict_list()",
matrixQueryCommand: "_jupyterlab_variableinspector_getmatrixcontent"
},
"python2": {
initScript: Languages.py_script,
queryCommand: "_jupyterlab_variableinspector_dict_list()",
matrixQueryCommand: "_jupyterlab_variableinspector_getmatrixcontent"
},
"python": {
initScript: Languages.py_script,
queryCommand: "_jupyterlab_variableinspector_dict_list()",
matrixQueryCommand: "_jupyterlab_variableinspector_getmatrixcontent"
},
"R": {
initScript: Languages.r_script,
queryCommand: ".ls.objects()",
matrixQueryCommand: ".ls.objects"
}
};
public static getScript( lang: string ): Promise<Languages.LanguageModel> {
return new Promise( function( resolve, reject ) {
if ( lang in Languages.scripts ) {
resolve( Languages.scripts[lang] );
} else {
reject( "Language " + lang + " not supported yet!" );
}
} );
}
}