Skip to content

Commit

Permalink
Mass renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
znicholls committed Oct 23, 2019
1 parent 732e7bb commit c3231bd
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 44 deletions.
6 changes: 3 additions & 3 deletions docs/source/database-crunchers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ Lead gas cruncher API
:members:


Rolling windows cruncher API
============================
Quantile rolling windows cruncher API
=====================================

.. automodule:: silicone.database_crunchers.rolling_window_quantiles
.. automodule:: silicone.database_crunchers.quantile_rolling_windows
:members:

2 changes: 1 addition & 1 deletion scripts/plot_correlations_between_gases.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ else
fi

mkdir -p "${OUTPUT_DIR}"
silicone-explore-quantiles "${SR15_CSV}" --output-dir "${OUTPUT_DIR}" --years "2010, 2015, 2020, 2030, 2050, 2100" --quantiles "0.0001, 0.05, 0.17, 0.33, 0.5, 0.67, 0.83, 0.95, 0.9999" --quantile-boxes 30 --quantile-decay-factor 0.7 --no-model-colours --legend-fraction 0.65
silicone-explore-quantiles-rolling-windows "${SR15_CSV}" --output-dir "${OUTPUT_DIR}" --years "2010, 2015, 2020, 2030, 2050, 2100" --quantiles "0.0001, 0.05, 0.17, 0.33, 0.5, 0.67, 0.83, 0.95, 0.9999" --quantile-boxes 30 --quantile-decay-factor 0.7 --no-model-colours --legend-fraction 0.65
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

ENTRY_POINTS = {
"console_scripts": [
"silicone-explore-quantiles = silicone.cli:plot_emission_correlations_cli"
"silicone-explore-quantiles-rolling-windows = silicone.cli:plot_emission_correlations_cruncher_quantile_rolling_windows_cli"
]
}

Expand Down
6 changes: 3 additions & 3 deletions src/silicone/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import click
import pyam

from .plotting import plot_emission_correlations
from .plotting import _plot_emission_correlations_cruncher_quantile_rolling_windows


