Skip to content

Commit

Permalink
Merge pull request #72 from holukas/anomaly-plot
Browse files Browse the repository at this point in the history
Anomaly plot
  • Loading branch information
holukas authored Mar 19, 2024
2 parents ade67d3 + d503b9b commit 191d318
Show file tree
Hide file tree
Showing 11 changed files with 499 additions and 30 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@

![DIIVE](images/logo_diive1_256px.png)

## v0.71.4 | 20 Mar 2024

### Changes

- Refactored class `LongtermAnomaliesYear` (`diive.core.plotting.bar.LongtermAnomaliesYear`)

![DIIVE](images/plotBarLongtermAnomaliesYear_diive_v0.71.4.png)

### Notebooks

- Added new notebook for `LongtermAnomaliesYear` (`notebooks/Plotting/LongTermAnomalies.ipynb`)

## v0.71.3 | 19 Mar 2024

### Changes
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ Fill gaps in time series with various methods

### Plotting

- Long-term anomalies per year ([notebook example](https://github.com/holukas/diive/blob/main/notebooks/Plotting/LongTermAnomalies.ipynb))
- Simple (interactive) time series
plot ([notebook example](https://github.com/holukas/diive/blob/main/notebooks/Plotting/TimeSeries.ipynb))
- ScatterXY plot ([notebook example](https://github.com/holukas/diive/blob/main/notebooks/Plotting/ScatterXY.ipynb))
Expand Down
44 changes: 32 additions & 12 deletions diive/core/plotting/bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,25 @@ class LongtermAnomaliesYear:

def __init__(self,
series: Series,
series_units: str,
reference_start_year: int,
reference_end_year: int):
reference_end_year: int,
series_label: str = None,
series_units: str = None):
"""Calculate and plot long-term anomaly for a variable, per year, compared to a reference period.
Args:
series: Time series for anomalies with one value per year.
reference_start_year: First year of the reference period.
reference_end_year: Last year of the reference period.
series_label: Label for *series* on the y-axis of the plot.
series_units: Units for *series* on the y-axis of the plot.
- Example notebook available in:
notebooks/Plotting/LongTermAnomalies.ipynb
"""
self.series = series
self.series_units = series_units
self.series_label = series_label
self.reference_start_year = reference_start_year
self.reference_end_year = reference_end_year

Expand All @@ -27,7 +41,7 @@ def __init__(self,
self.anomalies_df = self._calc_reference()

def _apply_format(self):
title = f"Anomaly per year ({self.data_first_year}-{self.data_last_year})"
title = f"{self.series_label} anomaly per year ({self.data_first_year}-{self.data_last_year})"
self.fig.suptitle(title, fontsize=theme.FIGHEADER_FONTSIZE)

ref_mean = self.anomalies_df['reference_mean'].iloc[-1]
Expand All @@ -47,7 +61,7 @@ def _apply_format(self):
self.ax.locator_params(axis='x', nbins=nbins)
pf.default_format(ax=self.ax,
ax_xlabel_txt='Year',
ax_ylabel_txt=self.series.name,
ax_ylabel_txt=f"{self.series_label} anomaly",
txt_ylabel_units=self.series_units,
showgrid=False)
self.ax.axhline(0, lw=1, color='black')
Expand All @@ -74,8 +88,10 @@ def get(self):

def plot(self, showplot: bool = True):
# ax1.plot(ta_longterm.index.values, ta_longterm['diff'].values)
self.anomalies_df['anomaly_above'].plot.bar(color='#F44336', ax=self.ax, legend=False)
self.anomalies_df['anomaly_below'].plot.bar(color='#2196F3', ax=self.ax, legend=False)
self.anomalies_df['anomaly_above'].plot.bar(color='#EF5350', ax=self.ax, legend=False, width=.7)
# self.anomalies_df['anomaly_above'].plot.bar(color='#F44336', ax=self.ax, legend=False)
self.anomalies_df['anomaly_below'].plot.bar(color='#42A5F5', ax=self.ax, legend=False, width=.7)
# self.anomalies_df['anomaly_below'].plot.bar(color='#2196F3', ax=self.ax, legend=False)
# ta_longterm_anomalies.plot.bar(x='year', y='Temperature', color='#2196F3', ax=ax1)
# ta_longterm_anomalies_above.plot.bar(x='year', y='Temperature', color='red', ax=ax1)
# ta_longterm_anomalies_below.plot.bar(x='year', y='Temperature', color='blue', ax=ax1)
Expand All @@ -89,15 +105,19 @@ def plot(self, showplot: bool = True):
def example():
## Long-term TA
## space-separated data
data_longterm_TA = r"F:\Dropbox\luhk_work\_current\CH-DAV_1864-2021_TA-YEARLY_Meteoswiss_order_105469_data.txt"
ta_longterm = pd.read_csv(data_longterm_TA, header=0, encoding='utf-8', delimiter=';',
keep_date_col=False, index_col='time', dtype=None,
engine='python')
ta_longterm = ta_longterm['tre200y0'].copy()
LongtermAnomaliesYear(series=ta_longterm,
data_longterm_TA = r"L:\Sync\luhk_work\80 - SITES\CH-DAV\Data\Datasets\MeteoSwiss\CH-DAV_1864-2023_TA-YEARLY_Meteoswiss_order_120443_data.txt"
df = pd.read_csv(data_longterm_TA, header=0, encoding='utf-8', delimiter=';',
keep_date_col=False, index_col='time', dtype=None,
engine='python')

series = df['tre200y0'].copy()
series_label = "Air temperature"
LongtermAnomaliesYear(series=series,
series_label=series_label,
series_units='(°C)',
reference_start_year=1864,
reference_end_year=1913).plot()


if __name__ == '__main__':
example()
2 changes: 1 addition & 1 deletion diive/core/plotting/plotfuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ def save_fig(fig,


def create_ax(facecolor: str = 'white',
figsize: tuple = (8, 4.5),
figsize: tuple = (14, 8),
dpi: int = 100):
"""Create figure and axis"""
# Figure setup
Expand Down
23 changes: 13 additions & 10 deletions diive/pkgs/analyses/decoupling.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,21 @@ def __init__(self,
n_bins_var1: int = 48,
n_subbins_var2: int = 2,
convert_to_percentiles: bool = False):
"""Investigate binned aggregates of a variable z in binned classes of x and y.
"""Investigate binned aggregates (median) of a variable z in binned classes of x and y.
For example: show mean GPP (y) in 5 classes of VPD (x), separate for 10 classes
For example: show median GPP (y) in 5 classes of VPD (x), separate for 10 classes
of air temperature (z).
Args:
df:
var1_col:
var2_col:
var3_col:
n_bins_var1:
n_subbins_var2:
df: Dataframe with variables
var1_col: Name of the first binning variable, will be shown as colors (z) in the plot.
var2_col: Name of the second binning variable, will be shown on the x-axis (x) in the plot.
var3_col: Name of the variable of interest, will be shown on the y-axis (y) in the plot.
n_bins_var1: Number of bins for variable *var1_col*.
n_subbins_var2: Number of bins for variable *var2_col*.
Returns:
Dict with results stored as dataframes for each bin of *var1_col*.
- Example notebook available in:
notebooks/Analyses/DecouplingSortingBins.ipynb
Expand Down Expand Up @@ -78,7 +81,7 @@ def binmedians(self) -> dict:
raise Exception('No binned means available, try to run .calcbins() first.')
return self._binmedians

def get_binmeans(self) -> dict:
def get_binmedians(self) -> dict:
"""Return dict of dataframes with variable 1 group as key"""
return self.binmedians

Expand Down Expand Up @@ -269,7 +272,7 @@ def example():
sbm.calcbins()
sbm.showplot_decoupling_sbm(marker='o', emphasize_lines=True)

binmedians = sbm.get_binmeans()
binmedians = sbm.get_binmedians()
first = next(iter(binmedians))
print(binmedians[first])

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 5 additions & 5 deletions notebooks/Analyses/DecouplingSortingBins.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"diive version: v0.71.3\n"
"diive version: v0.71.4\n"
]
}
],
Expand Down Expand Up @@ -137,7 +137,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Loaded .parquet file L:\\Sync\\luhk_work\\20 - CODING\\21 - DIIVE\\diive\\diive\\configs\\exampledata\\exampledata_CH-DAV_FP2022.5_2013-2022_ID20230206154316_30MIN.parquet (0.055 seconds). Detected time resolution of <30 * Minutes> / 30T \n"
"Loaded .parquet file L:\\Sync\\luhk_work\\20 - CODING\\21 - DIIVE\\diive\\diive\\configs\\exampledata\\exampledata_CH-DAV_FP2022.5_2013-2022_ID20230206154316_30MIN.parquet (0.092 seconds). Detected time resolution of <30 * Minutes> / 30T \n"
]
},
{
Expand Down Expand Up @@ -313,7 +313,7 @@
"id": "df38fda9-066a-4fef-986a-cfe921fb88c0",
"metadata": {},
"source": [
"# (1) **Calculate daily correlation between `Rg_f` and `SW_IN_POT`**"
"# **Calculate bin medians**"
]
},
{
Expand Down Expand Up @@ -369,7 +369,7 @@
"metadata": {},
"outputs": [],
"source": [
"binmedians = sbm.get_binmeans()"
"binmedians = sbm.get_binmedians()"
]
},
{
Expand Down Expand Up @@ -530,7 +530,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Finished 2024-03-19 17:06:54\n"
"Finished 2024-03-19 23:56:08\n"
]
}
],
Expand Down
3 changes: 2 additions & 1 deletion notebooks/OVERVIEW.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"metadata": {},
"source": [
"---\n",
"**Last updated:**: 19 Mar 2024 \n",
"**Last updated:**: 20 Mar 2024 \n",
"**Author**: Lukas Hörtnagl (holukas@ethz.ch) \n",
"Overview of example notebooks for the time series processing library `diive`."
]
Expand Down Expand Up @@ -237,6 +237,7 @@
"id": "3f12e3ac-15eb-4dcb-bbbc-4652149b25d9",
"metadata": {},
"source": [
"- [Long-term anomalies per year](Plotting/LongTermAnomalies.ipynb)\n",
"- [Simple (interactive) time series plot](Plotting/TimeSeries.ipynb)"
]
},
Expand Down
Loading

0 comments on commit 191d318

Please sign in to comment.