Repository navigation
Expand file tree
/
Copy pathcached.py
More file actions
122 lines (97 loc) · 3.04 KB
/
Copy pathcached.py
File metadata and controls
122 lines (97 loc) · 3.04 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
from __future__ import print_function, division, absolute_import
import numpy as np
import pandas as pd
from datashape import dshape, discover, integral, floating, boolean, complexes
from datashape.predicates import isscalar, isrecord, iscollection
from .dispatch import dispatch
from .expr import Expr, Field, ndim
from .compute import compute
from .compatibility import unicode
from odo import odo
class CachedDataset(object):
__slots__ = 'data', 'cache'
def __init__(self, data, cache=None):
self.data = data
self.cache = cache if cache is not None else {}
@dispatch(CachedDataset)
def discover(d, **kwargs):
return discover(d.data, **kwargs)
@dispatch(Field, CachedDataset)
def compute_up(expr, data, **kwargs):
return data.data[expr._name]
@dispatch(Expr, CachedDataset)
def compute_down(expr, data, **kwargs):
if expr in data.cache:
return data.cache[expr]
leaf, = expr._leaves()
# Do work
result = compute(expr, {leaf: data.data}, **kwargs)
ds = expr.dshape
new_type = concrete_type(ds)
# If the result is not the concrete data type for the datashape then make
# it concrete
if not isinstance(result, new_type):
result = odo(result, new_type, dshape=ds)
# Cache result
data.cache[expr] = result
return result
def concrete_type(ds):
"""A type into which we can safely deposit streaming data.
Parameters
----------
ds : DataShape
Returns
-------
type : type
The concrete type corresponding to the DataShape `ds`
Notes
-----
* This will return a Python type if possible
* Option types are not handled specially. The base type of the option type
is returned.
Examples
--------
>>> concrete_type('5 * int')
<class 'pandas.core.series.Series'>
>>> concrete_type('var * {name: string, amount: int}')
<class 'pandas.core.frame.DataFrame'>
>>> concrete_type('float64')
<... 'float'>
>>> concrete_type('float32')
<... 'float'>
>>> concrete_type('int64')
<... 'int'>
>>> concrete_type('int32')
<... 'int'>
>>> concrete_type('uint8')
<... 'int'>
>>> concrete_type('bool')
<... 'bool'>
>>> concrete_type('complex[float64]')
<... 'complex'>
>>> concrete_type('complex[float32]')
<... 'complex'>
>>> concrete_type('?int64')
<... 'int'>
"""
if isinstance(ds, (str, unicode)):
ds = dshape(ds)
if not iscollection(ds) and isscalar(ds.measure):
measure = getattr(ds.measure, 'ty', ds.measure)
if measure in integral.types:
return int
elif measure in floating.types:
return float
elif measure in boolean.types:
return bool
elif measure in complexes.types:
return complex
else:
return ds.measure.to_numpy_dtype().type
if not iscollection(ds):
return type(ds)
if ndim(ds) == 1:
return pd.DataFrame if isrecord(ds.measure) else pd.Series
if ndim(ds) > 1:
return np.ndarray
return list