%23%20%2F%2F%2F%20script%0A%23%20requires-python%20%3D%20%22%3E%3D3.12%22%0A%23%20dependencies%20%3D%20%5B%0A%23%20%20%20%20%20%22marimo%3E%3D0.24.0%22%2C%0A%23%20%20%20%20%20%22polars%3E%3D1.33.1%22%2C%0A%23%20%5D%0A%23%0A%23%20%5Btool.marimo-studio%5D%0A%23%20default%20%3D%20%22overview%22%0A%23%20preserve_session%20%3D%20false%0A%23%20runtime%20%3D%20%22server%22%0A%23%20runtimes%20%3D%20%5B%22server%22%2C%20%22wasm%22%2C%20%22zero-python%22%5D%0A%23%20show_cell_logs%20%3D%20false%0A%23%0A%23%20%5Btool.marimo-studio.cells%5D%0A%23%20%2F%2F%2F%0A%0Aimport%20marimo%0A%0A__generated_with%20%3D%20%220.24.2%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22%2C%20app_title%3D%22Rio%202016%20Athletes%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20introduction(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%20Rio%202016%20Athletes%0A%0A%20%20%20%20The%20Rio%202016%20roster%20records%2011%2C538%20athletes%20across%2028%20sports.%0A%20%20%20%20Participation%2C%20body%20profiles%2C%20and%20athlete%20medal%20awards%20are%20compared%20across%0A%20%20%20%20the%20full%20field.%0A%0A%20%20%20%20The%20roster%20mirrors%20the%20%5B%60flother%2Frio2016%60%5D(https%3A%2F%2Fgithub.com%2Fflother%2Frio2016)%0A%20%20%20%20dataset%20derived%20from%20the%20former%20Rio%202016%20site.%20DataHub%20publishes%20it%20under%20the%0A%20%20%20%20%5BODC-PDDL%201.0%5D(https%3A%2F%2Fopendatacommons.org%2Flicenses%2Fpddl%2F1-0%2F)%20license.%0A%20%20%20%20Height%20and%20weight%20contain%20missing%20values%2C%20and%20the%20source%20records%20sex%20with%0A%20%20%20%20two%20categories.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20io%0A%20%20%20%20import%20urllib.request%0A%20%20%20%20from%20datetime%20import%20date%0A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20polars%20as%20pl%0A%0A%20%20%20%20return%20date%2C%20io%2C%20mo%2C%20pl%2C%20urllib%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20data_context(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%20Athlete%20records%0A%0A%20%20%20%20Age%20is%20calculated%20on%20the%20opening%20date.%20Sport%2C%20nationality%2C%20and%20sex%20remain%0A%20%20%20%20categorical%20fields%2C%20while%20body%20measurements%20retain%20their%20reported%20units.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20load_athletes(date%2C%20io%2C%20pl%2C%20urllib)%3A%0A%20%20%20%20source_url%20%3D%20(%0A%20%20%20%20%20%20%20%20%22https%3A%2F%2Fraw.githubusercontent.com%2Fuwdata%2Fmosaic%2F%22%0A%20%20%20%20%20%20%20%20%2257f509c0f4140e66b0fb0f8b43172ebdd4dbbf6b%2Fdata%2Fathletes.csv%22%0A%20%20%20%20)%0A%20%20%20%20with%20urllib.request.urlopen(source_url)%20as%20response%3A%0A%20%20%20%20%20%20%20%20source%20%3D%20io.BytesIO(response.read())%0A%20%20%20%20athletes%20%3D%20(%0A%20%20%20%20%20%20%20%20pl.read_csv(source%2C%20try_parse_dates%3DTrue)%0A%20%20%20%20%20%20%20%20.with_columns(%0A%20%20%20%20%20%20%20%20%20%20%20%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20(pl.lit(date(2016%2C%208%2C%205))%20-%20pl.col(%22date_of_birth%22))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.dt.total_days()%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.truediv(365.2425)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.cast(pl.Float32)%0A%20%20%20%20%20%20%20%20%20%20%20%20).alias(%22age%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(pl.col(%22gold%22)%20%2B%20pl.col(%22silver%22)%20%2B%20pl.col(%22bronze%22))%0A%20%20%20%20%20%20%20%20%20%20%20%20.cast(pl.UInt8)%0A%20%20%20%20%20%20%20%20%20%20%20%20.alias(%22medal_awards%22)%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20.with_columns(%0A%20%20%20%20%20%20%20%20%20%20%20%20pl.col(%22id%22).cast(pl.UInt32)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20pl.col(%22nationality%22%2C%20%22sex%22%2C%20%22sport%22).cast(pl.Categorical)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20pl.col(%22height%22%2C%20%22weight%22).cast(pl.Float32)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20pl.col(%22gold%22%2C%20%22silver%22%2C%20%22bronze%22).cast(pl.UInt8)%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%20%20%20%20athletes.head(8)%0A%20%20%20%20return%20(athletes%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20participation_context(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%20Participation%0A%0A%20%20%20%20Select%20a%20sport%20to%20compare%20its%20field%20size%20and%20medal%20results%20with%20the%20complete%0A%20%20%20%20Games%20roster.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20sport_control(athletes%2C%20mo)%3A%0A%20%20%20%20sport%20%3D%20mo.ui.dropdown(%0A%20%20%20%20%20%20%20%20options%3D%5B%22All%20sports%22%2C%20*athletes%5B%22sport%22%5D.cast(str).unique().sort().to_list()%5D%2C%0A%20%20%20%20%20%20%20%20value%3D%22All%20sports%22%2C%0A%20%20%20%20%20%20%20%20label%3D%22Sport%22%2C%0A%20%20%20%20)%0A%20%20%20%20sport%0A%20%20%20%20return%20(sport%2C)%0A%0A%0A%40app.cell%0Adef%20participation(athletes%2C%20pl%2C%20sport)%3A%0A%20%20%20%20selected_athletes%20%3D%20(%0A%20%20%20%20%20%20%20%20athletes%0A%20%20%20%20%20%20%20%20if%20sport.value%20%3D%3D%20%22All%20sports%22%0A%20%20%20%20%20%20%20%20else%20athletes.filter(pl.col(%22sport%22).cast(str)%20%3D%3D%20sport.value)%0A%20%20%20%20)%0A%20%20%20%20athlete_summary%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22selection%22%3A%20sport.value%2C%0A%20%20%20%20%20%20%20%20%22athletes%22%3A%20selected_athletes.height%2C%0A%20%20%20%20%20%20%20%20%22nationalities%22%3A%20selected_athletes%5B%22nationality%22%5D.n_unique()%2C%0A%20%20%20%20%20%20%20%20%22sports%22%3A%20selected_athletes%5B%22sport%22%5D.n_unique()%2C%0A%20%20%20%20%20%20%20%20%22medalists%22%3A%20selected_athletes.filter(pl.col(%22medal_awards%22)%20%3E%200).height%2C%0A%20%20%20%20%7D%0A%20%20%20%20selected_roster%20%3D%20(%0A%20%20%20%20%20%20%20%20selected_athletes.sort(%5B%22medal_awards%22%2C%20%22name%22%5D%2C%20descending%3D%5BTrue%2C%20False%5D)%0A%20%20%20%20%20%20%20%20.select(%0A%20%20%20%20%20%20%20%20%20%20%20%20%22name%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22nationality%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22sport%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22sex%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22age%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22height%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22weight%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22gold%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22silver%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22bronze%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22medal_awards%22%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20.with_columns(%0A%20%20%20%20%20%20%20%20%20%20%20%20pl.col(%22age%22).cast(pl.Float64).round(1)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20pl.col(%22height%22).cast(pl.Float64).round(2)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20pl.col(%22weight%22).cast(pl.Float64).round(1)%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20.head(20)%0A%20%20%20%20)%0A%20%20%20%20selected_roster%0A%20%20%20%20return%20(athlete_summary%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20profile_context(mo)%3A%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%20Sport%20profiles%0A%0A%20%20%20%20Sport%20and%20sex%20summaries%20compare%20participation%2C%20body%20measurements%2C%20and%20medal%0A%20%20%20%20counts%20while%20retaining%20the%20observed%20sample%20sizes.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20analytical_tables(athletes%2C%20pl)%3A%0A%20%20%20%20_summary%20%3D%20athletes.select(%0A%20%20%20%20%20%20%20%20pl.len().alias(%22athletes%22)%2C%0A%20%20%20%20%20%20%20%20pl.col(%22nationality%22).n_unique().alias(%22nationalities%22)%2C%0A%20%20%20%20%20%20%20%20pl.col(%22sport%22).n_unique().alias(%22sports%22)%2C%0A%20%20%20%20%20%20%20%20(pl.col(%22sex%22).cast(pl.String)%20%3D%3D%20%22female%22).sum().alias(%22women%22)%2C%0A%20%20%20%20%20%20%20%20(pl.col(%22sex%22).cast(pl.String)%20%3D%3D%20%22male%22).sum().alias(%22men%22)%2C%0A%20%20%20%20%20%20%20%20(pl.col(%22medal_awards%22)%20%3E%200).sum().alias(%22medalists%22)%2C%0A%20%20%20%20%20%20%20%20pl.col(%22medal_awards%22).sum().alias(%22medal_awards%22)%2C%0A%20%20%20%20%20%20%20%20(pl.col(%22medal_awards%22)%20%3E%201).sum().alias(%22multi_medalists%22)%2C%0A%20%20%20%20%20%20%20%20pl.col(%22age%22).cast(pl.Float64).median().round(1).alias(%22median_age%22)%2C%0A%20%20%20%20%20%20%20%20pl.col(%22height%22).cast(pl.Float64).median().round(2).alias(%22median_height%22)%2C%0A%20%20%20%20%20%20%20%20pl.col(%22weight%22).median().round(0).cast(pl.Int64).alias(%22median_weight%22)%2C%0A%20%20%20%20%20%20%20%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20(pl.col(%22height%22).fill_null(0)%20%3E%200)%0A%20%20%20%20%20%20%20%20%20%20%20%20%26%20(pl.col(%22weight%22).fill_null(0)%20%3E%200)%0A%20%20%20%20%20%20%20%20%20%20%20%20%26%20(pl.col(%22age%22).fill_null(0)%20%3E%200)%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20.sum()%0A%20%20%20%20%20%20%20%20.alias(%22profile_count%22)%2C%0A%20%20%20%20).row(0%2C%20named%3DTrue)%0A%20%20%20%20_sport_ranking%20%3D%20(%0A%20%20%20%20%20%20%20%20athletes.group_by(%22sport%22)%0A%20%20%20%20%20%20%20%20.agg(%0A%20%20%20%20%20%20%20%20%20%20%20%20pl.len().alias(%22athletes%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20(pl.col(%22medal_awards%22)%20%3E%200).sum().alias(%22medalists%22)%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20.sort(%22athletes%22%2C%20%22sport%22%2C%20descending%3D%5BTrue%2C%20False%5D)%0A%20%20%20%20)%0A%20%20%20%20_largest_sport%20%3D%20_sport_ranking.row(0%2C%20named%3DTrue)%0A%20%20%20%20games_summary%20%3D%20%7B%0A%20%20%20%20%20%20%20%20**_summary%2C%0A%20%20%20%20%20%20%20%20%22largest_sport%22%3A%20str(_largest_sport%5B%22sport%22%5D)%2C%0A%20%20%20%20%20%20%20%20%22largest_count%22%3A%20_largest_sport%5B%22athletes%22%5D%2C%0A%20%20%20%20%20%20%20%20%22leading_sports%22%3A%20_sport_ranking.head(3)%5B%22sport%22%5D.cast(str).to_list()%2C%0A%20%20%20%20%20%20%20%20%22medalist_share_percent%22%3A%20round(%0A%20%20%20%20%20%20%20%20%20%20%20%20_summary%5B%22medalists%22%5D%20%2F%20_summary%5B%22athletes%22%5D%20*%20100%2C%0A%20%20%20%20%20%20%20%20%20%20%20%201%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%22profile_share_percent%22%3A%20round(%0A%20%20%20%20%20%20%20%20%20%20%20%20_summary%5B%22profile_count%22%5D%20%2F%20_summary%5B%22athletes%22%5D%20*%20100%2C%0A%20%20%20%20%20%20%20%20%20%20%20%201%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%7D%0A%20%20%20%20sport_profiles%20%3D%20(%0A%20%20%20%20%20%20%20%20athletes.group_by(%22sport%22%2C%20%22sex%22)%0A%20%20%20%20%20%20%20%20.agg(%0A%20%20%20%20%20%20%20%20%20%20%20%20pl.len().alias(%22athletes%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20pl.col(%22height%22).mean().alias(%22mean_height%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20pl.col(%22weight%22).mean().alias(%22mean_weight%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20pl.col(%22medal_awards%22).sum().alias(%22medal_awards%22)%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20.sort(%22athletes%22%2C%20%22sport%22%2C%20descending%3D%5BTrue%2C%20False%5D)%0A%20%20%20%20)%0A%20%20%20%20athlete_facts%20%3D%20athletes.select(%0A%20%20%20%20%20%20%20%20%22id%22%2C%0A%20%20%20%20%20%20%20%20%22name%22%2C%0A%20%20%20%20%20%20%20%20%22nationality%22%2C%0A%20%20%20%20%20%20%20%20%22sex%22%2C%0A%20%20%20%20%20%20%20%20%22age%22%2C%0A%20%20%20%20%20%20%20%20%22height%22%2C%0A%20%20%20%20%20%20%20%20%22weight%22%2C%0A%20%20%20%20%20%20%20%20%22sport%22%2C%0A%20%20%20%20%20%20%20%20%22gold%22%2C%0A%20%20%20%20%20%20%20%20%22silver%22%2C%0A%20%20%20%20%20%20%20%20%22bronze%22%2C%0A%20%20%20%20%20%20%20%20%22medal_awards%22%2C%0A%20%20%20%20)%0A%20%20%20%20top_sports%20%3D%20_sport_ranking.head(10)%0A%20%20%20%20sport_profiles.head(12)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20data_quality(athletes%2C%20pl)%3A%0A%20%20%20%20quality%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22rows%22%3A%20athletes.height%2C%0A%20%20%20%20%20%20%20%20%22missing_height%22%3A%20athletes.select(pl.col(%22height%22).is_null().sum()).item()%2C%0A%20%20%20%20%20%20%20%20%22missing_weight%22%3A%20athletes.select(pl.col(%22weight%22).is_null().sum()).item()%2C%0A%20%20%20%20%20%20%20%20%22biographies%22%3A%20athletes.filter(pl.col(%22info%22).is_not_null()).height%2C%0A%20%20%20%20%20%20%20%20%22source%22%3A%20%22Rio%202016%20athlete%20roster%22%2C%0A%20%20%20%20%7D%0A%20%20%20%20return%20(quality%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20conclusion(athlete_summary%2C%20mo%2C%20quality)%3A%0A%20%20%20%20mo.md(f%22%22%22%0A%20%20%20%20%23%23%20Reading%20the%20roster%0A%0A%20%20%20%20**%7Bathlete_summary%5B%22selection%22%5D%7D**%20contains%0A%20%20%20%20**%7Bathlete_summary%5B%22athletes%22%5D%3A%2C%7D%20athletes**%20from%0A%20%20%20%20**%7Bathlete_summary%5B%22nationalities%22%5D%7D%20nationalities**.%20The%20source%20omits%0A%20%20%20%20height%20for%20**%7Bquality%5B%22missing_height%22%5D%3A%2C%7D**%20athletes%20and%20weight%20for%0A%20%20%20%20**%7Bquality%5B%22missing_weight%22%5D%3A%2C%7D**%2C%20so%20body-profile%20comparisons%20retain%0A%20%20%20%20their%20observed%20sample%20sizes.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
ceb273996395ae1f0ccb8f46fa4f3231