14
14
"""
15
15
import pandas as pd
16
16
import numpy as np
17
- from PopSynthesis .Methods .IPSF .const import zone_field
17
+ from PopSynthesis .Methods .IPSF .const import count_field , zone_field
18
18
from typing import List
19
+ import itertools
20
+ import random
19
21
20
- def zone_adjustment (curr_syn_count : pd .DataFrame , diff_census : pd .Series , pool : pd .DataFrame , adjusted_atts : List [str ]) -> pd .DataFrame :
21
- assert zone_field in curr_syn_count .columns
22
- assert zone_field in pool .columns
23
22
24
- print (diff_census )
23
+ def find_combinations_with_prev_atts (filtered_targeted_samples : pd .DataFrame , prev_atts : List [str ]):
24
+ sub_samples = filtered_targeted_samples [prev_atts + [count_field ]]
25
+ count_samples = sub_samples .groupby (sub_samples .columns .difference ([count_field ]).to_list (), as_index = False )[count_field ].sum ()
26
+ return count_samples .set_index (count_samples .columns .difference ([count_field ]).to_list ())
27
+
28
+ def zone_adjustment (att : str , curr_syn_count : pd .DataFrame , diff_census : pd .Series , pool : pd .DataFrame , adjusted_atts : List [str ]) -> pd .DataFrame :
29
+ assert count_field in curr_syn_count .columns
30
+ assert count_field in pool .columns
31
+ assert len (curr_syn_count [zone_field ].unique ()) == 1
32
+ curr_syn_count = curr_syn_count .drop (columns = [zone_field ])
33
+
34
+ neg_states = diff_census [diff_census < 0 ].index .tolist ()
35
+ pos_states = diff_census [diff_census > 0 ].index .tolist ()
36
+ pairs_adjust = list (itertools .product (neg_states , pos_states ))
37
+ random .shuffle (pairs_adjust )
38
+
39
+ for neg_state , pos_state in pairs_adjust :
40
+ # Check neg state
41
+ filtered_syn_pop = curr_syn_count [curr_syn_count [att ] == neg_state ]
42
+ count_neg_in_syn = find_combinations_with_prev_atts (filtered_syn_pop , adjusted_atts )
43
+ neg_val = int (diff_census [neg_state ])
44
+
45
+ # check pos state
46
+ filtered_pool = pool [pool [att ] == pos_state ]
47
+ count_pos_in_pool = find_combinations_with_prev_atts (filtered_pool , adjusted_atts )
48
+ pos_val = int (diff_census [pos_state ])
49
+
50
+ n_adjust = min (abs (neg_val ), pos_val ) # the possible to adjust
51
+ possible_prev_comb = set (count_neg_in_syn .index ) & set (count_pos_in_pool .index )
52
+ filtered_count_neg = count_neg_in_syn .loc [list (possible_prev_comb )]
53
+ filtered_count_pos = count_pos_in_pool .loc [list (possible_prev_comb )]
54
+
55
+
56
+ break
0 commit comments