Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove vresutils #662

Merged
merged 6 commits into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/release_notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Upcoming Release
hydrogen fuel cell. Add switches for both re-electrification options under
``sector: hydrogen_turbine:`` and ``sector: hydrogen_fuel_cell:``.

* Remove ``vresutils`` dependency.

PyPSA-Eur 0.8.0 (18th March 2023)
=================================

Expand Down
1 change: 0 additions & 1 deletion doc/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ sphinxcontrib-bibtex
myst-parser # recommark is deprecated, https://stackoverflow.com/a/71660856/13573820

pypsa
vresutils>=0.3.1
powerplantmatching>=0.5.5
atlite>=0.2.9
dask[distributed]
Expand Down
3 changes: 1 addition & 2 deletions envs/environment.fixed.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ dependencies:
- nspr=4.35
- nss=3.88
- numexpr=2.8.3
- numpy=1.23.5
- numpy=1.24
- openjdk=17.0.3
- openjpeg=2.5.0
- openpyxl=3.1.0
Expand Down Expand Up @@ -378,4 +378,3 @@ dependencies:
- highspy==1.5.0.dev0
- pybind11==2.10.3
- tsam==2.2.2
- vresutils==0.3.1
3 changes: 1 addition & 2 deletions envs/environment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ dependencies:
- pytables
- lxml
- powerplantmatching>=0.5.5
- numpy<1.24
- numpy
- pandas>=1.4
- geopandas>=0.11.0
- xarray
Expand Down Expand Up @@ -55,5 +55,4 @@ dependencies:
- rasterio!=1.2.10

