Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jointly.calculate_magnitude returns 0 for NaN inputs #11

Closed
AlexS12 opened this issue Dec 26, 2021 · 0 comments · Fixed by #12
Closed

jointly.calculate_magnitude returns 0 for NaN inputs #11

AlexS12 opened this issue Dec 26, 2021 · 0 comments · Fixed by #12

Comments

@AlexS12
Copy link

AlexS12 commented Dec 26, 2021

  • jointly version: master
  • Python version: 3.9.7
  • Operating System: Ubuntu (inside Windows Subsystem for Linux)

Description

In the usage example, faros_df lacks acceleration data in some rows (x, y, z components are NaN). However, returned magnitude for that rows is 0.0 and I would expect NaN. Is there a reason for that?

What I Did

faros_df["Accel Mag"] = jointly.calculate_magnitude(
    faros_df, ["Accel X", "Accel Y", "Accel Z"]
)

Results in

Accel X Accel Y Accel Z ECG Accel Mag
-88.0 771.0 -531.5 -21.0 940.572831
NaN NaN NaN -10.0 0.000000
-86.0 779.0 -539.5 NaN 951.471098
NaN NaN NaN -2.0 0.000000
-82.5 781.0 -543.0 NaN 954.785971
... ... ... ... ...
NaN NaN NaN -9.0 0.000000
-166.5 896.0 -316.5 NaN 964.733383
NaN NaN NaN 12.0 0.000000
-156.5 900.0 -320.5 NaN 968.097361
NaN NaN NaN 28.0 0.000000

7400 rows × 5 columns

Possible solution

According to DataFrame.sum method documetation (https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.sum.html)

By default, the sum of an empty or all-NA Series is 0.

pd.Series([], dtype="float64").sum()  # min_count=0 is the default
0.0

This can be controlled with the min_count parameter. For example, if you’d like the sum of an empty series to be NaN, pass min_count=1.

pd.Series([], dtype="float64").sum(min_count=1)
nan

Thus, I would modify np.sqrt(np.square(data).sum(axis=1)) in calculate_magnitude by np.sqrt(np.square(data).sum(axis=1, min_count=1)) or use np.linalg.norm(data, axis=1).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant