Skip to content

Commit 53e7b8d

Browse files
committed
Update _nimbaldetach.py
1 parent 275899a commit 53e7b8d

1 file changed

Lines changed: 28 additions & 41 deletions

File tree

src/wristpy/processing/_nimbaldetach.py

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -114,63 +114,50 @@ def detach(
114114
]
115115
)
116116

117-
##### set up criteria for nonwear times #####
118-
119-
candidate_nw_starts = np.where(
117+
candidate_nw_starts = np.flatnonzero(
120118
(accel_temp_df["Num_axes_fwd"] >= num_axes)
121119
& (accel_temp_df["Percent_above_threshold_5min"] >= 0.9)
122120
)
123121

124-
end_crit_1 = np.array(
125-
np.where(
126-
(accel_temp_df["Num_axes_bwd"] == 0)
127-
& (accel_temp_df["Percent_above_threshold_5min_bwd"] <= 0.50)
128-
& (accel_temp_df["Mean_temp_5min"] > temp_inc_roc)
129-
)
130-
)[0]
122+
end_crit_1 = np.flatnonzero(
123+
(accel_temp_df["Num_axes_bwd"] == 0)
124+
& (accel_temp_df["Percent_above_threshold_5min_bwd"] <= 0.50)
125+
& (accel_temp_df["Mean_temp_5min"] > temp_inc_roc)
126+
)
131127

132-
end_crit_2 = np.array(
133-
np.where(
134-
(accel_temp_df["Num_axes_bwd"] == 0)
135-
& (accel_temp_df["Percent_above_threshold_5min_bwd"] <= 0.50)
136-
& (accel_temp_df["Min_temp_5min"] > low_temperature_cutoff)
137-
)
138-
)[0]
128+
end_crit_2 = np.flatnonzero(
129+
(accel_temp_df["Num_axes_bwd"] == 0)
130+
& (accel_temp_df["Percent_above_threshold_5min_bwd"] <= 0.50)
131+
& (accel_temp_df["Min_temp_5min"] > low_temperature_cutoff)
132+
)
139133

140134
end_crit_combined = np.sort(np.unique(np.concatenate((end_crit_1, end_crit_2))))
141135

142-
#### assign nonwear times#####
136+
max_temp_5min = accel_temp_df["Max_temp_5min"].to_numpy()
137+
mean_temp_5min = accel_temp_df["Mean_temp_5min"].to_numpy()
143138
vert_nonwear_array = np.zeros(len(acceleration_values))
144139
previous_end = 0
145-
for ind in candidate_nw_starts[0]:
146-
if ind < previous_end:
147-
continue
148-
valid_start = False
149-
start_ind = int(ind)
150-
end_ind = int(ind + sampling_rate * 60 * 5)
151140

152-
if (accel_temp_df["Max_temp_5min"][start_ind] < high_temperature_cutoff) & (
153-
accel_temp_df["Mean_temp_5min"][start_ind] < temp_dec_roc
154-
):
155-
valid_start = True
156-
elif accel_temp_df["Max_temp_5min"][start_ind] < low_temperature_cutoff:
157-
valid_start = True
158-
159-
if not valid_start:
141+
for start_ind in candidate_nw_starts:
142+
if start_ind < previous_end:
160143
continue
161144

162-
end_crit = end_crit_combined[end_crit_combined > end_ind]
145+
end_ind = start_ind + window_size_1min * 5
163146

164-
if len(end_crit) > 0:
165-
bout_end_index = end_crit[0]
147+
valid_start = (
148+
max_temp_5min[start_ind] < high_temperature_cutoff
149+
and mean_temp_5min[start_ind] < temp_dec_roc
150+
) or max_temp_5min[start_ind] < low_temperature_cutoff
166151

167-
else:
168-
bout_end_index = len(accel_temp_df) - 1
152+
if not valid_start:
153+
continue
169154

170-
accel_start_dp = int(start_ind)
171-
accel_end_dp = int(bout_end_index)
172-
vert_nonwear_array[accel_start_dp:accel_end_dp] = 1
155+
end_nw_indices = end_crit_combined[end_crit_combined > end_ind]
156+
bout_end_index = (
157+
end_nw_indices[0] if len(end_nw_indices) > 0 else accel_temp_df.height - 1
158+
)
173159

160+
vert_nonwear_array[start_ind:bout_end_index] = 1
174161
previous_end = bout_end_index
175162

176163
return vert_nonwear_array
@@ -182,7 +169,7 @@ def lowpass_filter_signal(
182169
low_f: float,
183170
filter_order: int = 2,
184171
) -> np.ndarray:
185-
"""Function that low pass fiters temperature data.
172+
"""Function that low pass filters temperature data.
186173
187174
Args:
188175
data: 1D numpy array of data to be filtered

0 commit comments

Comments
 (0)