Trying to solve this syntax issue: TypeError: __init__() takes 1 positional argument but 2 were given

Ok, so I’m building a new PILImage class that supports RAW images, so it’s named RawpyImage (because unlike PIL, Rawpy does support RAW files).

Here I’m trying to load the data into the dataset.

x_tfms=[RawpyImage.create]
y_tfms=[my_get_y, RawpyImage.create]
tfms = [x_tfms,y_tfms]
items =  get_image_files(path=path_short)
splits = RandomSplitter(seed=2022)(items)
dsets = Datasets(items=items, tfms=tfms, splits=splits)

Output:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-51-9dd4852e11c2> in <module>()
      4 items =  get_image_files(path=path_short)
      5 splits = RandomSplitter(seed=2022)(items)
----> 6 dsets = Datasets(items=items, tfms=tfms, splits=splits)

10 frames
/usr/local/lib/python3.7/dist-packages/fastai/data/core.py in __init__(self, items, tfms, tls, n_inp, dl_type, **kwargs)
    327     def __init__(self, items=None, tfms=None, tls=None, n_inp=None, dl_type=None, **kwargs):
    328         super().__init__(dl_type=dl_type)
--> 329         self.tls = L(tls if tls else [TfmdLists(items, t, **kwargs) for t in L(ifnone(tfms,[None]))])
    330         self.n_inp = ifnone(n_inp, max(1, len(self.tls)-1))
    331 

/usr/local/lib/python3.7/dist-packages/fastai/data/core.py in <listcomp>(.0)
    327     def __init__(self, items=None, tfms=None, tls=None, n_inp=None, dl_type=None, **kwargs):
    328         super().__init__(dl_type=dl_type)
--> 329         self.tls = L(tls if tls else [TfmdLists(items, t, **kwargs) for t in L(ifnone(tfms,[None]))])
    330         self.n_inp = ifnone(n_inp, max(1, len(self.tls)-1))
    331 

/usr/local/lib/python3.7/dist-packages/fastcore/foundation.py in __call__(cls, x, *args, **kwargs)
     95     def __call__(cls, x=None, *args, **kwargs):
     96         if not args and not kwargs and x is not None and isinstance(x,cls): return x
---> 97         return super().__call__(x, *args, **kwargs)
     98 
     99 # Cell

/usr/local/lib/python3.7/dist-packages/fastai/data/core.py in __init__(self, items, tfms, use_list, do_setup, split_idx, train_setup, splits, types, verbose, dl_type)
    253         if do_setup:
    254             pv(f"Setting up {self.tfms}", verbose)
--> 255             self.setup(train_setup=train_setup)
    256 
    257     def _new(self, items, split_idx=None, **kwargs):

/usr/local/lib/python3.7/dist-packages/fastai/data/core.py in setup(self, train_setup)
    275             for f in self.tfms.fs:
    276                 self.types.append(getattr(f, 'input_types', type(x)))
--> 277                 x = f(x)
    278             self.types.append(type(x))
    279         types = L(t if is_listy(t) else [t] for t in self.types).concat().unique()

/usr/local/lib/python3.7/dist-packages/fastcore/transform.py in __call__(self, x, **kwargs)
     71     @property
     72     def name(self): return getattr(self, '_name', _get_name(self))
---> 73     def __call__(self, x, **kwargs): return self._call('encodes', x, **kwargs)
     74     def decode  (self, x, **kwargs): return self._call('decodes', x, **kwargs)
     75     def __repr__(self): return f'{self.name}:\nencodes: {self.encodes}decodes: {self.decodes}'

/usr/local/lib/python3.7/dist-packages/fastcore/transform.py in _call(self, fn, x, split_idx, **kwargs)
     81     def _call(self, fn, x, split_idx=None, **kwargs):
     82         if split_idx!=self.split_idx and self.split_idx is not None: return x
---> 83         return self._do_call(getattr(self, fn), x, **kwargs)
     84 
     85     def _do_call(self, f, x, **kwargs):

/usr/local/lib/python3.7/dist-packages/fastcore/transform.py in _do_call(self, f, x, **kwargs)
     87             if f is None: return x
     88             ret = f.returns(x) if hasattr(f,'returns') else None
---> 89             return retain_type(f(x, **kwargs), x, ret)
     90         res = tuple(self._do_call(f, x_, **kwargs) for x_ in x)
     91         return retain_type(res, x)

/usr/local/lib/python3.7/dist-packages/fastcore/dispatch.py in __call__(self, *args, **kwargs)
    121         elif self.inst is not None: f = MethodType(f, self.inst)
    122         elif self.owner is not None: f = MethodType(f, self.owner)
--> 123         return f(*args, **kwargs)
    124 
    125     def __get__(self, inst, owner):

<ipython-input-50-d51256b01dcc> in create(cls, fn)
     36     ##@staticmethod
     37     def create(cls,fn)->None:
---> 38         return cls(load_raw_image(fn))
     39 
     40     def __repr__(self): return f'{self.__class__.__name__} mode={self.mode} size={"x".join([str(d) for d in self.size])}'

/usr/local/lib/python3.7/dist-packages/fastcore/meta.py in __call__(cls, x, *args, **kwargs)
     60         if hasattr(cls, '_new_meta'): x = cls._new_meta(x, *args, **kwargs)
     61         elif not isinstance(x,getattr(cls,'_bypass_type',object)) or len(args) or len(kwargs):
---> 62             x = super().__call__(*((x,)+args), **kwargs)
     63         if cls!=x.__class__: x.__class__ = cls
     64         return x

TypeError: __init__() takes 1 positional argument but 2 were given

Here’s my RawpyImage:

def load_raw_image(fn):
    fn=str(fn)
    with rawpy.imread(fn) as RAWobj:
        return RAWobj.postprocess().copy()

class RawpyImage(Image.Image, metaclass=BypassNewMeta): #, metaclass=BypassNewMeta
    "This is to copy class PILBase+PILImage"
    _bypass_type=Image.Image
    _show_args = {'cmap':'viridis'}
    _open_args = {'mode': 'RGB'}
    @classmethod 
    ##@staticmethod
    def create(cls,fn)->None:
        return cls(load_raw_image(fn))

        ...

Now I’m trying to understand why this error shows up:

TypeError: __init__() takes 1 positional argument but 2 were given

Please help me get over this syntax issue…
Thanks :slight_smile:

Not sure exactly why that leads to the TypeError you’re getting, but

splits = RandomSplitter(seed=2022)(items)

Should be splits = RandomSplitter(seed=2022, items)

Hey! Actually it should be as it was before. There is a whole concept of something called @Transform, that actually lets you use functions as arguments on some variables.

So here, RandomSplitter is a function that you pass in seed=2022, but it is also a transform that you apply on items.

You can read more about this here:

1 Like