|
9 | 9 | # Imports |
10 | 10 | # ------- |
11 | 11 | from movement import sample_data |
| 12 | +from movement.analysis.kinematics import compute_velocity |
| 13 | +from movement.filtering import filter_by_confidence, interpolate_over_time |
12 | 14 |
|
13 | 15 | # %% |
14 | 16 | # Load a sample dataset |
|
73 | 75 | # %% |
74 | 76 | # Filter out points with low confidence |
75 | 77 | # ------------------------------------- |
76 | | -# Using the |
77 | | -# :meth:`filter_by_confidence()\ |
78 | | -# <movement.move_accessor.MovementDataset.filtering_wrapper>` |
79 | | -# method of the ``move`` accessor, |
80 | | -# we can filter out points with confidence scores below a certain threshold. |
81 | | -# The default ``threshold=0.6`` will be used when ``threshold`` is not |
82 | | -# provided. |
83 | | -# This method will also report the number of NaN values in the dataset before |
84 | | -# and after the filtering operation by default (``print_report=True``). |
| 78 | +# Using the :func:`movement.filtering.filter_by_confidence` function from the |
| 79 | +# :mod:`movement.filtering` module, we can filter out points with confidence |
| 80 | +# scores below a certain threshold. This function takes ``position`` and |
| 81 | +# ``confidence`` as required arguments, and accepts an optional ``threshold`` |
| 82 | +# parameter, which defaults to ``threshold=0.6`` unless specified otherwise. |
| 83 | +# The function will also report the number of NaN values in the dataset before |
| 84 | +# and after the filtering operation by default, but you can disable this |
| 85 | +# by passing ``print_report=False``. |
| 86 | +# |
85 | 87 | # We will use :meth:`xarray.Dataset.update` to update ``ds`` in-place |
86 | 88 | # with the filtered ``position``. |
87 | 89 |
|
88 | | -ds.update({"position": ds.move.filter_by_confidence()}) |
89 | | - |
90 | | -# %% |
91 | | -# .. note:: |
92 | | -# The ``move`` accessor :meth:`filter_by_confidence()\ |
93 | | -# <movement.move_accessor.MovementDataset.filtering_wrapper>` |
94 | | -# method is a convenience method that applies |
95 | | -# :func:`movement.filtering.filter_by_confidence`, |
96 | | -# which takes ``position`` and ``confidence`` as arguments. |
97 | | -# The equivalent function call using the |
98 | | -# :mod:`movement.filtering` module would be: |
99 | | -# |
100 | | -# .. code-block:: python |
101 | | -# |
102 | | -# from movement.filtering import filter_by_confidence |
103 | | -# |
104 | | -# ds.update({"position": filter_by_confidence(position, confidence)}) |
| 90 | +ds.update({"position": filter_by_confidence(ds.position, ds.confidence)}) |
105 | 91 |
|
106 | 92 | # %% |
107 | 93 | # We can see that the filtering operation has introduced NaN values in the |
|
120 | 106 | # %% |
121 | 107 | # Interpolate over missing values |
122 | 108 | # ------------------------------- |
123 | | -# Using the |
124 | | -# :meth:`interpolate_over_time()\ |
125 | | -# <movement.move_accessor.MovementDataset.filtering_wrapper>` |
126 | | -# method of the ``move`` accessor, |
127 | | -# we can interpolate over the gaps we've introduced in the pose tracks. |
| 109 | +# Using the :func:`movement.filtering.interpolate_over_time` function from the |
| 110 | +# :mod:`movement.filtering` module, we can interpolate over gaps |
| 111 | +# we've introduced in the pose tracks. |
128 | 112 | # Here we use the default linear interpolation method (``method=linear``) |
129 | 113 | # and interpolate over gaps of 40 frames or less (``max_gap=40``). |
130 | 114 | # The default ``max_gap=None`` would interpolate over all gaps, regardless of |
131 | 115 | # their length, but this should be used with caution as it can introduce |
132 | 116 | # spurious data. The ``print_report`` argument acts as described above. |
133 | 117 |
|
134 | | -ds.update({"position": ds.move.interpolate_over_time(max_gap=40)}) |
135 | | - |
136 | | -# %% |
137 | | -# .. note:: |
138 | | -# The ``move`` accessor :meth:`interpolate_over_time()\ |
139 | | -# <movement.move_accessor.MovementDataset.filtering_wrapper>` |
140 | | -# is also a convenience method that applies |
141 | | -# :func:`movement.filtering.interpolate_over_time` |
142 | | -# to the ``position`` data variable. |
143 | | -# The equivalent function call using the |
144 | | -# :mod:`movement.filtering` module would be: |
145 | | -# |
146 | | -# .. code-block:: python |
147 | | -# |
148 | | -# from movement.filtering import interpolate_over_time |
149 | | -# |
150 | | -# ds.update({"position": interpolate_over_time( |
151 | | -# position_filtered, max_gap=40 |
152 | | -# )}) |
| 118 | +ds.update({"position": interpolate_over_time(ds.position, max_gap=40)}) |
153 | 119 |
|
154 | 120 | # %% |
155 | 121 | # We see that all NaN values have disappeared, meaning that all gaps were |
|
176 | 142 | # %% |
177 | 143 | # Filtering multiple data variables |
178 | 144 | # --------------------------------- |
179 | | -# All :mod:`movement.filtering` functions are available via the |
180 | | -# ``move`` accessor. These ``move`` accessor methods operate on the |
181 | | -# ``position`` data variable in the dataset ``ds`` by default. |
182 | | -# There is also an additional argument ``data_vars`` that allows us to |
183 | | -# specify which data variables in ``ds`` to filter. |
184 | | -# When multiple data variable names are specified in ``data_vars``, |
185 | | -# the method will return a dictionary with the data variable names as keys |
186 | | -# and the filtered DataArrays as values, otherwise it will return a single |
187 | | -# DataArray that is the filtered data. |
188 | | -# This is useful when we want to apply the same filtering operation to |
| 145 | +# We can also apply the same filtering operation to |
189 | 146 | # multiple data variables in ``ds`` at the same time. |
190 | 147 | # |
191 | 148 | # For instance, to filter both ``position`` and ``velocity`` data variables |
192 | | -# in ``ds``, based on the confidence scores, we can specify |
193 | | -# ``data_vars=["position", "velocity"]`` in the method call. |
194 | | -# As the filtered data variables are returned as a dictionary, we can once |
195 | | -# again use :meth:`xarray.Dataset.update` to update ``ds`` in-place |
| 149 | +# in ``ds``, based on the confidence scores, we can specify a dictionary |
| 150 | +# with the data variable names as keys and the corresponding filtered |
| 151 | +# DataArrays as values. Then we can once again use |
| 152 | +# :meth:`xarray.Dataset.update` to update ``ds`` in-place |
196 | 153 | # with the filtered data variables. |
197 | 154 |
|
198 | | -ds["velocity"] = ds.move.compute_velocity() |
199 | | -filtered_data_dict = ds.move.filter_by_confidence( |
200 | | - data_vars=["position", "velocity"] |
201 | | -) |
202 | | -ds.update(filtered_data_dict) |
| 155 | +# Add velocity data variable to the dataset |
| 156 | +ds["velocity"] = compute_velocity(ds.position) |
| 157 | + |
| 158 | +# Create a dictionary mapping data variable names to filtered DataArrays |
| 159 | +# We disable report printing for brevity |
| 160 | +update_dict = { |
| 161 | + var: filter_by_confidence(ds[var], ds.confidence, print_report=False) |
| 162 | + for var in ["position", "velocity"] |
| 163 | +} |
| 164 | + |
| 165 | +# Use the dictionary to update the dataset in-place |
| 166 | +ds.update(update_dict) |
0 commit comments