@click.command(context_settings={"help_option_names": ["-h", "--help"]})
Expand Down Expand Up @@ -57,7 +57,7 @@
show_default=True,
help="In the model-coloured version, how much does the figure need to be reduced by to leave room for the legend?",
)
def plot_emission_correlations_cli(
def plot_emission_correlations_cruncher_quantile_rolling_windows_cli(
emissions_data,
output_dir,
years,
Expand All @@ -78,7 +78,7 @@ def plot_emission_correlations_cli(

emissions_data = pyam.IamDataFrame(emissions_data)

plot_emission_correlations(
_plot_emission_correlations_cruncher_quantile_rolling_windows(
emissions_data,
output_dir,
years,
Expand Down
4 changes: 3 additions & 1 deletion src/silicone/database_crunchers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@
"""

from .lead_gas import DatabaseCruncherLeadGas # noqa: F401
from .rolling_window_quantiles import DatabaseCruncherRollingWindows # noqa: F401
from .quantile_rolling_windows import ( # noqa: F401
DatabaseCruncherQuantileRollingWindows,
)
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from .base import _DatabaseCruncher


class DatabaseCruncherRollingWindows(_DatabaseCruncher):
class DatabaseCruncherQuantileRollingWindows(_DatabaseCruncher):
"""
Database cruncher which uses the 'rolling windows' technique.
Expand Down Expand Up @@ -191,7 +191,7 @@ def derive_relationship(

def filler(in_iamdf, interpolate=False):
"""
Filler function derived from :obj:`DatabaseCruncherRollingWindows`.
Filler function derived from :obj:`DatabaseCruncherQuantileRollingWindows`.
Parameters
----------
Expand Down
54 changes: 28 additions & 26 deletions src/silicone/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import pandas as pd
from pyam import IamDataFrame

from .database_crunchers import DatabaseCruncherRollingWindows
from .database_crunchers import DatabaseCruncherQuantileRollingWindows


def plot_emission_correlations(
def _plot_emission_correlations_cruncher_quantile_rolling_windows(
emms_df,
output_dir,
years,
Expand All @@ -20,17 +20,15 @@ def plot_emission_correlations(
model_colours,
legend_fraction,
):
"""
"""
# TODO: split this function into smaller bits
if quantiles is not None:
cruncher = DatabaseCruncherRollingWindows(
cruncher = DatabaseCruncherQuantileRollingWindows(
emms_df.filter(region="World", level=0, variable="Emissions|*")
)

# re-write to something like
# loop over years
# filter the data
# derive the relationship
# plot based on the filler, needs a new function, plot filler
# plot raw data too
for year_of_interest in years:
# Obtain the list of gases to examine
df_gases = (
Expand Down Expand Up @@ -73,24 +71,17 @@ def plot_emission_correlations(

# Plot the results
if model_colours:
plot_multiple_models(
_plot_multiple_models(
legend_fraction, seaborn_df, x_gas, y_gas, x_units, y_units
)
# if all plots are the same colour, we don't have to do all this work
else:
plot_emissions(seaborn_df, x_gas, y_gas, x_units, y_units)
_plot_emissions(seaborn_df, x_gas, y_gas, x_units, y_units)

# Optionally calculate and plot quantiles
if quantiles is not None:
x_max = seaborn_df[x_gas].max()
x_range = x_max - seaborn_df[x_gas].min()
x_range = x_range if not np.equal(x_range, 0) else np.abs(0.1 * x_max)
no_x_pts = 101
x_pts = np.linspace(
seaborn_df[x_gas].min() - 0.1 * x_range,
seaborn_df[x_gas].max() + 0.1 * x_range,
no_x_pts,
)
x_pts = _get_plot_x_pts(seaborn_df, x_gas, no_x_pts)

tmp_df = (
cruncher._db.filter(variable=x_gas, year=year_of_interest)
Expand All @@ -103,7 +94,7 @@ def plot_emission_correlations(
tmp_df["scenario"] = [str(i) for i in range(no_x_pts)]
tmp_df = IamDataFrame(tmp_df)

smooth_quant_df = pd.DataFrame(columns=x_pts, index=quantiles)
quant_df = pd.DataFrame(columns=x_pts, index=quantiles)
for quantile in quantiles:
filler = cruncher.derive_relationship(
y_gas,
Expand All @@ -114,7 +105,7 @@ def plot_emission_correlations(
)

filled_points = filler(tmp_df).timeseries().values.squeeze()
smooth_quant_df.loc[quantile, :] = filled_points
quant_df.loc[quantile, :] = filled_points
plt.plot(
x_pts,
filled_points,
Expand All @@ -125,7 +116,7 @@ def plot_emission_correlations(
plt.legend()

if output_dir is not None:
smooth_quant_df.to_csv(
quant_df.to_csv(
os.path.join(
output_dir,
"{}_{}_{}.csv".format(
Expand Down Expand Up @@ -153,8 +144,6 @@ def plot_emission_correlations(
]
print("Finished {} vs {} in {}".format(x_gas, y_gas, year_of_interest))

print(correlations_df)
print(rank_corr_df)
if output_dir is not None:
correlations_df.to_csv(
os.path.join(
Expand All @@ -168,14 +157,14 @@ def plot_emission_correlations(
)


def plot_emissions(seaborn_df, x_gas, y_gas, x_units, y_units):
def _plot_emissions(seaborn_df, x_gas, y_gas, x_units, y_units):
colours_for_plot = "black"
plt.scatter(x=x_gas, y=y_gas, label=colours_for_plot, data=seaborn_df, alpha=0.5)
plt.xlabel("Emissions of {} ({})".format(x_gas[10:], x_units))
plt.ylabel("Emissions of {} ({})".format(y_gas[10:], y_units))


def plot_multiple_models(legend_fraction, seaborn_df, x_gas, y_gas, x_units, y_units):
def _plot_multiple_models(legend_fraction, seaborn_df, x_gas, y_gas, x_units, y_units):
ax = plt.subplot(111)
all_models = list(seaborn_df["model"].unique())
markers = itertools.cycle(["s", "o", "v", "<", ">", ","])
Expand All @@ -194,3 +183,16 @@ def plot_multiple_models(legend_fraction, seaborn_df, x_gas, y_gas, x_units, y_u
ax.legend(loc="center left", bbox_to_anchor=(1, 0.5))
plt.xlabel("Emissions of {} ({})".format(x_gas[10:], x_units))
plt.ylabel("Emissions of {} ({})".format(y_gas[10:], y_units))


def _get_plot_x_pts(df, x_emissions, no_x_pts):
x_max = df[x_emissions].max()
x_range = x_max - df[x_emissions].min()
x_range = x_range if not np.equal(x_range, 0) else np.abs(0.1 * x_max)
x_pts = np.linspace(
df[x_emissions].min() - 0.1 * x_range,
df[x_emissions].max() + 0.1 * x_range,
no_x_pts,
)

return x_pts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from base import _DataBaseCruncherTester
from pyam import IamDataFrame

from silicone.database_crunchers import DatabaseCruncherRollingWindows
from silicone.database_crunchers import DatabaseCruncherQuantileRollingWindows

_ma = "model_a"
_mb = "model_b"
Expand All @@ -26,7 +26,7 @@


class TestDatabaseCruncherRollingWindows(_DataBaseCruncherTester):
tclass = DatabaseCruncherRollingWindows
tclass = DatabaseCruncherQuantileRollingWindows
tdb = pd.DataFrame(
[
[_ma, _sa, "World", _eco2, _gtc, 1, 2, 3, 4],
Expand Down
12 changes: 7 additions & 5 deletions tests/integration/test_explorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import pandas as pd
from click.testing import CliRunner

from silicone.cli import plot_emission_correlations_cli
from silicone.cli import (
plot_emission_correlations_cruncher_quantile_rolling_windows_cli,
)


def _make_input_file(df, dpath):
Expand Down Expand Up @@ -35,7 +37,7 @@ def test_plot_emission_correlations(check_aggregate_df, tmpdir, caplog):
# caplog can capture any logging calls (if we add any in future)
with caplog.at_level("DEBUG"):
result = runner.invoke(
plot_emission_correlations_cli,
plot_emission_correlations_cruncher_quantile_rolling_windows_cli,
[
input_file,
"--output-dir",
Expand Down Expand Up @@ -91,7 +93,7 @@ def test_plot_emission_correlations_no_quantiles_or_model_colours(
# caplog can capture any logging calls (if we add any in future)
with caplog.at_level("DEBUG"):
result = runner.invoke(
plot_emission_correlations_cli,
plot_emission_correlations_cruncher_quantile_rolling_windows_cli,
[
input_file,
"--output-dir",
Expand Down Expand Up @@ -131,7 +133,7 @@ def test_plot_emission_correlations_no_output(check_aggregate_df, tmpdir, caplog
# caplog can capture any logging calls (if we add any in future)
with caplog.at_level("DEBUG"):
result = runner.invoke(
plot_emission_correlations_cli,
plot_emission_correlations_cruncher_quantile_rolling_windows_cli,
[
input_file,
"--years",
Expand Down Expand Up @@ -171,7 +173,7 @@ def test_plot_emission_correlations_no_co2_emms(check_aggregate_df, tmpdir, capl
# caplog can capture any logging calls (if we add any in future)
with caplog.at_level("DEBUG"):
result = runner.invoke(
plot_emission_correlations_cli,
plot_emission_correlations_cruncher_quantile_rolling_windows_cli,
[
input_file,
"--output-dir",
Expand Down

0 comments on commit c3231bd

Please sign in to comment.