Skip to content

Commit 1470d8b

Browse files
committed
Address mismatched stratum indices across hauls
1 parent 9a7e525 commit 1470d8b

File tree

2 files changed

+41
-49
lines changed

2 files changed

+41
-49
lines changed

echopop/acoustics.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,6 @@ def nasc_to_biomass(
330330
adult_proportions["weight_proportion"] = 1 - age1_proportions["weight_proportion"]
331331
# ------------ Weight
332332
adult_proportions["nasc_proportion"] = 1 - age1_proportions["nasc_proportion"]
333-
334333
else:
335334
# ---- Assign filled adult proportions dataframe
336335
adult_proportions = pd.DataFrame(

echopop/biology.py

Lines changed: 41 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -628,58 +628,57 @@ def weight_proportions(
628628
catch_data[stratum_column].unique(),
629629
aged_weights_binned.unstack().reset_index()[stratum_column].unique(),
630630
unaged_weights_binned.unstack().reset_index()[stratum_column].unique(),
631-
])).tolist()}).set_index([stratum_column])
632-
633-
# Back-fill missing strata
634-
# ---- Aged fish
635-
aged_weights_binned_filled = (
636-
aged_weights_binned.unstack().reset_index(name="weight").set_index([stratum_column]).reindex(all_strata.index)
637-
638-
.reindex([1]).infer_objects(copy=False)
639-
)
640-
# ---- Sum these weights for each sex (male/female)
641-
aged_weights_sex = aged_weights_binned.sum().unstack(["age_bin"]).sum(axis=1).unstack(0)
642-
# ---- Calculate the stratum totals
643-
aged_strata_weights = aged_weights_sex.sum().reset_index(name="stratum_weight")
631+
])).tolist()})
644632

645-
# Calculate the sexed and total stratum weights for each sex among unaged fish
646-
# ---- Sum the net haul weights from station 1/unaged fish
633+
# Prepare the tottal catch strata data
634+
# ---- Total catch per strata
647635
catch_strata_weights = catch_data.count_variable(
648636
contrasts=[stratum_column], variable="haul_weight", fun="sum"
649637
)
650638
# ---- Rename resulting columns for both
651639
catch_strata_weights.rename(columns={"count": "stratum_weight"}, inplace=True)
652-
unaged_weights_binned.unstack().reset_index()[stratum_column]
653-
# Sum the sexed and total weights from the weight-fitted unaged data
654-
655-
# ---- Calculate the total weight per stratum per sex
656-
unaged_weights_sex = unaged_weights_binned_filled.sum()
640+
# ---- Back-fill for all strata
641+
catch_strata_weights_filled = (
642+
catch_strata_weights.set_index([stratum_column])
643+
.reindex(all_strata[stratum_column]).fillna(0.0)
644+
)
645+
646+
# Back-fill missing strata for aged fish
647+
# ---- Repivot and back-fill
648+
aged_weights_binned_filled = (
649+
aged_weights_binned.
650+
stack(future_stack=True)
651+
.T.unstack("sex").
652+
reindex(all_strata[stratum_column])
653+
.fillna(0.0).T.unstack()
654+
)
655+
# ---- Sum these weights for each sex (male/female)
656+
aged_weights_sex = aged_weights_binned_filled.sum().unstack(0)
657657
# ---- Calculate the stratum totals
658-
unaged_strata_weights = unaged_weights_sex.unstack(0).sum(axis=0)
659-
660-
661-
# Back-fill any missing strata
662-
# ---- Aged
663-
658+
aged_strata_weights = aged_weights_sex.sum().reset_index(name="stratum_weight")
659+
660+
# Back-fill missing strata for unaged fish
661+
# ---- Repivot and back-fill
664662
unaged_weights_binned_filled = (
665663
unaged_weights_binned.stack(future_stack=True).T
666-
.reindex(catch_strata_weights[stratum_column]).infer_objects(copy=False)
664+
.reindex(all_strata[stratum_column]).infer_objects(copy=False)
667665
.fillna(0.0).T.unstack()
668666
)
669-
# ---- Calculate the total weight per stratum per sex
667+
# ---- Sum these weights for each sex (male/female)
670668
unaged_weights_sex = unaged_weights_binned_filled.sum()
671669
# ---- Calculate the stratum totals
672-
unaged_strata_weights = unaged_weights_sex.unstack(0).sum(axis=0)
670+
unaged_strata_weights = unaged_weights_sex.unstack(0).sum(axis=0)
673671
# ---- Standardize the unaged sexed weights
674672
unaged_weights_sex_standardized = (
675673
(unaged_weights_sex / unaged_strata_weights).unstack(0)
676-
* catch_strata_weights["stratum_weight"].to_numpy()
677-
).fillna(0.0)
678-
674+
* catch_strata_weights_filled.values.T
675+
).fillna(0.0)
676+
679677
# Calculate the specimen (aged) weight proportions
680678
# ---- Re-pivot the aged weight bins table
681679
aged_weights_binned_pvt = (
682-
aged_weights_binned.unstack()
680+
aged_weights_binned_filled
681+
.stack([stratum_column, "sex"], future_stack=True)
683682
.reset_index(name="weight")
684683
.pivot_table(
685684
columns=[stratum_column],
@@ -688,12 +687,10 @@ def weight_proportions(
688687
observed=False,
689688
)
690689
)
691-
# ---- Back-fill missing strata
692-
693690
# ---- Divide by the aged stratum weights (relative to only aged fish)
694691
aged_weight_proportions_pvt = (
695692
aged_weights_binned_pvt / aged_strata_weights["stratum_weight"].to_numpy()
696-
)
693+
).fillna(0.0)
697694
# ---- Pivot back to the desired format
698695
aged_weight_proportions = (
699696
aged_weight_proportions_pvt.stack()
@@ -898,7 +895,7 @@ def age1_metric_proportions(
898895
values="proportion_number_aged",
899896
aggfunc="sum",
900897
observed=False,
901-
)
898+
).T.reindex(all_strata).T.fillna(0.0)
902899
# ---- Dot product to calculate the new average sigma_bs for all ages
903900
updated_sigma_bs = length_bins["length_sq"].values.dot(age_proportions_dot_table)
904901

