@@ -393,39 +393,50 @@ def plot_frequencies_time_series(
393393 # Extract variant labels.
394394 variant_labels = ds ["variant_label" ].values
395395
396+ # Check if CI variables are available.
397+ has_ci = "event_frequency_ci_low" in ds
398+
396399 # Build a long-form dataframe from the dataset.
397400 dfs = []
398401 for cohort in df_cohorts .itertuples ():
399402 ds_cohort = ds .isel (cohorts = cohort .Index )
400- df = pd .DataFrame (
401- {
402- "taxon" : cohort .taxon ,
403- "area" : cohort .area ,
404- "date" : cohort .period_start ,
405- "period" : str (
406- cohort .period
407- ), # use string representation for hover label
408- "sample_size" : cohort .size ,
409- "variant" : variant_labels ,
410- "count" : ds_cohort ["event_count" ].values ,
411- "nobs" : ds_cohort ["event_nobs" ].values ,
412- "frequency" : ds_cohort ["event_frequency" ].values ,
413- "frequency_ci_low" : ds_cohort ["event_frequency_ci_low" ].values ,
414- "frequency_ci_upp" : ds_cohort ["event_frequency_ci_upp" ].values ,
415- }
416- )
403+ cohort_data = {
404+ "taxon" : cohort .taxon ,
405+ "area" : cohort .area ,
406+ "date" : cohort .period_start ,
407+ "period" : str (
408+ cohort .period
409+ ), # use string representation for hover label
410+ "sample_size" : cohort .size ,
411+ "variant" : variant_labels ,
412+ "count" : ds_cohort ["event_count" ].values ,
413+ "nobs" : ds_cohort ["event_nobs" ].values ,
414+ "frequency" : ds_cohort ["event_frequency" ].values ,
415+ }
416+ if has_ci :
417+ cohort_data ["frequency_ci_low" ] = ds_cohort [
418+ "event_frequency_ci_low"
419+ ].values
420+ cohort_data ["frequency_ci_upp" ] = ds_cohort [
421+ "event_frequency_ci_upp"
422+ ].values
423+ df = pd .DataFrame (cohort_data )
417424 dfs .append (df )
418425 df_events = pd .concat (dfs , axis = 0 ).reset_index (drop = True )
419426
420427 # Remove events with no observations.
421428 df_events = df_events .query ("nobs > 0" ).copy ()
422429
423- # Calculate error bars.
424- frq = df_events ["frequency" ]
425- frq_ci_low = df_events ["frequency_ci_low" ]
426- frq_ci_upp = df_events ["frequency_ci_upp" ]
427- df_events ["frequency_error" ] = frq_ci_upp - frq
428- df_events ["frequency_error_minus" ] = frq - frq_ci_low
430+ # Calculate error bars if CI data is available.
431+ error_y_args = {}
432+ if has_ci :
433+ frq = df_events ["frequency" ]
434+ frq_ci_low = df_events ["frequency_ci_low" ]
435+ frq_ci_upp = df_events ["frequency_ci_upp" ]
436+ df_events ["frequency_error" ] = frq_ci_upp - frq
437+ df_events ["frequency_error_minus" ] = frq - frq_ci_low
438+ error_y_args ["error_y" ] = "frequency_error"
439+ error_y_args ["error_y_minus" ] = "frequency_error_minus"
429440
430441 # Make a plot.
431442 fig = px .line (
@@ -434,8 +445,7 @@ def plot_frequencies_time_series(
434445 facet_row = "area" ,
435446 x = "date" ,
436447 y = "frequency" ,
437- error_y = "frequency_error" ,
438- error_y_minus = "frequency_error_minus" ,
448+ ** error_y_args ,
439449 color = "variant" ,
440450 markers = True ,
441451 hover_name = "variant" ,
@@ -515,19 +525,19 @@ def plot_frequencies_map_markers(
515525 variant_label = variant
516526
517527 # Convert to a dataframe for convenience.
518- df_markers = ds_variant [
519- [
520- "cohort_taxon " ,
521- "cohort_area " ,
522- "cohort_period " ,
523- "cohort_lat_mean " ,
524- "cohort_lon_mean " ,
525- "cohort_size " ,
526- "event_frequency" ,
527- "event_frequency_ci_low" ,
528- "event_frequency_ci_upp" ,
529- ]
530- ].to_dataframe ()
528+ cols = [
529+ "cohort_taxon" ,
530+ "cohort_area " ,
531+ "cohort_period " ,
532+ "cohort_lat_mean " ,
533+ "cohort_lon_mean " ,
534+ "cohort_size " ,
535+ "event_frequency " ,
536+ ]
537+ has_ci = "event_frequency_ci_low" in ds
538+ if has_ci :
539+ cols += [ "event_frequency_ci_low" , "event_frequency_ci_upp" ]
540+ df_markers = ds_variant [ cols ].to_dataframe ()
531541
532542 # Select data matching taxon and period parameters.
533543 df_markers = df_markers .loc [
@@ -557,8 +567,11 @@ def plot_frequencies_map_markers(
557567 Area: { x .cohort_area } <br/>
558568 Period: { x .cohort_period } <br/>
559569 Sample size: { x .cohort_size } <br/>
560- Frequency: { x .event_frequency :.0%}
561- (95% CI: { x .event_frequency_ci_low :.0%} - { x .event_frequency_ci_upp :.0%} )
570+ Frequency: { x .event_frequency :.0%} """
571+ if has_ci :
572+ popup_html += f"""
573+ (95% CI: { x .event_frequency_ci_low :.0%} - { x .event_frequency_ci_upp :.0%} )"""
574+ popup_html += """
562575 """
563576 marker .popup = ipyleaflet .Popup (
564577 child = ipywidgets .HTML (popup_html ),
0 commit comments