@@ -29,7 +29,7 @@ def weight_distr(df, mode):
29
29
plt .ylabel ("Total Patient Observations" )
30
30
plt .xlabel ("Recorded Weight (Kg)" )
31
31
plt .grid ()
32
- return wgt_grp_sum_plot
32
+ plt . show ()
33
33
34
34
35
35
def overlap_view_adults (
@@ -215,6 +215,18 @@ def overlap_view_pediatrics(
215
215
return selected_param_plot
216
216
217
217
218
+ def overlap_view_pediatrics_show (
219
+ obs_df , subjid , param , include_carry_forward , include_percentiles , wt_df , ht_df
220
+ ):
221
+ """
222
+ Wraps overlap_view_pediatrics with plt.show().
223
+ """
224
+ plot = overlap_view_pediatrics (
225
+ obs_df , subjid , param , include_carry_forward , include_percentiles , wt_df , ht_df
226
+ )
227
+ plt .show ()
228
+
229
+
218
230
def overlap_view_double_pediatrics (
219
231
obs_df ,
220
232
subjid ,
@@ -359,7 +371,7 @@ def overlap_view_double_pediatrics(
359
371
360
372
# Reset figsize to default
361
373
plt .rcParams ["figure.figsize" ] = [6.4 , 4.8 ]
362
- return fig
374
+ plt . show ()
363
375
364
376
365
377
def mult_obs (obs ):
@@ -375,6 +387,24 @@ def mult_obs(obs):
375
387
return obs [obs ["any_ones" ] == 0 ]
376
388
377
389
390
+ def five_by_five_shape (n ):
391
+ """
392
+ Determines shape of five by five view, allowing for fewer than 25 observations.
393
+
394
+ Parameters:
395
+ n: length of subject list to display
396
+
397
+ Returns:
398
+ Dimensions of grid/subplots as (nrows, ncols)
399
+ """
400
+ if n // 5 == 0 :
401
+ return (1 , n % 5 )
402
+ elif n % 5 > 0 :
403
+ return ((n // 5 ) + 1 , 5 )
404
+ else :
405
+ return (n // 5 , 5 )
406
+
407
+
378
408
def five_by_five_view (obs_df , subjids , param , wt_df , ht_df , bmi_df , linestyle ):
379
409
"""
380
410
Creates a small multiples plot showing the growth trend for 25 individuals
@@ -384,17 +414,32 @@ def five_by_five_view(obs_df, subjids, param, wt_df, ht_df, bmi_df, linestyle):
384
414
subjids: An list of the ids of the individuals to be plotted
385
415
param: (String) Whether to plot heights or weights. Expected values are "HEIGHTCM" or "WEIGHTKG"
386
416
"""
387
- fig , ax = plt .subplots (5 , 5 )
388
- for y in range (5 ):
389
- for x in range (5 ):
390
- subjid = subjids [x * 5 + y ]
417
+ if len (subjids ) == 0 :
418
+ print ("No matching subjects found." )
419
+ return
420
+ nrows , ncols = five_by_five_shape (len (subjids ))
421
+ fig , ax = plt .subplots (nrows , ncols )
422
+ for y in range (ncols ):
423
+ for x in range (nrows ):
424
+ try :
425
+ subjid = subjids [x * 5 + y ]
426
+ except IndexError as ie :
427
+ # No more subjects to render
428
+ break
391
429
individual = obs_df [obs_df .subjid == subjid ]
392
430
selected_param = individual [individual .param == param ]
393
- ax [x , y ].plot (selected_param .age , selected_param .measurement , marker = "." )
431
+ # Indexing varies by dimensionality, so simplify
432
+ if nrows > 1 :
433
+ tgt = ax [x , y ]
434
+ elif len (subjids ) == 1 :
435
+ tgt = ax
436
+ else :
437
+ tgt = ax [y ]
438
+ tgt .plot (selected_param .age , selected_param .measurement , marker = "." )
394
439
excluded_selected_param = selected_param [
395
440
selected_param .clean_value != "Include"
396
441
]
397
- ax [ x , y ] .scatter (
442
+ tgt .scatter (
398
443
excluded_selected_param .age ,
399
444
excluded_selected_param .measurement ,
400
445
c = "r" ,
@@ -411,22 +456,23 @@ def five_by_five_view(obs_df, subjids, param, wt_df, ht_df, bmi_df, linestyle):
411
456
& (percentile_df .age >= math .floor (individual .age .min ()))
412
457
& (percentile_df .age <= math .ceil (individual .age .max ()))
413
458
]
414
- ax [ x , y ] .plot (
459
+ tgt .plot (
415
460
percentile_window .age ,
416
461
percentile_window .P5 ,
417
462
color = "k" ,
418
463
linestyle = linestyle ,
419
464
zorder = 1 ,
420
465
)
421
- ax [ x , y ] .plot (
466
+ tgt .plot (
422
467
percentile_window .age ,
423
468
percentile_window .P95 ,
424
469
color = "k" ,
425
470
linestyle = linestyle ,
426
471
zorder = 1 ,
427
472
)
428
- ax [x , y ].set (title = subjid )
429
- fig .set_size_inches (20 , 12 )
473
+ tgt .set (title = subjid )
474
+ # Set size dynamically to average out about the same
475
+ fig .set_size_inches (4 * ncols , 2.4 * nrows )
430
476
return plt .tight_layout ()
431
477
432
478
@@ -465,7 +511,7 @@ def bmi_with_percentiles(merged_df, bmi_percentiles, subjid):
465
511
466
512
ax [1 ].set (xlabel = "age (y)" , ylabel = "BMI" , title = "BMI Cleaned" )
467
513
ax [1 ].grid ()
468
- return plt
514
+ plt . show ()
469
515
470
516
471
517
def param_with_percentiles (merged_df , subjid , param , wt_df , ht_df , bmi_df ):
@@ -499,7 +545,7 @@ def param_with_percentiles(merged_df, subjid, param, wt_df, ht_df, bmi_df):
499
545
500
546
ax [1 ].set (xlabel = "age (y)" , ylabel = "" , title = (param + " Cleaned" ))
501
547
ax [1 ].grid ()
502
- return plt
548
+ plt . show ()
503
549
504
550
505
551
def top_ten (
0 commit comments