Skip to content

Commit

Permalink
add universal functions from util
Browse files Browse the repository at this point in the history
  • Loading branch information
torave committed Apr 20, 2025
1 parent 8bb8ecd commit ef954f3
Showing 1 changed file with 26 additions and 121 deletions.
147 changes: 26 additions & 121 deletions notebooks/notebook_one_day_data.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@
" \n",
" return date_input, [ts[0] for ts in timestamps]\n",
"\n",
"date, timestamps = get_unix_timestamps_for_day()\n",
"\n"
"date, timestamps = get_unix_timestamps_for_day()"
]
},
{
Expand Down Expand Up @@ -107,7 +106,6 @@
"# Start_date is the first timestamp, end_date is the last\n",
"start_date, end_date = timestamps[0], timestamps[-1]\n",
"\n",
"\n",
"# Stores the values in the variables\n",
"weather_data, folder = fetch_data(start_date, end_date, city_name)"
]
Expand Down Expand Up @@ -147,7 +145,7 @@
"source": [
"### Lese fra fil\n",
"\n",
"Henter opp data lagret i filen, lagd over, og skriver ut lesbart ved hjelp av pandas"
"Ved hjelp av funksjonen `extract_city_df` fjernes unødvendige kolonner, og dataen blir normalisert for lettere lesbarhet."
]
},
{
Expand Down Expand Up @@ -209,10 +207,10 @@
"plt.figure(figsize=(12, 6))\n",
"\n",
"# Scatter plot for each temperature reading\n",
"plt.scatter(x_axis, temp, color='tab:green', label='Temperaturmålinger', alpha=0.6)\n",
"plt.scatter(x_axis, temp, color='tab:red', label='Temperaturmålinger', alpha=0.7)\n",
"\n",
"# Add a horizontal line for the mean temperature\n",
"plt.axhline(y=temp_mean, color='green', linestyle='--', label=f'Gj.snitt {temp_mean}°C')\n",
"plt.axhline(y=temp_mean, color='tab:red', linestyle=\"dashed\", label=f'Gj.snitt {temp_mean}°C')\n",
"\n",
"# Get the current axis and store it as ax\n",
"ax = plt.gca()\n",
Expand Down Expand Up @@ -247,8 +245,11 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Visualiserer nedbør\n",
"Ved hjelp av matplotlib visualiserer vi nedbør for ønsket dag."
"### Sjekker eksitensen av kolonner\n",
"\n",
"Gjennom prosjektet har vi oppdaget at kolonnene som ofte mangler er 'rain.1h' og 'snow.1h'. Derfor importerer vi funksjonen `ensure_column` som tar inn dataframen og kolonnene vi vil sjekke. Dersom kolonnene ikke eksisterer blir de lagt til og fylt med 'NaN' før dataframen returneres.\n",
"\n",
"Sjekker også for kolonnene 'wind.gust' og 'wind.speed' da de skal brukes til plotting senere."
]
},
{
Expand All @@ -257,76 +258,15 @@
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"import matplotlib.dates as mdates\n",
"import numpy as np\n",
"\n",
"from my_package.util import ensure_rain_column\n",
"from my_package.util import ensure_snow_column\n",
"\n",
"x_axis = df.index\n",
"\n",
"# Checks if the rain is a value, it will not be if it is no rain and then cause a KeyError\n",
"try:\n",
" rain = df['rain.1h']\n",
"\n",
"# If no rain, make the rain column and fill it with NaN\n",
"except KeyError:\n",
" df = ensure_rain_column(df)\n",
"from my_package.util import ensure_column\n",
"\n",
"# Checks if the snow is a value, it will not be if it is no rain and then cause a KeyError\n",
"try:\n",
" snow = df['snow.1h']\n",
"# The columns we want to check if exsist\n",
"columns_to_ensure = ['rain.1h', 'snow.1h', 'wind.speed', 'wind.gust']\n",
"\n",
"# If no snow, make the snow column and fill it with NaN\n",
"except KeyError:\n",
" df = ensure_snow_column(df)\n",
"# Runs the function with wanted colummns\n",
"df = ensure_column(df, columns_to_ensure)\n",
"\n",
"# Choose the width and height of the plot\n",
"plt.figure(figsize=(15, 6))\n",
"\n",
"# Check with rain, will cause NameError if the try/except over fails\n",
"try:\n",
" plt.bar(x_axis, rain, width=0.02, alpha=0.5, color='tab:blue', label='rain')\n",
"except: NameError\n",
"\n",
"# Check with snow, will cause NameError if the try/except over fails\n",
"try: \n",
" plt.bar(x_axis, snow, width=0.02, alpha=0.5, color='tab:grey', label='snow')\n",
"except: NameError\n",
"\n",
"# Get the current axsis, and store it as ax\n",
"ax = plt.gca()\n",
"\n",
"# Use the current ax, to get a tick-mark on the x_axis for each hour, and print like \"HH:MM\"\n",
"ax.xaxis.set_major_locator(mdates.HourLocator())\n",
"ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M'))\n",
"\n",
"# Add the label-desciption\n",
"plt.legend(loc = 'upper right')\n",
"\n",
"# Add title to the plot, with date\n",
"plt.title(f'Precipitation {city_name}, ({date}))')\n",
"\n",
"# Shows the plot\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Vise dataframe, med nye kolonner\n",
"Hvis dataframen ikke inneholdt 'rain.1h' eller 'snow.1h', skal de nå ha blitt lagt til med 'NaN' verdier."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Display df, to see if 'rain.1h' and 'snow.1h' was added with NaN values\n",
"# Display dataframe with eventual changes\n",
"display(df)"
]
},
Expand All @@ -337,7 +277,7 @@
"### Sjekk for manglende verdier\n",
"Missigno sjekker og visualiserer manglende verdier, slik at det blir lettere å se hvilke kolonner feilen ligger i. \n",
"\n",
"Vis the blir \"hull\" i en søyle, tyder the på manglende verdier."
"Hvis det blir \"hull\" i en søyle, tyder the på manglende verdier."
]
},
{
Expand All @@ -361,9 +301,9 @@
"\n",
"Under sjekker vi først om regn eller snø er i målingen, og hvis de er, bytter vi ut NaN med 0. \n",
"\n",
"Så sjekker vi om alle verdiene i en kolonne er 'NaN', isåfall så fjerner vi hele kolonnen. Grunne til at dette ikke inkluderer snø og regn, er fordi vi senere plotter disse verdiene, og da får vi ikke feil om verdien er 0, men vil få om hele kolonnen mangler.\n",
"Så sjekker vi om alle verdiene i en kolonne er 'NaN', isåfall så fjerner vi hele kolonnen. Grunnen til at dette ikke inkluderer snø og regn, er fordi vi senere plotter disse verdiene, og da får vi ikke feil om verdien er 0, men vil få om hele kolonnen mangler.\n",
"\n",
"Deretter sjekker vi andre verdier, og bytter enten 'NaN' med 0, eller med verdien før. Verdiene vi setter til 0 gjelder da snø, regn og vind, resten blir satt til verdien før."
"Deretter bruker vi interpolate dersom de skulle være NaN verdier, det er en funksjon som tar utgangspunkt i verdien før og verdien etter for å 'gjette' verdien som mangler. Vi har lagt til 'limit-direction', som gjør at den gjetter selv om man bare har en verdi på siden, som feks. første og siste verdi. "
]
},
{
Expand All @@ -372,54 +312,19 @@
"metadata": {},
"outputs": [],
"source": [
"from my_package.util import fill_rain_column\n",
"from my_package.util import fill_snow_column\n",
"from my_package.util import fill_column_0\n",
"\n",
"df = fill_rain_column(df)\n",
"# The columns we want to replace 'NaN' with 0\n",
"columns_to_0 = ['rain.1h', 'snow.1h', 'wind.gust']\n",
"\n",
"df = fill_snow_column(df)\n",
"# Runs the function with wanted columns\n",
"df = fill_column_0(df, columns_to_0)\n",
"\n",
"# Drops all the columns, if it has 'NaN' value.\n",
"df = df.dropna(axis='columns', how='all')\n",
"\n",
"# If wind_gust is stored, fill the NaN with 0\n",
"try: \n",
" df['wind.gust'] = df['wind.gust'].fillna(0)\n",
"except KeyError:\n",
" print(\"['wind.gust'], not in df\")\n",
"\n",
"# If wind_deg is stored, fill the NaN with 0\n",
"try: \n",
" df['wind.deg'] = df['wind.deg'].fillna(0)\n",
"except KeyError:\n",
" print(\"['wind.deg'], not in df\")\n",
"\n",
"# If wind_speed is stored, fill the NaN with 0\n",
"try: \n",
" df['wind.speed'] = df['wind.speed'].fillna(0)\n",
"except KeyError:\n",
" print(\"['wind.speed'], not in df\")\n",
"\n",
"# If temperature is missing, take the same as the one before\n",
"df['main.temp'] = df['main.temp'].fillna('obj.ffill()')\n",
"\n",
"# Forward fill missing values in what the temperature feels like\n",
"df['main.feels_like'] = df['main.feels_like'].fillna('obj.ffill()')\n",
"\n",
"# Forward fill missing values in the pressure\n",
"df['main.pressure'] = df['main.pressure'].fillna('obj.ffill()')\n",
"\n",
"# Forward fill missing values in the humidity\n",
"df['main.humidity'] = df['main.humidity'].fillna('obj.ffill()')\n",
"\n",
"# Forward fill missing values in the lowest temperature \n",
"df['main.temp_min'] = df['main.temp_min'].fillna('obj.ffill()')\n",
"\n",
"# Forward fill missing values in the highest temperature \n",
"df['main.temp_max'] = df['main.temp_max'].fillna('obj.ffill()')\n",
"\n",
"# Forward fill missing values of clouds\n",
"df['clouds.all'] = df['clouds.all'].fillna('obj.ffill()')\n",
"# Interpolate other missing 'NaN'-values\n",
"df = df.interpolate(method='linear', limit_direction='both')\n",
"\n",
"# Display the df, now without NaN\n",
"display(df)"
Expand Down Expand Up @@ -476,7 +381,7 @@
"# x_axis set to the index, which mean the datetime\n",
"x_axis = df.index\n",
"\n",
"# Gets the values\n",
"# Gets the values we need to for the plot\n",
"rain = df['rain.1h']\n",
"temp = df['main.temp']\n",
"snow = df['snow.1h']\n",
Expand Down

0 comments on commit ef954f3

Please sign in to comment.