@@ -913,22 +910,18 @@ def age1_metric_proportions(
913910
aggfunc="sum",
914911
observed=False,
915912
)
913+
# ---- Reindex
914+
age1_proportions_table = age1_proportions_table.T.reindex(all_strata).T.fillna(0.0)
916915
# ---- Dot product to calculate the average sigma_bs for age-1 fish
917916
age1_sigma_bs = length_bins["length_sq"].values.dot(age1_proportions_table)
918-
# ---- Calculate age-1 NASC proportioon per stratum
919-
age1_nasc_proportions = age1_sigma_bs / updated_sigma_bs
917+
# ---- Calculate age-1 NASC proportioon per stratum
918+
age1_nasc_proportions = np.divide(age1_sigma_bs,
919+
updated_sigma_bs,
920+
where=updated_sigma_bs != 0.0)
920921
# ---- Sum the number proportions for each age bin within each stratum
921922
age1_strata_proportions = age1_proportions_table.sum(axis=0).to_numpy()
922923

923924
# Convert the primary tables into pivot tables
924-
# ---- Aged
925-
age_proportions_table = age_proportions_all.pivot_table(
926-
index=["length_bin"],
927-
columns=[stratum_col, "age_bin"],
928-
values="proportion_number_aged",
929-
aggfunc="sum",
930-
observed=False,
931-
)
932925
# ---- Unaged
933926
unage_proportions_table = unage_proportions_all.pivot_table(
934927
columns=[stratum_col],
@@ -982,7 +975,7 @@ def age1_metric_proportions(
982975

983976
# Return output
984977
# ---- Create DataFrame
985-
apportioned_age1 = pd.DataFrame({f"{stratum_col}": np.unique(age_proportions[stratum_col])})
978+
apportioned_age1 = pd.DataFrame({f"{stratum_col}": all_strata})
986979
# ---- Number proportions
987980
apportioned_age1["number_proportion"] = age1_strata_proportions
988981
# ---- Weight proportions

0 commit comments

Comments
 (0)