Hide code cell source
%load_ext watermark
import pandas as pd
import numpy as np
from typing import Type, Optional, Callable
from typing import List, Dict, Union, Tuple
from myst_nb import glue

# from review_methods_tests import collect_vitals, find_missing, find_missing_loc_dates
# from review_methods_tests import make_a_summary

import matplotlib.pyplot as plt
import matplotlib as mpl
import matplotlib.colors
from matplotlib.colors import LinearSegmentedColormap, ListedColormap

import setvariables as conf_
import reportclass as r_class

Cumulative reports#

Cumulative reports display the test statistic of sample results between elements of a geographic or administrative region. Cumulative reports are visualized with heat maps. The starting point for cumulative reports is a valid ReportClass object. The granularity of the results are at a minimum the municipal level. The lowest recognized administrative unit.

Note

The GPT assistant is being trained to accept the following methods and commands as key word arguments. So that different clients have access to a standardized output for tabular data.

A top level description#

A short and detailed summary of the report can be created by synthesising three tables from the ReportClass.

header = a_report.a_short_description
components = a_report.the_number_of_attributes_in_a_feature('feature_type')
comp_summary = a_report.summarize_feature_labels(feature='feature_type')
# default arguments that define the most common objects
# this assumes that the columns quantity and fail rate exist
mc_criteria_one = {
        
        'column': 'quantity',
        'val': 5
    }

mc_criteria_two = {
        'column': 'fail rate',
        'val': 0.6
    }

Synthesising table data#

Calling a_report.a_short_description, a_report.the_number_of_attributes_in_a_feature and a_report.summarize_feature_labels provides the data for a top level description and a comparison of the sample total pcs/m of different attributes or features in the data.

a_report.short_description

  Report
type canton
name Bern
start 2015-11-01
end 2021-12-31
samples 196
quantity 13759

a_report.the_number_of_attributes_in_a_feature

  city feature_name samples
l 14 4 99
p 1 1 1
r 14 6 96

a_report.summarize_feature_labels

  pcs_m
label l p r
25% 0,89 2,81 0,20
50% 1,83 2,81 0,73
75% 3,62 2,81 1,59
count 99 1 96
max 14,80 2,81 7,92
mean 2,80 2,81 1,21
min 0,16 2,81 0,02
std 2,64 nan 1,46
total 9'902 169 3'688

Text generated from summary tables

There were 13'759 objects identified in the period between 2015-11-01 and 2021-12-31 in the canton of Bern. In total, 196 samples were recorded, 99 on lakes, 96 at rivers and 1 at parks. The lake samples were recorded from 14 cities and 14 for rivers. The alpes only had one representative. The one sample from the alpes is very close to what would be expected from a lake sample. The rivers have the lowest pcs/m.

Note

Defining summary texts

Summary texts can be composed in many different ways. There is a role for LLMs at this part of the report. At the very least helping find other locations that fall within the same values. Providing context to either the geographic or adminstrative boundaries.

The person establishing the report can manage the content creation from the LLM or there can be a shared prompt for all reports.

Features#

The features are defined by the setvariables.geo_h variable. This is a list of the different boundaries present within the data. For example, a survey location can belong to a river, lake or park. The administrative boundaries are set by hierarchy in the geo_h variable, the default is set to canton, city.

Therefore, for any subset of data there will be different features present. The attribute report_class.available_features lists the different boundaries that exist in the selected data.

['parent_boundary', 'feature_type', 'feature_name', 'city']

Feature names#

The feature names are the labels for the features that are in the report data. Subreports can be generated for each feautre. The prior example is bielersee. In IQAASL the selection was {'feature_type':'l'} for most of the report. The selection {'feature_type':'r'} was the last section of each survey area report and {'feature_type':'p'} had its own chapter (The Alps and Jura).

my_labels = a_report.feature_labels()

The name and type of each feature available in the report can be accessed by calling a_report.feature_labels(). The keys() method will list the available feature types, .values() will list the feature names and the city names for all locations with samples.

Hide code cell source
# the lakes 
my_labels = a_report.feature_labels()
print(my_labels['l']['feature_name'])
['aare' 'bielersee' 'brienzersee' 'thunersee']
Hide code cell source
# in the same way the name of the parks and the cities in those parks can be indentified
print(my_labels['p']['feature_name'])
['alpes-bernoises']
Hide code cell source
# the same for rivers
print(my_labels['r']['feature_name'])
['aare' 'aarenidau-buren-kanal' 'emme' 'langeten' 'schuss' 'zulg']

Comparing results#

Once the the objects of interest are identified (criteria) they can be compared accross the diferent feature_types and labels.

t = a_cumulative_report(w_df[w_df.code.isin(most_common.index)], feature_name='feature_type', object_column='code')
translated_and_style_for_display(t, a_report.lang_maps[a_report.language], a_report.language, gradient=True)

