@@ -46,31 +46,46 @@ def stratified_transect_statistic(
46
46
stratum_col = settings_dict ["stratum_name" ]
47
47
# ---- Get the variable name
48
48
var_name = settings_dict ["variable" ]
49
+ # ---- Create copy of transect data
50
+ transect_copy = transect_data .copy ()
51
+ # ---- Create transect summary copy
52
+ summary_copy = transect_summary .copy ()
53
+ # ---- Create strata summary copy
54
+ strata_copy = strata_summary .copy ()
49
55
50
56
# Get indexed transect distance
51
- transect_distances = transect_summary .set_index ([stratum_col ,
52
- "transect_num" ])["transect_distance" ]
57
+ transect_distances = summary_copy .set_index ([stratum_col ,
58
+ "transect_num" ])["transect_distance" ]
53
59
# ---- Drop any transects where distance is 0.0 (i.e. from a single mesh node)
54
60
if np .any (transect_distances == 0.0 ):
55
61
# ---- Pick out transects where distance = 0.0 nmi
56
- zero_distances = transect_distances [transect_distances == 0.0 ]. index . to_numpy ()
62
+ zero_distances = transect_distances [transect_distances == 0.0 ]
57
63
# ---- Update `transect_distances`
58
64
transect_distances = transect_distances [transect_distances > 0.0 ]
65
+ # ---- Set identical index
66
+ transect_copy .set_index ([stratum_col , "transect_num" ], inplace = True )
59
67
# ---- Update `transect_data`
60
- transect_data = transect_data [~ transect_data ["transect_num" ].isin (zero_distances )]
68
+ transect_copy .drop (zero_distances .index , inplace = True )
69
+ # ---- Reset
70
+ transect_copy .reset_index (inplace = True )
71
+ # ---- Set identical index
72
+ summary_copy .set_index ([stratum_col , "transect_num" ], inplace = True )
61
73
# ---- Get the 'poor' transect strata
62
74
zero_distances_strata = (
63
- transect_summary .loc [zero_distances ].groupby ([stratum_col ], observed = False ).size ()
75
+ summary_copy .loc [zero_distances .index ]
76
+ .reset_index ().groupby ([stratum_col ], observed = False ).size ()
64
77
)
65
78
# ---- Update `transect_summary`
66
- transect_summary = transect_summary [~ transect_summary ["transect_num" ].isin (zero_distances )]
79
+ summary_copy .drop (zero_distances .index , inplace = True )
80
+ # ---- Reset
81
+ summary_copy .reset_index (inplace = True )
67
82
# ---- Update `strata_summary`
68
83
# -------- Set index
69
- strata_summary .set_index ([stratum_col ], inplace = True )
84
+ strata_copy .set_index ([stratum_col ], inplace = True )
70
85
# -------- Subtract the 'poor' transects from the total transect counts
71
- strata_summary ["transect_count" ] = strata_summary ["transect_count" ] - zero_distances_strata
86
+ strata_copy ["transect_count" ] = strata_copy ["transect_count" ] - zero_distances_strata
72
87
# -------- Reset index
73
- strata_summary .reset_index (inplace = True )
88
+ strata_copy .reset_index (inplace = True )
74
89
75
90
if settings_dict ["verbose" ]:
76
91
if settings_dict ["dataset" ] == "kriging" :
@@ -92,7 +107,7 @@ def stratified_transect_statistic(
92
107
93
108
# Calculate the number of transects per stratum
94
109
num_transects_to_sample = np .round (
95
- strata_summary .set_index (stratum_col )["transect_count" ] * transect_sample
110
+ strata_copy .set_index (stratum_col )["transect_count" ] * transect_sample
96
111
).astype (int )
97
112
98
113
# Offset term used for later variance calculation
@@ -102,25 +117,25 @@ def stratified_transect_statistic(
102
117
sample_dof = num_transects_to_sample * (num_transects_to_sample - sample_offset )
103
118
104
119
# Transect areas
105
- transect_areas = transect_summary .groupby ([stratum_col , "transect_num" ],
106
- observed = False )["transect_area" ].sum ()
120
+ transect_areas = summary_copy .groupby ([stratum_col , "transect_num" ],
121
+ observed = False )["transect_area" ].sum ()
107
122
108
123
# Get indexed total transect area
109
- total_transect_area = strata_summary .set_index (stratum_col )["transect_area_total" ]
124
+ total_transect_area = strata_copy .set_index (stratum_col )["transect_area_total" ]
110
125
111
126
# Get indexed biological value
112
- biological_values = transect_data .groupby ([stratum_col , "transect_num" ],
127
+ biological_values = transect_copy .groupby ([stratum_col , "transect_num" ],
113
128
observed = False )[var_name ].sum ()
114
129
115
130
# Get indexed transect numbers
116
- transect_numbers = transect_summary .set_index (stratum_col )["transect_num" ]
131
+ transect_numbers = summary_copy .set_index (stratum_col )["transect_num" ]
117
132
118
133
# Calculate the summed/mean density per transect
119
134
# ---- Set temporary index
120
- transect_summary .set_index ([stratum_col , "transect_num" ], inplace = True )
135
+ summary_copy .set_index ([stratum_col , "transect_num" ], inplace = True )
121
136
# ---- Compute summed/mean density
122
- transect_summary ["density" ] = transect_data .groupby ([stratum_col , "transect_num" ],
123
- observed = False )[
137
+ summary_copy ["density" ] = transect_copy .groupby ([stratum_col , "transect_num" ],
138
+ observed = False )[
124
139
settings_dict ["variable" ]
125
140
].sum ()
126
141
@@ -204,17 +219,17 @@ def stratified_transect_statistic(
204
219
total_area = area_array .sum ()
205
220
206
221
# Reset index for `transect_summary`
207
- transect_summary .reset_index (inplace = True )
222
+ summary_copy .reset_index (inplace = True )
208
223
209
224
# Compute the "population" (i.e. original data) statistics
210
225
# This is necessary for constructing the bootstrapped confidence intervals
211
226
# ---- Mean density
212
227
if settings_dict ["variable" ] == "nasc" :
213
228
# ---- Compute sum per transect line first
214
- line_density = transect_data .groupby ([stratum_col ,
229
+ line_density = transect_copy .groupby ([stratum_col ,
215
230
"transect_num" ])[var_name ].sum ().to_frame ()
216
231
# ---- Create copy of `transect_summary` and set index
217
- line_length = transect_summary .copy ().set_index ([stratum_col , "transect_num" ])
232
+ line_length = summary_copy .copy ().set_index ([stratum_col , "transect_num" ])
218
233
# ---- Add stratum
219
234
line_density [stratum_col ] = line_length [stratum_col ]
220
235
# ---- Convert to the density
@@ -229,10 +244,10 @@ def stratified_transect_statistic(
229
244
survey_density_mean = stratum_density_means .mean ()
230
245
else :
231
246
# ---- Get density column name
232
- density_name = [col for col in transect_data .columns if "_density" in col ]
247
+ density_name = [col for col in transect_copy .columns if "_density" in col ]
233
248
# ---- Calculate mean per stratum
234
249
stratum_density_means = (
235
- transect_data .groupby ([stratum_col ], observed = False )[density_name ]
250
+ transect_copy .groupby ([stratum_col ], observed = False )[density_name ]
236
251
.mean ()
237
252
.to_numpy ()
238
253
.flatten ()
@@ -241,7 +256,7 @@ def stratified_transect_statistic(
241
256
survey_density_mean = stratum_density_means .mean ()
242
257
# ---- Total
243
258
# -------- By stratum
244
- stratum_total = transect_data .groupby ([stratum_col ], observed = False )[var_name ].sum ().to_numpy ()
259
+ stratum_total = transect_copy .groupby ([stratum_col ], observed = False )[var_name ].sum ().to_numpy ()
245
260
# -------- By survey
246
261
survey_total = stratum_total .sum ()
247
262
# ---- Compute the stratum total proportions relative to survey sum
@@ -305,7 +320,7 @@ def stratified_transect_statistic(
305
320
stratified_results = {
306
321
"variable" : settings_dict ["variable" ],
307
322
"ci_percentile" : 0.95 ,
308
- "num_transects" : strata_summary ["transect_count" ].sum (),
323
+ "num_transects" : strata_copy ["transect_count" ].sum (),
309
324
"stratum_area" : area_array ,
310
325
"total_area" : total_area ,
311
326
"estimate" : {
0 commit comments