sollan / alma

Fully automated (rodent) limb motion analysis toolbox for behavioral analysis with bodypart coordinate data, building upon markerless pose estimation.
GNU General Public License v3.0
13 stars 5 forks source link

KeyError: 'toe x' when importing csv in Treadmill #23

Closed arnaumarin closed 1 year ago

arnaumarin commented 1 year ago

Hi, I recorded and analyzed some data with deeplabcut from a mouse and I imported the CSV with the different markers (toe, mtp, ankle, knee, hip and iliac crest). Once I import the data into the Treadmill option and select Semi-automated, I get the following error:

Traceback (most recent call last):
  File "C:\Users\Name\anaconda3\envs\alma\lib\site-packages\pandas\core\indexes\base.py", line 3803, in get_loc
    return self._engine.get_loc(casted_key)
  File "pandas\_libs\index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 165, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 5745, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 5753, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'toe x'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "C:\Users\Name\Desktop\alma\Panels\AnalyzeStride.py", line 449, in UpdateSpeed
    self.EstimateParams(self)
  File "C:\Users\Name\Desktop\alma\Panels\AnalyzeStride.py", line 481, in EstimateParams
    self.est_cm_speed, self.est_px_speed, self.est_pixels_per_cm, self.est_px_to_cm_speed_ratio = KinematicsFunctions.estimate_speed(self.df, 'toe', self.cm_speed, None, self.frame_rate, self.right_to_left)
  File "C:\Users\Name\Desktop\alma\Functions\KinematicsFunctions.py", line 59, in estimate_speed
    x_change = np.diff(pd_dataframe[f'{bodypart} x'][pd_dataframe[f'{bodypart} likelihood']>0.5])
  File "C:\Users\Name\anaconda3\envs\alma\lib\site-packages\pandas\core\frame.py", line 3804, in __getitem__
    indexer = self.columns.get_loc(key)
  File "C:\Users\Name\anaconda3\envs\alma\lib\site-packages\pandas\core\indexes\base.py", line 3805, in get_loc
    raise KeyError(key) from err
KeyError: 'toe x'

The goal is to extract the metrics of the gait. Any help will be greately appreciate it. Thanks,

sollan commented 1 year ago

Hi! Sounds like your DLC results file doesn't contain the column "toe x" (case sensitive). Could you please double check the marker names in the csv? If the marker is named "toe", the results should be in the correct format already.

Also, for the (semi-)automated analysis, it is important to update the config.yaml with your own recording frame rate. Please make sure you've done that. Even though that doesn't seem linked to the error directly, it would cause problems with the estimations.

Best, Annette

sollan commented 1 year ago

Alright, makes sense. For now, the easiest way would be to just rename all markers to all lower case (toe, mtp, iliac crest, ...) and get some preliminary results.

You also have other options:

  1. update the marker names in DLC
  2. write a script to modify the csv headers to lower case, after generating DLC output csv files with upper case
  3. (most appreciated!) help us refactor the code to accept user-defined marker names :)
arnaumarin commented 1 year ago

Yes, Seems like my names were in capital letters:

image

Now, seems like I find the following error:

Too much dragging? Please check raw data.
Median of selected bodypart x coordinate change per frame: 0.0
Calculating a subset of parameters independent of step cycles...
Traceback (most recent call last):
  File "C:\Users\Pilar\Desktop\alma\Panels\AnalyzeStride.py", line 694, in ExtractParameters
    parameters, pd_dataframe_coords, is_stance, bodyparts = KinematicsFunctions.extract_parameters(self.frame_rate, self.df, self.cutoff_f, 'toe', 
TypeError: cannot unpack non-iterable NoneType object

How does the app detect if the mouse draggs a lot? Thanks! (this video is certainly in bad conditions, I just wanted to make sure that I can make the pipeline work).

sollan commented 1 year ago

Hello again! To summarize, we assume that in non-dragging mice, the x-coordinate of the limb endpoint (toe, in this case) should be moving at a non-constant speed (acceleration at the onset of swing phase and remaining still during stance). If instead there is significant dragging involved, for example if the movement of the hindlimb depends mostly on forelimbs, the endpoint of the "dragging" limb would seem to be moving at an almost constant speed. We check for this in the distribution of the acceleration of the toe and see if it's clustered around zero (indicating too much dragging for any meaningful analysis of gait, since we have no way to determine swing/stance cycle).

In order to test the pipeline, an adequate DLC model is necessary (I'm not sure how bad your video conditions are, but it doesn't have to be perfect at this stage), as well as sufficient datapoints (which depends on the duration of meaningful recording, video quality - because of the "likelihood" estimation of DLC that we use to filter out bad data, as well as frame rate of recording), because some parameters have to be estimated based on the distribution of data, which only works when we have enough datapoints.

Please also check out our paper or the wiki page if you're curious about the implementation details, and feel free to ask if anything's unclear. I realized it is not linked to the repo and will add it to the readme file.

Hope this helps!

Best, Annette

arnaumarin commented 1 year ago

Hi again, thanks for your updates. I did a good quality recording with a mouse walking in a straight line (about 5 steps). I tried using the treadmill option (the mouse is walking left to right, so I updated the .yaml) but I get the following error:

Too much dragging? Please check raw data.
Median of selected bodypart x coordinate change per frame: 0.0
Calculating a subset of parameters independent of step cycles...
Traceback (most recent call last):
  File "C:\Users\Pilar\Desktop\alma\Panels\AnalyzeStride.py", line 694, in ExtractParameters
    parameters, pd_dataframe_coords, is_stance, bodyparts = KinematicsFunctions.extract_parameters(self.frame_rate, self.df, self.cutoff_f, 'toe', 
TypeError: cannot unpack non-iterable NoneType object

Any idea what would it mean? I am afraid that the video is too short (it is only 5 steps straight in 7 seconds). Is it possible that we might need longer recording?

Also, given that it is not strictly walking on a treadmill (the mouse is walking right to left in a free space, but limited to only go straight line) I tested on the Spontaneous Walking option. What does the first vector mean? ([48,210,353,506,...] in the following case) I think in this case it detected that there are 5 steps in the video, but with the following error next:

[48, 210, 353, 506, 651, 801]
0    1.3416666666666666
1    1.1833333333333333
2    1.2666666666666666
3                   1.2
4    1.2416666666666667
5                   nan
Name: cycle duration (s), dtype: object
Traceback (most recent call last):
  File "C:\Users\Name\Desktop\alma\Panels\AnalyzeStride.py", line 707, in ExtractParameters
    KinematicsFunctions.make_parameters_output(os.path.join(os.path.dirname(self.output_path), f'continuous_strides_parameters_{self.filename}.csv'), parameters_truncated)
  File "C:\Users\Name\Desktop\alma\Functions\KinematicsFunctions.py", line 201, in make_parameters_output
    parameters.to_csv(pathname)
  File "C:\Users\Name\anaconda3\envs\alma\lib\site-packages\pandas\util\_decorators.py", line 211, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\Name\anaconda3\envs\alma\lib\site-packages\pandas\core\generic.py", line 3720, in to_csv
    return DataFrameRenderer(formatter).to_csv(
  File "C:\Users\Name\anaconda3\envs\alma\lib\site-packages\pandas\util\_decorators.py", line 211, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\Name\anaconda3\envs\alma\lib\site-packages\pandas\io\formats\format.py", line 1189, in to_csv
    csv_formatter.save()
  File "C:\Users\Name\anaconda3\envs\alma\lib\site-packages\pandas\io\formats\csvs.py", line 241, in save
    with get_handle(
  File "C:\Users\Name\anaconda3\envs\alma\lib\site-packages\pandas\io\common.py", line 734, in get_handle
    check_parent_directory(str(handle))
  File "C:\Users\Name\anaconda3\envs\alma\lib\site-packages\pandas\io\common.py", line 596, in check_parent_directory
    if not parent.is_dir():
  File "C:\Users\Name\anaconda3\envs\alma\lib\pathlib.py", line 1403, in is_dir
    return S_ISDIR(self.stat().st_mode)
  File "C:\Users\Name\anaconda3\envs\alma\lib\pathlib.py", line 1194, in stat
    return self._accessor.stat(self)
OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: 'C:\\Users\\Name\\Downloads\\continuous_strides_parameters_C:\\Users\\Name\\Downloads'

Thanks!

sollan commented 1 year ago

Hi! Indeed the recording might be too short. (On another note, I hope your DLC model was trained with sufficient data, or the coordinate output could already be problematic.)

Yes, since you didn’t use a treadmill set-up, it was right to use the “spontaneous walking” option. Now the problem seems to be the output path - C:\\Users\\Name\\Downloads\\continuous_strides_parameters_C:\\Users\\Name\\Downloads which contains a concatenation of two separate paths to “Downloads” folder (the prefix “continuous_strides_parameters” is generated by the program).

In order to save the output, you should see a prompt to select the output folder and to specify your own filename (since you are analyzing one file only at this point.) There should also be some text appearing on the interface (“Saving output to <your/output/path>”.) Could you please double check and let us know the exact sequence of actions you took, if the error persists?

sollan commented 1 year ago

Closing this due to inactivity. If you still need support, please open a new issue or contact us using the correspondence info in the publication.