@@ -17,14 +17,18 @@ def _generate_estimate(input):
1717 else :
1818 df = input [0 ].data .copy ()
1919 if input [0 ].index_type == "singlegroup" :
20- df = input [1 ](df , group = input [0 ].group_pop_var , total = input [0 ].total_pop_var ,)
20+ df = input [1 ](
21+ df ,
22+ group = input [0 ].group_pop_var ,
23+ total = input [0 ].total_pop_var ,
24+ )
2125 estimate = (
2226 input [0 ]
2327 .__class__ (df , input [0 ].group_pop_var , input [0 ].total_pop_var , ** input [2 ])
2428 .statistic
2529 )
2630 else :
27- df = input [1 ](df , groups = input [0 ].groups )
31+ df = input [1 ](df , groups = input [0 ].groups , verbose = input [ 3 ] )
2832 estimate = input [0 ].__class__ (df , input [0 ].groups , ** input [2 ]).statistic
2933 return estimate
3034
@@ -36,6 +40,7 @@ def simulate_null(
3640 n_jobs = - 1 ,
3741 backend = "loky" ,
3842 index_kwargs = None ,
43+ verbose = False ,
3944):
4045 """Simulate a series of index values in parallel to serve as a null distribution.
4146
@@ -56,6 +61,8 @@ def simulate_null(
5661 index_kwargs : dict, optional
5762 additional keyword arguments used to fit the index, such as distance or network
5863 if estimating a spatial index; by default None
64+ verbose: bool
65+ whether to print warning statements
5966
6067 Returns
6168 -------
@@ -67,13 +74,13 @@ def simulate_null(
6774 if n_jobs == - 1 :
6875 n_jobs = multiprocessing .cpu_count ()
6976 estimates = Parallel (n_jobs = n_jobs , backend = backend )(
70- delayed (_generate_estimate )((seg_class , sim_func , index_kwargs ))
77+ delayed (_generate_estimate )((seg_class , sim_func , index_kwargs , verbose ))
7178 for i in tqdm (range (iterations ))
7279 )
7380 return pd .Series (estimates )
7481
7582
76- def simulate_person_permutation (df , group = None , total = None , groups = None ):
83+ def simulate_person_permutation (df , group = None , total = None , groups = None , verbose = False ):
7784 """Simulate the permutation of individuals across spatial units.
7885
7986 Parameters
@@ -145,7 +152,7 @@ def simulate_person_permutation(df, group=None, total=None, groups=None):
145152 return gpd .GeoDataFrame (df , geometry = geoms .geometry .name )
146153
147154
148- def simulate_evenness (df , group = None , total = None , groups = None ):
155+ def simulate_evenness (df , group = None , total = None , groups = None , verbose = True ):
149156 """Simulate even redistribution of population groups across spatial units.
150157
151158 Parameters
@@ -192,17 +199,17 @@ def simulate_evenness(df, group=None, total=None, groups=None):
192199 global_prob_vector = df .sum (axis = 0 ) / df .sum ().sum ()
193200 t = df [groups ].sum (axis = 1 ).astype (int )
194201
195- simul = list (
196- map (lambda i : list (np .random .multinomial (i , global_prob_vector )), t )
197- )
202+ simul = [list (np .random .multinomial (i , global_prob_vector )) for i in t ]
198203 output = pd .DataFrame (simul , columns = groups )
199204 if geoms :
200205 return gpd .GeoDataFrame (output , geometry = geoms , crs = crs )
201206
202207 return output
203208
204209
205- def simulate_systematic_randomization (df , group = None , total = None , groups = None ):
210+ def simulate_systematic_randomization (
211+ df , group = None , total = None , groups = None , verbose = True
212+ ):
206213 """Simulate systematic redistribution of population groups across spatial units.
207214
208215 Parameters
@@ -211,7 +218,7 @@ def simulate_systematic_randomization(df, group=None, total=None, groups=None):
211218 geodataframe with population data to be randomized
212219 group : str, optional
213220 name of column on geodataframe that holds the group total
214- (for use with singlegroup indices).
221+ (for use with singlegroup indices).
215222 total : str, optional
216223 name of column on geodataframe that holds the total population for
217224 each unit. For singlegroup indices, this parameter is required. For
@@ -242,16 +249,18 @@ def simulate_systematic_randomization(df, group=None, total=None, groups=None):
242249 Reference: :cite:`allen2015more`
243250 """
244251 if groups :
245- if not total :
252+ if not total and verbose :
246253 warn (
247- "No `total` argument passed. Assuming population groups are exhaustive"
254+ "No `total` argument passed. Assuming population groups are exhaustive" ,
255+ stacklevel = 2 ,
248256 )
249257 total = "total"
250258 df [total ] = df [groups ].sum (axis = 1 )
251259 if group :
252- assert (
253- total
254- ), "If simulating a single group, you must also supply a total population column"
260+ if not total :
261+ raise ValueError (
262+ "If simulating a single group, you must also supply a total population column"
263+ )
255264 df ["other_group_pop" ] = df [total ] - df [group ]
256265 groups = [group , "other_group_pop" ]
257266
@@ -320,7 +329,9 @@ def simulate_geo_permutation(df, **kwargs):
320329 return data
321330
322331
323- def simulate_systematic_geo_permutation (df , group = None , total = None , groups = None ):
332+ def simulate_systematic_geo_permutation (
333+ df , group = None , total = None , groups = None , verbose = True
334+ ):
324335 """Simulate systematic redistribution followed by random permutation of geographic units.
325336
326337 Parameters
@@ -342,12 +353,16 @@ def simulate_systematic_geo_permutation(df, group=None, total=None, groups=None)
342353 geopandas.GeoDataFrame
343354 geodataframe with systematically randomized population groups
344355 """
345- df = simulate_systematic_randomization (df , group = group , total = total , groups = groups )
356+ df = simulate_systematic_randomization (
357+ df , group = group , total = total , groups = groups , verbose = verbose
358+ )
346359 df = simulate_geo_permutation (df )
347360 return df
348361
349362
350- def simulate_evenness_geo_permutation (df , group = None , total = None , groups = None ):
363+ def simulate_evenness_geo_permutation (
364+ df , group = None , total = None , groups = None , verbose = True
365+ ):
351366 """Simulate evenness followed by random permutation of geographic units.
352367
353368 Parameters
0 commit comments