@@ -52,7 +52,7 @@ def __setitem__(self, k, v):
52
52
if k not in self : super ().__setitem__ (k , Function (v ))
53
53
self [k ].dispatch (v )
54
54
55
- # %% ../nbs/01_transform.ipynb 14
55
+ # %% ../nbs/01_transform.ipynb 13
56
56
class _TfmMeta (type ):
57
57
@classmethod
58
58
def __prepare__ (cls , name , bases ): return _TfmDict ()
@@ -82,7 +82,7 @@ def __new__(cls, name, bases, namespace):
82
82
new_cls .__signature__ = inspect .signature (new_cls .__init__ )
83
83
return new_cls
84
84
85
- # %% ../nbs/01_transform.ipynb 15
85
+ # %% ../nbs/01_transform.ipynb 14
86
86
class Transform (metaclass = _TfmMeta ):
87
87
"Delegates (`__call__`,`decode`,`setup`) to (<code>encodes</code>,<code>decodes</code>,<code>setups</code>) if `split_idx` matches"
88
88
split_idx ,init_enc ,order ,train_setup = None ,None ,0 ,None
@@ -137,21 +137,21 @@ def _do_call(self, nm, *args, **kwargs):
137
137
138
138
add_docs (Transform , decode = "Delegate to decodes to undo transform" , setup = "Delegate to setups to set up transform" )
139
139
140
- # %% ../nbs/01_transform.ipynb 157
140
+ # %% ../nbs/01_transform.ipynb 156
141
141
class InplaceTransform (Transform ):
142
142
"A `Transform` that modifies in-place and just returns whatever it's passed"
143
143
def _call (self , fn , * args , split_idx = None , ** kwargs ):
144
144
super ()._call (fn ,* args , split_idx = split_idx , ** kwargs )
145
145
return args [0 ]
146
146
147
- # %% ../nbs/01_transform.ipynb 161
147
+ # %% ../nbs/01_transform.ipynb 160
148
148
class DisplayedTransform (Transform ):
149
149
"A transform with a `__repr__` that shows its attrs"
150
150
151
151
@property
152
152
def name (self ): return f"{ super ().name } -- { getattr (self ,'__stored_args__' ,{})} \n "
153
153
154
- # %% ../nbs/01_transform.ipynb 167
154
+ # %% ../nbs/01_transform.ipynb 166
155
155
class ItemTransform (Transform ):
156
156
"A transform that always take tuples as items"
157
157
_retain = True
@@ -165,21 +165,21 @@ def _call1(self, x, name, **kwargs):
165
165
return retain_type (y , x , Any )
166
166
167
167
168
- # %% ../nbs/01_transform.ipynb 176
168
+ # %% ../nbs/01_transform.ipynb 175
169
169
def get_func (t , name , * args , ** kwargs ):
170
170
"Get the `t.name` (potentially partial-ized with `args` and `kwargs`) or `noop` if not defined"
171
171
f = nested_callable (t , name )
172
172
return f if not (args or kwargs ) else partial (f , * args , ** kwargs )
173
173
174
- # %% ../nbs/01_transform.ipynb 180
174
+ # %% ../nbs/01_transform.ipynb 179
175
175
class Func ():
176
176
"Basic wrapper around a `name` with `args` and `kwargs` to call on a given type"
177
177
def __init__ (self , name , * args , ** kwargs ): self .name ,self .args ,self .kwargs = name ,args ,kwargs
178
178
def __repr__ (self ): return f'sig: { self .name } ({ self .args } , { self .kwargs } )'
179
179
def _get (self , t ): return get_func (t , self .name , * self .args , ** self .kwargs )
180
180
def __call__ (self ,t ): return mapped (self ._get , t )
181
181
182
- # %% ../nbs/01_transform.ipynb 183
182
+ # %% ../nbs/01_transform.ipynb 182
183
183
class _Sig ():
184
184
def __getattr__ (self ,k ):
185
185
def _inner (* args , ** kwargs ): return Func (k , * args , ** kwargs )
@@ -188,7 +188,7 @@ def _inner(*args, **kwargs): return Func(k, *args, **kwargs)
188
188
Sig = _Sig ()
189
189
190
190
191
- # %% ../nbs/01_transform.ipynb 189
191
+ # %% ../nbs/01_transform.ipynb 188
192
192
def compose_tfms (x , tfms , is_enc = True , reverse = False , ** kwargs ):
193
193
"Apply all `func_nm` attribute of `tfms` on `x`, maybe in `reverse` order"
194
194
if reverse : tfms = reversed (tfms )
@@ -198,13 +198,13 @@ def compose_tfms(x, tfms, is_enc=True, reverse=False, **kwargs):
198
198
return x
199
199
200
200
201
- # %% ../nbs/01_transform.ipynb 194
201
+ # %% ../nbs/01_transform.ipynb 193
202
202
def mk_transform (f ):
203
203
"Convert function `f` to `Transform` if it isn't already one"
204
204
f = instantiate (f )
205
205
return f if isinstance (f ,(Transform ,Pipeline )) else Transform (f )
206
206
207
- # %% ../nbs/01_transform.ipynb 195
207
+ # %% ../nbs/01_transform.ipynb 194
208
208
def gather_attrs (o , k , nm ):
209
209
"Used in __getattr__ to collect all attrs `k` from `self.{nm}`"
210
210
if k .startswith ('_' ) or k == nm : raise AttributeError (k )
@@ -213,12 +213,12 @@ def gather_attrs(o, k, nm):
213
213
if not res : raise AttributeError (k )
214
214
return res [0 ] if len (res )== 1 else L (res )
215
215
216
- # %% ../nbs/01_transform.ipynb 196
216
+ # %% ../nbs/01_transform.ipynb 195
217
217
def gather_attr_names (o , nm ):
218
218
"Used in __dir__ to collect all attrs `k` from `self.{nm}`"
219
219
return L (getattr (o ,nm )).map (dir ).concat ().unique ()
220
220
221
- # %% ../nbs/01_transform.ipynb 197
221
+ # %% ../nbs/01_transform.ipynb 196
222
222
class Pipeline :
223
223
"A pipeline of composed (for encode/decode) transforms, setup with types"
224
224
def __init__ (self , funcs = None , split_idx = None ):
0 commit comments