Skip to content

Commit c1fc33b

Browse files
committed
plum working - needs clean up
1 parent c2a6572 commit c1fc33b

File tree

4 files changed

+89
-150
lines changed

4 files changed

+89
-150
lines changed

fastcore/transform.py

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
# AUTOGENERATED! DO NOT EDIT! File to edit: nbs/05_transform.ipynb (unless otherwise specified).
22

3+
4+
from __future__ import annotations
5+
6+
37
__all__ = ['Transform', 'InplaceTransform', 'DisplayedTransform', 'ItemTransform', 'get_func', 'Func', 'Sig',
48
'compose_tfms', 'mk_transform', 'gather_attrs', 'gather_attr_names', 'Pipeline']
59

610
# Cell
11+
#nbdev_comment from __future__ import annotations
712
from .imports import *
813
from .foundation import *
914
from .utils import *
@@ -13,16 +18,22 @@
1318
from plum import add_conversion_method, dispatch, Dispatcher
1419

1520
# Cell
21+
# TODO: Shouldn't sig of method set first parameter type to self? how do i get that...
22+
# i.e. self assumes type(self) / self.__class__?
1623
def _mk_plum_func(d, n, f=None, cls=None):
1724
f = (lambda x: x) if f is None else copy(f)
1825
f.__name__ = n
1926
# plum uses __qualname__ to infer f's owner
2027
f.__qualname__ = n if cls is None else '.'.join([cls.__name__,n])
28+
# TODO: Shouldn't we create a Function here and dispatch from that?
29+
# We don't need this in the dispatch table i think...
30+
# TODO: Should Function take name qualname etc and have a .from_callable method?
31+
# since the func isn't even dispatched by default
2132
pf = d(f)
2233
if cls is not None:
2334
setattr(cls,n,pf)
24-
# plum uses __set_name__ to resolve a Function's owner.
25-
# since class variable is assigned after class is created, __set_name__ must be called directly
35+
# plum uses __set_name__ to resolve a plum.Function's owner.
36+
# since we assign after class creation, __set_name__ must be called directly
2637
# source: https://docs.python.org/3/reference/datamodel.html#object.__set_name__
2738
pf.__set_name__(cls,n)
2839
return pf
@@ -33,22 +44,28 @@ def _mk_plum_func(d, n, f=None, cls=None):
3344
def _is_tfm_method(f,n):
3445
return n in _tfm_methods and callable(f)
3546

47+
# TODO: Do we still need this given the fact that plum searches mro without needing them to be Functions?
3648
class _TfmDict(dict):
3749
def __setitem__(self,k,v):
3850
if _is_tfm_method(v,k): v = dispatch(v)
3951
super().__setitem__(k,v)
4052

4153
# Cell
4254
class _TfmMeta(type):
43-
def __new__(cls, name, bases, dict):
44-
res = super().__new__(cls, name, bases, dict)
45-
res.__signature__ = inspect.signature(res.__init__)
46-
return res
47-
55+
# TODO: commenting since this breaks inspect.signature of Transform instances,
56+
# which then breaks inspect.signature of a partial of a Transform instance
57+
#def __new__(cls, name, bases, dict):
58+
# res = super().__new__(cls, name, bases, dict)
59+
# res.__signature__ = inspect.signature(res.__init__)
60+
# return res
61+
62+
# TODO: Can we move this to Transform.__init__? Then we don't reeeeeally need a metaclass anymore...?
63+
# Ohhhhh man, can we dispatch this? If called with callable, do this, and so on
4864
def __call__(cls, *args, **kwargs):
4965
f = first(args)
5066
n = getattr(f,'__name__',None)
5167
if _is_tfm_method(f,n):
68+
# use __dict__ over hasattr since it excludes parent classes
5269
if n in cls.__dict__: return getattr(cls,n).dispatch(f)
5370
return _mk_plum_func(dispatch,n,f,cls)
5471
return super().__call__(*args,**kwargs)
@@ -76,6 +93,8 @@ def __init__(self, enc=None, dec=None, split_idx=None, order=None):
7693
if not self.init_enc: return
7794