For example the most common objects are found at different densitiies depending on the feature type.

  Cumulé Lac Parc Rivière
Autres Textiles 0,00 0,00 0,00 0,00
Verre Brisé 0,00 0,02 0,00 0,00
Fragments De Verre Ou De Céramique > 2,5 Cm 0,00 0,00 0,02 0,00
Mégots Et Filtres À Cigarettes 0,07 0,10 1,25 0,05
Emballages De Bonbons, De Snacks 0,04 0,08 0,72 0,01
Bâche, Feuille Plastique Industrielle 0,03 0,13 0,00 0,00
Mousse De Plastique Pour L'Isolation Thermique 0,00 0,00 0,00 0,00
Emballage Plastique Non Alimentaire 0,00 0,00 0,00 0,00
Couvercles En Plastique Bouteille: G21, G22, G23, G24 0,00 0,03 0,04 0,00
Fragments De Polystyrène Expansé: G76, G81, G82, G83 0,00 0,07 0,00 0,00
Fragments De Plastique: G80, G79, G78, G75 0,09 0,32 0,12 0,00

Alternate object groups groupname#

If the column has other labeled values for object identification it can be used to aggregate results for each sample id. Here we consider groupname, there is more than one object in a group. They represent use cases.

t = a_cumulative_report(w_df, feature_name='feature_type', object_column='groupname')
translated_and_style_for_display(t, a_report.lang_maps[a_report.language], a_report.language, gradient=True)

For example the different use cases are found at different densitiies depending on the feature type.

  Cumulé Lac Parc Rivière
Agriculture 0,06 0,14 0,00 0,02
Nourriture Et Boissons 0,21 0,28 0,95 0,11
Infrastructures 0,11 0,21 0,07 0,05
Micro-Plastiques (< 5Mm) 0,00 0,00 0,21 0,00
Emballage Non Alimentaire 0,06 0,07 0,00 0,05
Articles Personnels 0,04 0,04 0,04 0,02
Morceaux De Plastique 0,09 0,32 0,12 0,00
Loisirs 0,04 0,10 0,06 0,00
Tabac 0,11 0,14 1,30 0,06
Non Classé 0,02 0,03 0,02 0,00
Eaux Usées 0,00 0,03 0,04 0,00

By Survey area or parent_boundary#

There are two parent boundaries in Bern, the Alpes and Jura and the Aare river basin.

  Aare Cumulé Alpes Et Jura
Autres Textiles 0,00 0,00 0,00
Verre Brisé 0,00 0,00 0,00
Fragments De Verre Ou De Céramique > 2,5 Cm 0,00 0,00 0,02
Mégots Et Filtres À Cigarettes 0,07 0,07 1,25
Emballages De Bonbons, De Snacks 0,04 0,04 0,72
Bâche, Feuille Plastique Industrielle 0,03 0,03 0,00
Mousse De Plastique Pour L'Isolation Thermique 0,00 0,00 0,00
Emballage Plastique Non Alimentaire 0,00 0,00 0,00
Couvercles En Plastique Bouteille: G21, G22, G23, G24 0,00 0,00 0,04
Fragments De Polystyrène Expansé: G76, G81, G82, G83 0,00 0,00 0,00
Fragments De Plastique: G80, G79, G78, G75 0,09 0,09 0,12

By feature_name:#

There are many different features (rivers, lakes and parks) in Bern.

  Aare Aarenidau-Buren-Kanal Cumulé Alpes-Bernoises Bielersee Brienzersee Emme Langeten Schuss Thunersee Zulg
Autres Textiles 0,00 0,00 0,00 0,00 0,00 0,00 0,00 0,00 0,00 0,00 0,00
Verre Brisé 0,00 0,03 0,00 0,00 0,02 0,00 0,00 0,00 0,15 0,02 0,00
Fragments De Verre Ou De Céramique > 2,5 Cm 0,00 0,00 0,00 0,02 0,00 0,00 0,00 0,29 0,00 0,00 0,00
Mégots Et Filtres À Cigarettes 0,03 0,28 0,07 1,25 0,06 0,06 0,11 0,07 0,27 0,14 0,00
Emballages De Bonbons, De Snacks 0,00 0,01 0,04 0,72 0,16 0,27 0,00 0,07 0,08 0,03 0,06
Bâche, Feuille Plastique Industrielle 0,01 0,00 0,03 0,00 0,30 0,47 0,00 0,00 0,00 0,04 0,00
Mousse De Plastique Pour L'Isolation Thermique 0,00 0,00 0,00 0,00 0,00 0,05 0,00 0,00 0,00 0,00 0,00
Emballage Plastique Non Alimentaire 0,00 0,00 0,00 0,00 0,00 0,00 0,00 0,00 0,03 0,00 0,00
Couvercles En Plastique Bouteille: G21, G22, G23, G24 0,00 0,00 0,00 0,04 0,06 0,11 0,00 0,00 0,01 0,00 0,00
Fragments De Polystyrène Expansé: G76, G81, G82, G83 0,00 0,00 0,00 0,00 0,05 0,15 0,00 0,00 0,00 0,08 0,00
Fragments De Plastique: G80, G79, G78, G75 0,00 0,04 0,09 0,12 0,62 0,53 0,00 0,29 0,06 0,11 0,00

