@@ -628,58 +628,57 @@ def weight_proportions(
628
628
catch_data [stratum_column ].unique (),
629
629
aged_weights_binned .unstack ().reset_index ()[stratum_column ].unique (),
630
630
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 ()})
644
632
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
647
635
catch_strata_weights = catch_data .count_variable (
648
636
contrasts = [stratum_column ], variable = "haul_weight" , fun = "sum"
649
637
)
650
638
# ---- Rename resulting columns for both
651
639
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 )
657
657
# ---- 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
664
662
unaged_weights_binned_filled = (
665
663
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 )
667
665
.fillna (0.0 ).T .unstack ()
668
666
)
669
- # ---- Calculate the total weight per stratum per sex
667
+ # ---- Sum these weights for each sex (male/female)
670
668
unaged_weights_sex = unaged_weights_binned_filled .sum ()
671
669
# ---- 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 )
673
671
# ---- Standardize the unaged sexed weights
674
672
unaged_weights_sex_standardized = (
675
673
(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
+
679
677
# Calculate the specimen (aged) weight proportions
680
678
# ---- Re-pivot the aged weight bins table
681
679
aged_weights_binned_pvt = (
682
- aged_weights_binned .unstack ()
680
+ aged_weights_binned_filled
681
+ .stack ([stratum_column , "sex" ], future_stack = True )
683
682
.reset_index (name = "weight" )
684
683
.pivot_table (
685
684
columns = [stratum_column ],
@@ -688,12 +687,10 @@ def weight_proportions(
688
687
observed = False ,
689
688
)
690
689
)
691
- # ---- Back-fill missing strata
692
-
693
690
# ---- Divide by the aged stratum weights (relative to only aged fish)
694
691
aged_weight_proportions_pvt = (
695
692
aged_weights_binned_pvt / aged_strata_weights ["stratum_weight" ].to_numpy ()
696
- )
693
+ ). fillna ( 0.0 )
697
694
# ---- Pivot back to the desired format
698
695
aged_weight_proportions = (
699
696
aged_weight_proportions_pvt .stack ()
@@ -898,7 +895,7 @@ def age1_metric_proportions(
898
895
values = "proportion_number_aged" ,
899
896
aggfunc = "sum" ,
900
897
observed = False ,
901
- )
898
+ ). T . reindex ( all_strata ). T . fillna ( 0.0 )
902
899
# ---- Dot product to calculate the new average sigma_bs for all ages
903
900
updated_sigma_bs = length_bins ["length_sq" ].values .dot (age_proportions_dot_table )
904
901
@@ -913,22 +910,18 @@ def age1_metric_proportions(
913
910
aggfunc = "sum" ,
914
911
observed = False ,
915
912
)
913
+ # ---- Reindex
914
+ age1_proportions_table = age1_proportions_table .T .reindex (all_strata ).T .fillna (0.0 )
916
915
# ---- Dot product to calculate the average sigma_bs for age-1 fish
917
916
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 )
920
921
# ---- Sum the number proportions for each age bin within each stratum
921
922
age1_strata_proportions = age1_proportions_table .sum (axis = 0 ).to_numpy ()
922
923
923
924
# 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
- )
932
925
# ---- Unaged
933
926
unage_proportions_table = unage_proportions_all .pivot_table (
934
927
columns = [stratum_col ],
@@ -982,7 +975,7 @@ def age1_metric_proportions(
982
975
983
976
# Return output
984
977
# ---- 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 })
986
979
# ---- Number proportions
987
980
apportioned_age1 ["number_proportion" ] = age1_strata_proportions
988
981
# ---- Weight proportions
0 commit comments