@@ -396,39 +396,50 @@ def plot_frequencies_time_series(
396396 # Extract variant labels.
397397 variant_labels = ds ["variant_label" ].values
398398
399+ # Check if CI variables are available.
400+ has_ci = "event_frequency_ci_low" in ds
401+
399402 # Build a long-form dataframe from the dataset.
400403 dfs = []
401404 for cohort in df_cohorts .itertuples ():
402405 ds_cohort = ds .isel (cohorts = cohort .Index )
403- df = pd .DataFrame (
404- {
405- "taxon" : cohort .taxon ,
406- "area" : cohort .area ,
407- "date" : cohort .period_start ,
408- "period" : str (
409- cohort .period
410- ), # use string representation for hover label
411- "sample_size" : cohort .size ,
412- "variant" : variant_labels ,
413- "count" : ds_cohort ["event_count" ].values ,
414- "nobs" : ds_cohort ["event_nobs" ].values ,
415- "frequency" : ds_cohort ["event_frequency" ].values ,
416- "frequency_ci_low" : ds_cohort ["event_frequency_ci_low" ].values ,
417- "frequency_ci_upp" : ds_cohort ["event_frequency_ci_upp" ].values ,
418- }
419- )
406+ cohort_data = {
407+ "taxon" : cohort .taxon ,
408+ "area" : cohort .area ,
409+ "date" : cohort .period_start ,
410+ "period" : str (
411+ cohort .period
412+ ), # use string representation for hover label
413+ "sample_size" : cohort .size ,
414+ "variant" : variant_labels ,
415+ "count" : ds_cohort ["event_count" ].values ,
416+ "nobs" : ds_cohort ["event_nobs" ].values ,
417+ "frequency" : ds_cohort ["event_frequency" ].values ,
418+ }
419+ if has_ci :
420+ cohort_data ["frequency_ci_low" ] = ds_cohort [
421+ "event_frequency_ci_low"
422+ ].values
423+ cohort_data ["frequency_ci_upp" ] = ds_cohort [
424+ "event_frequency_ci_upp"
425+ ].values
426+ df = pd .DataFrame (cohort_data )
420427 dfs .append (df )
421428 df_events = pd .concat (dfs , axis = 0 ).reset_index (drop = True )
422429
423430 # Remove events with no observations.
424431 df_events = df_events .query ("nobs > 0" ).copy ()
425432
426- # Calculate error bars.
427- frq = df_events ["frequency" ]
428- frq_ci_low = df_events ["frequency_ci_low" ]
429- frq_ci_upp = df_events ["frequency_ci_upp" ]
430- df_events ["frequency_error" ] = frq_ci_upp - frq
431- df_events ["frequency_error_minus" ] = frq - frq_ci_low
433+ # Calculate error bars if CI data is available.
434+ error_y_args = {}
435+ if has_ci :
436+ frq = df_events ["frequency" ]
437+ frq_ci_low = df_events ["frequency_ci_low" ]
438+ frq_ci_upp = df_events ["frequency_ci_upp" ]
439+ df_events ["frequency_error" ] = frq_ci_upp - frq
440+ df_events ["frequency_error_minus" ] = frq - frq_ci_low
441+ error_y_args ["error_y" ] = "frequency_error"
442+ error_y_args ["error_y_minus" ] = "frequency_error_minus"
432443
433444 # Make a plot.
434445 fig = px .line (
@@ -437,8 +448,7 @@ def plot_frequencies_time_series(
437448 facet_row = "area" ,
438449 x = "date" ,
439450 y = "frequency" ,
440- error_y = "frequency_error" ,
441- error_y_minus = "frequency_error_minus" ,
451+ ** error_y_args ,
442452 color = "variant" ,
443453 markers = True ,
444454 hover_name = "variant" ,
@@ -518,19 +528,19 @@ def plot_frequencies_map_markers(
518528 variant_label = variant
519529
520530 # Convert to a dataframe for convenience.
521- df_markers = ds_variant [
522- [
523- "cohort_taxon " ,
524- "cohort_area " ,
525- "cohort_period " ,
526- "cohort_lat_mean " ,
527- "cohort_lon_mean " ,
528- "cohort_size " ,
529- "event_frequency" ,
530- "event_frequency_ci_low" ,
531- "event_frequency_ci_upp" ,
532- ]
533- ].to_dataframe ()
531+ cols = [
532+ "cohort_taxon" ,
533+ "cohort_area " ,
534+ "cohort_period " ,
535+ "cohort_lat_mean " ,
536+ "cohort_lon_mean " ,
537+ "cohort_size " ,
538+ "event_frequency " ,
539+ ]
540+ has_ci = "event_frequency_ci_low" in ds
541+ if has_ci :
542+ cols += [ "event_frequency_ci_low" , "event_frequency_ci_upp" ]
543+ df_markers = ds_variant [ cols ].to_dataframe ()
534544
535545 # Select data matching taxon and period parameters.
536546 df_markers = df_markers .loc [
@@ -560,8 +570,11 @@ def plot_frequencies_map_markers(
560570 Area: { x .cohort_area } <br/>
561571 Period: { x .cohort_period } <br/>
562572 Sample size: { x .cohort_size } <br/>
563- Frequency: { x .event_frequency :.0%}
564- (95% CI: { x .event_frequency_ci_low :.0%} - { x .event_frequency_ci_upp :.0%} )
573+ Frequency: { x .event_frequency :.0%} """
574+ if has_ci :
575+ popup_html += f"""
576+ (95% CI: { x .event_frequency_ci_low :.0%} - { x .event_frequency_ci_upp :.0%} )"""
577+ popup_html += """
565578 """
566579 marker .popup = ipyleaflet .Popup (
567580 child = ipywidgets .HTML (popup_html ),
0 commit comments