By city:#

  Beatenberg Belp Bern Biel/Bienne Brienz (Be) Brügg Burgdorf Bönigen Erlach Gals Grindelwald Kallnach Köniz Langenthal Ligerz Lüscherz Muri Bei Bern Nidau Port Rubigen Spiez Steffisburg Thun Unterseen Utzenstorf Vinelz Walperswil Cumulé
Autres Textiles 0,00 0,00 0,00 0,00 0,00 0,00 0,04 0,00 0,00 0,00 0,00 0,00 0,00 0,00 0,00 0,00 0,04 0,00 0,00 0,00 0,00 0,00 0,00 0,00 0,00 0,00 0,00 0,00
Verre Brisé 0,02 0,00 0,00 0,07 0,00 0,36 0,00 0,00 0,02 0,07 0,00 0,00 0,00 0,00 0,20 0,20 0,00 0,12 0,01 0,00 0,00 0,00 0,00 0,02 0,00 0,00 0,00 0,00
Fragments De Verre Ou De Céramique > 2,5 Cm 0,00 0,00 0,00 0,00 0,00 0,00 0,00 0,00 0,00 0,00 0,02 0,00 0,14 0,29 0,00 0,00 0,21 0,00 0,00 0,00 0,06 0,00 0,00 0,00 0,00 0,00 0,00 0,00
Mégots Et Filtres À Cigarettes 0,55 0,00 0,02 1,15 0,00 0,28 0,07 1,19 0,44 0,09 1,25 0,00 0,59 0,07 1,00 0,04 0,54 0,00 0,63 0,06 0,08 0,00 0,23 0,55 0,11 0,04 0,00 0,07
Emballages De Bonbons, De Snacks 0,12 0,00 0,00 0,34 0,39 0,00 0,02 0,06 0,05 0,12 0,72 0,08 0,05 0,07 0,57 0,02 0,06 0,60 0,07 0,11 0,00 0,06 0,09 0,11 0,00 0,11 0,00 0,04
Bâche, Feuille Plastique Industrielle 0,03 0,02 0,02 0,18 0,67 0,00 0,22 0,15 0,00 0,03 0,00 0,32 0,00 0,00 0,40 0,05 0,00 0,40 0,00 0,00 0,00 0,00 0,13 0,13 0,00 0,79 0,00 0,03
Mousse De Plastique Pour L'Isolation Thermique 0,42 0,00 0,00 0,04 0,02 0,00 0,00 0,10 0,00 0,00 0,00 0,00 0,00 0,00 0,40 0,00 0,00 0,00 0,01 0,00 0,00 0,00 0,00 0,09 0,00 0,00 0,19 0,00
Emballage Plastique Non Alimentaire 0,04 0,00 0,00 0,09 0,06 0,00 0,11 0,00 0,02 0,00 0,00 0,01 0,00 0,00 0,14 0,00 0,00 0,24 0,04 0,00 0,00 0,00 0,00 0,03 0,00 0,00 0,00 0,00
Couvercles En Plastique Bouteille: G21, G22, G23, G24 0,12 0,00 0,00 0,09 0,28 0,00 0,00 0,04 0,06 0,00 0,04 0,06 0,00 0,00 0,00 0,00 0,04 0,04 0,03 0,06 0,00 0,00 0,02 0,04 0,00 0,06 0,03 0,00
Fragments De Polystyrène Expansé: G76, G81, G82, G83 0,18 0,00 0,00 0,18 0,22 0,00 0,00 0,07 0,02 0,00 0,00 0,14 0,00 0,00 0,00 0,00 0,00 0,04 0,00 0,00 0,05 0,00 0,16 0,11 0,00 0,03 0,00 0,00
Fragments De Plastique: G80, G79, G78, G75 0,44 0,00 0,01 0,50 0,39 0,03 0,02 1,01 0,49 0,28 0,12 0,12 0,07 0,29 1,00 0,14 0,12 0,64 0,22 0,00 0,04 0,00 0,24 0,21 0,00 1,00 0,00 0,09

Note

The available features are column names of the survey data. They represent the different geopraphic or administrative boundaries in the selected report data.

  • parent_boundary is a geographic boundary such as a river basin or a category such as mountains

  • feature_type designates whether the location is at a river, lake or park

  • feature_name is the name of the river, lake or park