7895
self._d = Dispatcher() # TODO: do we need to hold this reference?
96+
# TODO: do u have to set the name from the original func here? for pipelines to work
97+
# TODO: I don't think this is registering any methods! Why is a func needed then???
7998
for n in _tfm_methods: setattr(self,n,_mk_plum_func(self._d,n))
8099
if enc:
81100
self.encodes.dispatch(enc)
@@ -88,7 +107,18 @@ def __init__(self, enc=None, dec=None, split_idx=None, order=None):
88107
def name(self): return getattr(self, '_name', _get_name(self))
89108
def __call__(self, x, **kwargs): return self._call('encodes', x, **kwargs)
90109
def decode (self, x, **kwargs): return self._call('decodes', x, **kwargs)
91-
def __repr__(self): return f'{self.name}:\nencodes: {self.encodes}decodes: {self.decodes}'
110+
def __repr__(self):
111+
def _pf_repr(pf):
112+
e = []
113+
for s, (f_, r) in self.encodes.methods.items():
114+
types = ','.join(str(o) for o in s.types)
115+
types = f'({types})'
116+
e.append(f'{f_.__name__}: {types} -> {r}')
117+
return e
118+
r = f'{self.name}:'
119+
r += '\n encodes:\n' + '\n'.join(' ' + o for o in _pf_repr(self.encodes))
120+
r += '\n decodes:\n' + '\n'.join(' ' + o for o in _pf_repr(self.decodes))
121+
return r
92122

93123
def setup(self, items=None, train_setup=False):
94124
train_setup = train_setup if self.train_setup is None else self.train_setup

nbs/01_basics.ipynb

