soft-matter/trackpy

tp.emsd TypeError: mean() got an unexpected keyword argument 'level'

Closed this issue · 7 comments

Trying to use the emsd function.

em = tp.emsd(tm, microns_per_pixel, frames_per_second)

But got following error, I'm pretty new Python to and don't know where start to fix this error. Any advice appreciated!


TypeError Traceback (most recent call last)
Cell In[122], line 1
----> 1 em = tp.emsd(tm, microns_per_pixel, frames_per_second) # microns per pixel = 100/285., frames per second = 24
2 fig, ax = plt.subplots()
3 ax.plot(em.index, em, 'o')

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\trackpy\motion.py:235, in emsd(traj, mpp, fps, max_lagtime, detail, pos_columns)
233 ids.append(pid)
234 msds = pandas_concat(msds, keys=ids, names=['particle', 'frame'])
--> 235 results = msds.mul(msds['N'], axis=0).mean(level=1) # weighted average
236 results = results.div(msds['N'].mean(level=1), axis=0) # weights normalized
237 # Above, lagt is lumped in with the rest for simplicity and speed.
238 # Here, rebuild it from the frame index.

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\core\generic.py:11556, in NDFrame._add_numeric_operations..mean(self, axis, skipna, numeric_only, **kwargs)
11539 @doc(
11540 _num_doc,
11541 desc="Return the mean of the values over the requested axis.",
(...)
11554 **kwargs,
11555 ):

11556 return NDFrame.mean(self, axis, skipna, numeric_only, **kwargs)

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\core\generic.py:11201, in NDFrame.mean(self, axis, skipna, numeric_only, **kwargs)
11194 def mean(
11195 self,
11196 axis: Axis | None = 0,
(...)
11199 **kwargs,
11200 ) -> Series | float:

11201 return self._stat_function(
11202 "mean", nanops.nanmean, axis, skipna, numeric_only, **kwargs
11203 )

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\core\generic.py:11154, in NDFrame._stat_function(self, name, func, axis, skipna, numeric_only, **kwargs)
11152 nv.validate_median((), kwargs)
11153 else:

11154 nv.validate_stat_func((), kwargs, fname=name)
11156 validate_bool_kwarg(skipna, "skipna", none_allowed=False)
11158 return self._reduce(
11159 func, name=name, axis=axis, skipna=skipna, numeric_only=numeric_only
11160 )

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\compat\numpy\function.py:80, in CompatValidator.call(self, args, kwargs, fname, max_fname_arg_count, method)
78 validate_args(fname, args, max_fname_arg_count, self.defaults)
79 elif method == "kwargs":
---> 80 validate_kwargs(fname, kwargs, self.defaults)
81 elif method == "both":
82 validate_args_and_kwargs(
83 fname, args, kwargs, max_fname_arg_count, self.defaults
84 )

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\util_validators.py:162, in validate_kwargs(fname, kwargs, compat_args)
140 """
141 Checks whether parameters passed to the **kwargs argument in a
142 function fname are valid parameters as specified in *compat_args
(...)
159 map to the default values specified in compat_args
160 """
161 kwds = kwargs.copy()
--> 162 _check_for_invalid_keys(fname, kwargs, compat_args)
163 _check_for_default_values(fname, kwds, compat_args)

File ~\AppData\Local\Programs\Python\Python311\Lib\site-packages\pandas\util_validators.py:136, in _check_for_invalid_keys(fname, kwargs, compat_args)
134 if diff:
135 bad_arg = list(diff)[0]
--> 136 raise TypeError(f"{fname}() got an unexpected keyword argument '{bad_arg}'")

TypeError: mean() got an unexpected keyword argument 'level'

Seeing this : 235 results = msds.mul(msds['N'], axis=0).mean(level=1) # weighted average and this : TypeError: mean() got an unexpected keyword argument 'level', it seems like a deprecation issue.

From the pandas documentation :

Deprecated since version 1.3.0: The level keyword is deprecated. Use groupby instead.

Which version of pandas is compatible with trackpy 0.6.0rc1?

nkeim commented

Thanks for reporting this! Pandas 1.3 is the most recent version that I'm certain trackpy works with.

This needs a pull request, or a working version of that line of code.

These are the only changes needed, both in motion.py:
Line 235: results = msds.mul(msds['N'], axis=0).groupby(level=1).mean() # weighted average
Line 236: results = results.div(msds['N'].groupby(level=1).mean(), axis=0) # weights normalized

I changed them in my own copy of motion.py within my virtual env and it works fine as expected.
Thanks for this great software :)

nkeim commented

That's great! Thank you!!

I think PR #735 might be related too.

These are the only changes needed, both in motion.py: Line 235: results = msds.mul(msds['N'], axis=0).groupby(level=1).mean() # weighted average Line 236: results = results.div(msds['N'].groupby(level=1).mean(), axis=0) # weights normalized

I changed them in my own copy of motion.py within my virtual env and it works fine as expected. Thanks for this great software :)

I have submitted a pull request (#742) with this edit.
(In the meantime, anyone wishing to access the updated version may use my fork: https://github.com/horowitz-lab/trackpy/tree/fixpandasmean2023