- pip:
- vresutils>=0.3.1
- tsam>=1.1.0
4 changes: 0 additions & 4 deletions rules/solve_electricity.smk
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ rule solve_network:
),
python=LOGS
+ "solve_network/elec_s{simpl}_{clusters}_ec_l{ll}_{opts}_python.log",
memory=LOGS
+ "solve_network/elec_s{simpl}_{clusters}_ec_l{ll}_{opts}_memory.log",
benchmark:
BENCHMARKS + "solve_network/elec_s{simpl}_{clusters}_ec_l{ll}_{opts}"
threads: 4
Expand All @@ -41,8 +39,6 @@ rule solve_operations_network:
),
python=LOGS
+ "solve_operations_network/elec_s{simpl}_{clusters}_ec_l{ll}_{opts}_op_python.log",
memory=LOGS
+ "solve_operations_network/elec_s{simpl}_{clusters}_ec_l{ll}_{opts}_op_memory.log",
benchmark:
(
BENCHMARKS
Expand Down
2 changes: 0 additions & 2 deletions rules/solve_myopic.smk
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,6 @@ rule solve_sector_network_myopic:
+ "elec_s{simpl}_{clusters}_l{ll}_{opts}_{sector_opts}_{planning_horizons}_solver.log",
python=LOGS
+ "elec_s{simpl}_{clusters}_l{ll}_{opts}_{sector_opts}_{planning_horizons}_python.log",
memory=LOGS
+ "elec_s{simpl}_{clusters}_l{ll}_{opts}_{sector_opts}_{planning_horizons}_memory.log",
threads: 4
resources:
mem_mb=config["solving"]["mem"],
Expand Down
2 changes: 0 additions & 2 deletions rules/solve_overnight.smk
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ rule solve_sector_network:
+ "elec_s{simpl}_{clusters}_l{ll}_{opts}_{sector_opts}_{planning_horizons}_solver.log",
python=LOGS
+ "elec_s{simpl}_{clusters}_l{ll}_{opts}_{sector_opts}_{planning_horizons}_python.log",
memory=LOGS
+ "elec_s{simpl}_{clusters}_l{ll}_{opts}_{sector_opts}_{planning_horizons}_memory.log",
threads: config["solving"]["solver"].get("threads", 4)
resources:
mem_mb=config["solving"]["mem"],
Expand Down
23 changes: 19 additions & 4 deletions scripts/add_electricity.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,18 @@
"""

import logging
from itertools import product

import geopandas as gpd
import numpy as np
import pandas as pd
import powerplantmatching as pm
import pypsa
import scipy.sparse as sparse
import xarray as xr
from _helpers import configure_logging, update_p_nom_max
from powerplantmatching.export import map_country_bus
from vresutils import transfer as vtransfer
from shapely.prepared import prep

idx = pd.IndexSlice

Expand Down Expand Up @@ -216,6 +218,21 @@ def load_powerplants(ppl_fn):
)


def shapes_to_shapes(orig, dest):
"""
Adopted from vresutils.transfer.Shapes2Shapes()
"""
orig_prepped = list(map(prep, orig))
transfer = sparse.lil_matrix((len(dest), len(orig)), dtype=float)

for i, j in product(range(len(dest)), range(len(orig))):
if orig_prepped[j].intersects(dest[i]):
area = orig[j].intersection(dest[i]).area
transfer[i, j] = area / dest[i].area

return transfer


def attach_load(n, regions, load, nuts3_shapes, countries, scaling=1.0):
substation_lv_i = n.buses.index[n.buses["substation_lv"]]
regions = gpd.read_file(regions).set_index("name").reindex(substation_lv_i)
Expand All @@ -232,9 +249,7 @@ def upsample(cntry, group):
return pd.DataFrame({group.index[0]: l})
else:
nuts3_cntry = nuts3.loc[nuts3.country == cntry]
transfer = vtransfer.Shapes2Shapes(
group, nuts3_cntry.geometry, normed=False
).T.tocsr()
transfer = shapes_to_shapes(group, nuts3_cntry.geometry).T.tocsr()
gdp_n = pd.Series(
transfer.dot(nuts3_cntry["gdp"].fillna(1.0).values), index=group.index
)
Expand Down
6 changes: 3 additions & 3 deletions scripts/prepare_sector_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
override_component_attrs,
update_config_with_sector_opts,
)
from add_electricity import calculate_annuity
from build_energy_totals import build_co2_totals, build_eea_co2, build_eurostat_co2
from networkx.algorithms import complement
from networkx.algorithms.connectivity.edge_augmentation import k_edge_augmentation
from pypsa.geo import haversine_pts
from pypsa.io import import_components_from_dataframe
from scipy.stats import beta
from vresutils.costdata import annuity

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -742,7 +742,7 @@ def prepare_costs(cost_file, config, nyears):
costs = costs.fillna(config["fill_values"])

def annuity_factor(v):
return annuity(v["lifetime"], v["discount rate"]) + v["FOM"] / 100
return calculate_annuity(v["lifetime"], v["discount rate"]) + v["FOM"] / 100

costs["fixed"] = [
annuity_factor(v) * v["investment"] * nyears for i, v in costs.iterrows()
Expand Down Expand Up @@ -851,7 +851,7 @@ def add_wave(n, wave_cost_factor):
capacity = pd.Series({"Attenuator": 750, "F2HB": 1000, "MultiPA": 600})

# in EUR/MW
annuity_factor = annuity(25, 0.07) + 0.03
annuity_factor = calculate_annuity(25, 0.07) + 0.03
costs = (
1e6
* wave_cost_factor
Expand Down
29 changes: 11 additions & 18 deletions scripts/solve_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
override_component_attrs,
update_config_with_sector_opts,
)
from vresutils.benchmark import memory_logger

logger = logging.getLogger(__name__)
pypsa.pf.logger.setLevel(logging.WARNING)
Expand Down Expand Up @@ -667,23 +666,17 @@ def solve_network(n, config, opts="", **kwargs):

np.random.seed(solve_opts.get("seed", 123))

fn = getattr(snakemake.log, "memory", None)
with memory_logger(filename=fn, interval=30.0) as mem:
if "overrides" in snakemake.input.keys():
overrides = override_component_attrs(snakemake.input.overrides)
n = pypsa.Network(
snakemake.input.network, override_component_attrs=overrides
)
else:
n = pypsa.Network(snakemake.input.network)
if "overrides" in snakemake.input.keys():
overrides = override_component_attrs(snakemake.input.overrides)
n = pypsa.Network(snakemake.input.network, override_component_attrs=overrides)
else:
n = pypsa.Network(snakemake.input.network)

n = prepare_network(n, solve_opts, config=snakemake.config)
n = prepare_network(n, solve_opts, config=snakemake.config)

n = solve_network(
n, config=snakemake.config, opts=opts, log_fn=snakemake.log.solver
)

n.meta = dict(snakemake.config, **dict(wildcards=dict(snakemake.wildcards)))
n.export_to_netcdf(snakemake.output[0])
n = solve_network(
n, config=snakemake.config, opts=opts, log_fn=snakemake.log.solver
)

logger.info("Maximum memory usage: {}".format(mem.mem_usage))
n.meta = dict(snakemake.config, **dict(wildcards=dict(snakemake.wildcards)))
n.export_to_netcdf(snakemake.output[0])
31 changes: 12 additions & 19 deletions scripts/solve_operations_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
update_config_with_sector_opts,
)
from solve_network import prepare_network, solve_network
from vresutils.benchmark import memory_logger

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -46,23 +45,17 @@

np.random.seed(solve_opts.get("seed", 123))

fn = getattr(snakemake.log, "memory", None)
with memory_logger(filename=fn, interval=30.0) as mem:
if "overrides" in snakemake.input:
overrides = override_component_attrs(snakemake.input.overrides)
n = pypsa.Network(
snakemake.input.network, override_component_attrs=overrides
)
else:
n = pypsa.Network(snakemake.input.network)
if "overrides" in snakemake.input:
overrides = override_component_attrs(snakemake.input.overrides)
n = pypsa.Network(snakemake.input.network, override_component_attrs=overrides)
else:
n = pypsa.Network(snakemake.input.network)

n.optimize.fix_optimal_capacities()
n = prepare_network(n, solve_opts, config=snakemake.config)
n = solve_network(
n, config=snakemake.config, opts=opts, log_fn=snakemake.log.solver
)

n.meta = dict(snakemake.config, **dict(wildcards=dict(snakemake.wildcards)))
n.export_to_netcdf(snakemake.output[0])
n.optimize.fix_optimal_capacities()
n = prepare_network(n, solve_opts, config=snakemake.config)
n = solve_network(
n, config=snakemake.config, opts=opts, log_fn=snakemake.log.solver
)

logger.info("Maximum memory usage: {}".format(mem.mem_usage))
n.meta = dict(snakemake.config, **dict(wildcards=dict(snakemake.wildcards)))
n.export_to_netcdf(snakemake.output[0])