Lines changed: 8 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -5327,7 +5327,7 @@
53275327
{
53285328
"data": {
53295329
"text/plain": [
5330-
"8"
5330+
"4"
53315331
]
53325332
},
53335333
"execution_count": null,
@@ -5553,24 +5553,7 @@
55535553
"cell_type": "code",
55545554
"execution_count": null,
55555555
"metadata": {},
5556-
"outputs": [
5557-
{
5558-
"data": {
5559-
"text/markdown": [
5560-
"<h4 id=\"ipython_shell\" class=\"doc_header\"><code>ipython_shell</code><a href=\"https://github.com/fastai/fastcore/tree/master/fastcore/imports.py#L70\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
5561-
"\n",
5562-
"> <code>ipython_shell</code>()\n",
5563-
"\n",
5564-
"Same as `get_ipython` but returns `False` if not in IPython"
5565-
],
5566-
"text/plain": [
5567-
"<IPython.core.display.Markdown object>"
5568-
]
5569-
},
5570-
"metadata": {},
5571-
"output_type": "display_data"
5572-
}
5573-
],
5556+
"outputs": [],
55745557
"source": [
55755558
"show_doc(ipython_shell)"
55765559
]
@@ -5579,24 +5562,7 @@
55795562
"cell_type": "code",
55805563
"execution_count": null,
55815564
"metadata": {},
5582-
"outputs": [
5583-
{
5584-
"data": {
5585-
"text/markdown": [
5586-
"<h4 id=\"in_ipython\" class=\"doc_header\"><code>in_ipython</code><a href=\"https://github.com/fastai/fastcore/tree/master/fastcore/imports.py#L75\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
5587-
"\n",
5588-
"> <code>in_ipython</code>()\n",
5589-
"\n",
5590-
"Check if code is running in some kind of IPython environment"
5591-
],
5592-
"text/plain": [
5593-
"<IPython.core.display.Markdown object>"
5594-
]
5595-
},
5596-
"metadata": {},
5597-
"output_type": "display_data"
5598-
}
5599-
],
5565+
"outputs": [],
56005566
"source": [
56015567
"show_doc(in_ipython)"
56025568
]
@@ -5605,24 +5571,7 @@
56055571
"cell_type": "code",
56065572
"execution_count": null,
56075573
"metadata": {},
5608-
"outputs": [
5609-
{
5610-
"data": {
5611-
"text/markdown": [
5612-
"<h4 id=\"in_colab\" class=\"doc_header\"><code>in_colab</code><a href=\"https://github.com/fastai/fastcore/tree/master/fastcore/imports.py#L79\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
5613-
"\n",
5614-
"> <code>in_colab</code>()\n",
5615-
"\n",
5616-
"Check if the code is running in Google Colaboratory"
5617-
],
5618-
"text/plain": [
5619-
"<IPython.core.display.Markdown object>"
5620-
]
5621-
},
5622-
"metadata": {},
5623-
"output_type": "display_data"
5624-
}
5625-
],
5574+
"outputs": [],
56265575
"source": [
56275576
"show_doc(in_colab)"
56285577
]
@@ -5631,24 +5580,7 @@
56315580
"cell_type": "code",
56325581
"execution_count": null,
56335582
"metadata": {},
5634-
"outputs": [
5635-
{
5636-
"data": {
5637-
"text/markdown": [
5638-
"<h4 id=\"in_jupyter\" class=\"doc_header\"><code>in_jupyter</code><a href=\"https://github.com/fastai/fastcore/tree/master/fastcore/imports.py#L83\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
5639-
"\n",
5640-
"> <code>in_jupyter</code>()\n",
5641-
"\n",
5642-
"Check if the code is running in a jupyter notebook"
5643-
],
5644-
"text/plain": [
5645-
"<IPython.core.display.Markdown object>"
5646-
]
5647-
},
5648-
"metadata": {},
5649-
"output_type": "display_data"
5650-
}
5651-
],
5583+
"outputs": [],
56525584
"source": [
56535585
"show_doc(in_jupyter)"
56545586
]
@@ -5657,24 +5589,7 @@
56575589
"cell_type": "code",
56585590
"execution_count": null,
56595591
"metadata": {},
5660-
"outputs": [
5661-
{
5662-
"data": {
5663-
"text/markdown": [
5664-
"<h4 id=\"in_notebook\" class=\"doc_header\"><code>in_notebook</code><a href=\"https://github.com/fastai/fastcore/tree/master/fastcore/imports.py#L88\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
5665-
"\n",
5666-
"> <code>in_notebook</code>()\n",
5667-
"\n",
5668-
"Check if the code is running in a jupyter notebook"
5669-
],
5670-
"text/plain": [
5671-
"<IPython.core.display.Markdown object>"
5672-
]
5673-
},
5674-
"metadata": {},
5675-
"output_type": "display_data"
5676-
}
5677-
],
5592+
"outputs": [],
56785593
"source": [
56795594
"show_doc(in_notebook)"
56805595
]
@@ -5690,18 +5605,7 @@
56905605
"cell_type": "code",
56915606
"execution_count": null,
56925607
"metadata": {},
5693-
"outputs": [
5694-
{
5695-
"data": {
5696-
"text/plain": [
5697-
"(True, True, False, True)"
5698-
]
5699-
},
5700-
"execution_count": null,
5701-
"metadata": {},
5702-
"output_type": "execute_result"
5703-
}
5704-
],
5608+
"outputs": [],
57055609
"source": [
57065610
"IN_IPYTHON, IN_JUPYTER, IN_COLAB, IN_NOTEBOOK"
57075611
]
@@ -5717,27 +5621,7 @@
57175621
"cell_type": "code",
57185622
"execution_count": null,
57195623
"metadata": {},
5720-
"outputs": [
5721-
{
5722-
"name": "stdout",
5723-
"output_type": "stream",
5724-
"text": [
5725-
"Converted 00_test.ipynb.\n",
5726-
"Converted 01_basics.ipynb.\n",
5727-
"Converted 02_foundation.ipynb.\n",
5728-
"Converted 03_xtras.ipynb.\n",
5729-
"Converted 03a_parallel.ipynb.\n",
5730-
"Converted 03b_net.ipynb.\n",
5731-
"Converted 04_dispatch.ipynb.\n",
5732-
"Converted 05_transform.ipynb.\n",
5733-
"Converted 06_docments.ipynb.\n",
5734-
"Converted 07_meta.ipynb.\n",
5735-
"Converted 08_script.ipynb.\n",
5736-
"Converted index.ipynb.\n",
5737-
"Converted parallel_win.ipynb.\n"
5738-
]
5739-
}
5740-
],
5624+
"outputs": [],
57415625
"source": [
57425626
"#hide\n",
57435627
"from nbdev.export import notebook2script\n",

0 commit comments

Comments
 (0)