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

refactor(read_device): skip return calibration coefficients by default #45

Merged
merged 2 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 21 additions & 16 deletions src/actipy/processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def detect_nonwear(data, patience='90m', window='10s', stdtol=15 / 1000):
return data, info


def calibrate_gravity(data, calib_cube=0.3, calib_min_samples=50, window='10s', stdtol=15 / 1000, chunksize=1_000_000): # noqa: C901
def calibrate_gravity(data, calib_cube=0.3, calib_min_samples=50, window='10s', stdtol=15 / 1000, return_coeffs=True, chunksize=1_000_000): # noqa: C901
"""
Gravity calibration method of van Hees et al. 2014 (https://pubmed.ncbi.nlm.nih.gov/25103964/)

Expand Down Expand Up @@ -283,10 +283,12 @@ def calibrate_gravity(data, calib_cube=0.3, calib_min_samples=50, window='10s',
del stationary_indicator
del nonzero

info['CalibNumSamples'] = len(xyz)

if len(xyz) < calib_min_samples:
info['CalibOK'] = 0
info['CalibErrorBefore(mg)'] = np.nan
info['CalibErrorAfter(mg)'] = np.nan
info['CalibOK'] = 0
warnings.warn(f"Skipping calibration: Insufficient stationary samples: {len(xyz)} < {calib_min_samples}")
return data, info

Expand Down Expand Up @@ -316,15 +318,17 @@ def calibrate_gravity(data, calib_cube=0.3, calib_min_samples=50, window='10s',
# Check that we have sufficiently uniformly distributed points:
# need at least one point outside each face of the cube
if (np.max(xyz, axis=0) < calib_cube).any() or (np.min(xyz, axis=0) > -calib_cube).any():
info['CalibOK'] = 0
info['CalibErrorAfter(mg)'] = init_err * 1000
info['CalibNumIters'] = 0
info['CalibOK'] = 0

return data, info

# If initial error is already below threshold, skip and return
if init_err < ERR_TOL:
info['CalibOK'] = 1
info['CalibErrorAfter(mg)'] = init_err * 1000
info['CalibNumIters'] = 0
info['CalibOK'] = 1

return data, info

Expand Down Expand Up @@ -374,6 +378,7 @@ def calibrate_gravity(data, calib_cube=0.3, calib_min_samples=50, window='10s',
break

info['CalibErrorAfter(mg)'] = best_err * 1000
info['CalibNumIters'] = it + 1

if (best_err >= ERR_TOL) or (it + 1 >= MAXITER):
info['CalibOK'] = 0
Expand Down Expand Up @@ -427,18 +432,18 @@ def calibrate_gravity(data, calib_cube=0.3, calib_min_samples=50, window='10s',
del data_mmap

info['CalibOK'] = 1
info['CalibNumIters'] = it + 1
info['CalibNumSamples'] = len(xyz)
info['CalibxIntercept'] = best_intercept[0]
info['CalibyIntercept'] = best_intercept[1]
info['CalibzIntercept'] = best_intercept[2]
info['CalibxSlope'] = best_slope[0]
info['CalibySlope'] = best_slope[1]
info['CalibzSlope'] = best_slope[2]
if hasT:
info['CalibxSlopeT'] = best_slopeT[0]
info['CalibySlopeT'] = best_slopeT[1]
info['CalibzSlopeT'] = best_slopeT[2]

if return_coeffs:
info['CalibxIntercept'] = best_intercept[0]
info['CalibyIntercept'] = best_intercept[1]
info['CalibzIntercept'] = best_intercept[2]
info['CalibxSlope'] = best_slope[0]
info['CalibySlope'] = best_slope[1]
info['CalibzSlope'] = best_slope[2]
if hasT:
info['CalibxSlopeT'] = best_slopeT[0]
info['CalibySlopeT'] = best_slopeT[1]
info['CalibzSlopeT'] = best_slopeT[2]

return data, info

Expand Down
2 changes: 1 addition & 1 deletion src/actipy/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def read_device(input_file,

if calibrate_gravity:
timer.start("Gravity calibration...")
data, info_calib = P.calibrate_gravity(data)
data, info_calib = P.calibrate_gravity(data, return_coeffs=False)
info.update(info_calib)
timer.stop()

Expand Down
Loading