Skip to content

Commit

Permalink
use absolute imports
Browse files Browse the repository at this point in the history
  • Loading branch information
lkstrp committed Jan 3, 2025
1 parent c3cdff9 commit 7c24e21
Show file tree
Hide file tree
Showing 115 changed files with 407 additions and 475 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,4 @@ To generate a PDF of the dependency graph of all steps `build/dag.pdf` run:

## License

The code in this repo is MIT licensed, see `./LICENSE.md`.
The code in this repo is MIT licensed, see `./LICENSE.md`.
5 changes: 2 additions & 3 deletions rules/common.smk
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ from functools import partial, lru_cache

import os, sys, glob

path = workflow.source_path("../scripts/_helpers.py")
sys.path.insert(0, os.path.dirname(path))
sys.path.insert(0, os.path.abspath("../scripts"))

from _helpers import validate_checksum, update_config_from_wildcards
from scripts._helpers import validate_checksum, update_config_from_wildcards
from snakemake.utils import update_config


Expand Down
4 changes: 1 addition & 3 deletions scripts/_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
# SPDX-FileCopyrightText: : 2020-2024 The PyPSA-Eur Authors
#
# SPDX-License-Identifier: MIT
"""
"""
""" """

from __future__ import absolute_import, print_function

Expand Down
9 changes: 5 additions & 4 deletions scripts/add_brownfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
import pandas as pd
import pypsa
import xarray as xr
from _helpers import (
from add_existing_baseyear import add_build_year_to_new_assets
from pypsa.clustering.spatial import normed_or_uniform

from scripts._helpers import (
configure_logging,
get_snapshots,
set_scenario_config,
update_config_from_wildcards,
)
from add_existing_baseyear import add_build_year_to_new_assets
from pypsa.clustering.spatial import normed_or_uniform

logger = logging.getLogger(__name__)
idx = pd.IndexSlice
Expand Down Expand Up @@ -258,7 +259,7 @@ def update_heat_pump_efficiency(n: pypsa.Network, n_p: pypsa.Network, year: int)

if __name__ == "__main__":
if "snakemake" not in globals():
from _helpers import mock_snakemake
from scripts._helpers import mock_snakemake

snakemake = mock_snakemake(
"add_brownfield",
Expand Down
12 changes: 5 additions & 7 deletions scripts/add_electricity.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,15 @@
import powerplantmatching as pm
import pypsa
import xarray as xr
from _helpers import (
from powerplantmatching.export import map_country_bus
from pypsa.clustering.spatial import DEFAULT_ONE_PORT_STRATEGIES, normed_or_uniform

from scripts._helpers import (
configure_logging,
get_snapshots,
set_scenario_config,
update_p_nom_max,
)
from powerplantmatching.export import map_country_bus
from pypsa.clustering.spatial import DEFAULT_ONE_PORT_STRATEGIES, normed_or_uniform

idx = pd.IndexSlice

Expand Down Expand Up @@ -308,7 +309,6 @@ def load_and_aggregate_powerplants(
aggregation_strategies: dict = None,
exclude_carriers: list = None,
) -> pd.DataFrame:

if not aggregation_strategies:
aggregation_strategies = {}

Expand Down Expand Up @@ -410,7 +410,6 @@ def attach_load(
busmap_fn: str,
scaling: float = 1.0,
) -> None:

load = (
xr.open_dataarray(load_fn).to_dataframe().squeeze(axis=1).unstack(level="time")
)
Expand All @@ -431,7 +430,6 @@ def set_transmission_costs(
line_length_factor: float = 1.0,
link_length_factor: float = 1.0,
) -> None:

n.lines["capital_cost"] = (
n.lines["length"]
* line_length_factor
Expand Down Expand Up @@ -1014,7 +1012,7 @@ def attach_stores(n, costs, extendable_carriers):

if __name__ == "__main__":
if "snakemake" not in globals():
from _helpers import mock_snakemake
from scripts._helpers import mock_snakemake

snakemake = mock_snakemake("add_electricity", clusters=100)
configure_logging(snakemake)
Expand Down
19 changes: 12 additions & 7 deletions scripts/add_existing_baseyear.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,21 @@
import powerplantmatching as pm
import pypsa
import xarray as xr
from _helpers import (

from scripts._helpers import (
configure_logging,
set_scenario_config,
update_config_from_wildcards,
)
from add_electricity import sanitize_carriers
from definitions.heat_sector import HeatSector
from definitions.heat_system import HeatSystem
from definitions.heat_system_type import HeatSystemType
from prepare_sector_network import cluster_heat_buses, define_spatial, prepare_costs
from scripts.add_electricity import sanitize_carriers
from scripts.definitions.heat_sector import HeatSector
from scripts.definitions.heat_system import HeatSystem
from scripts.definitions.heat_system_type import HeatSystemType
from scripts.prepare_sector_network import (
cluster_heat_buses,
define_spatial,
prepare_costs,
)

logger = logging.getLogger(__name__)
cc = coco.CountryConverter()
Expand Down Expand Up @@ -1063,7 +1068,7 @@ def set_defaults(n):
# %%
if __name__ == "__main__":
if "snakemake" not in globals():
from _helpers import mock_snakemake
from scripts._helpers import mock_snakemake

snakemake = mock_snakemake(
"add_existing_baseyear",
Expand Down
7 changes: 3 additions & 4 deletions scripts/add_transmission_projects_and_dlr.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
import pandas as pd
import pypsa
import xarray as xr
from _helpers import configure_logging, set_scenario_config

from scripts._helpers import configure_logging, set_scenario_config

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -71,7 +72,7 @@ def attach_line_rating(

if __name__ == "__main__":
if "snakemake" not in globals():
from _helpers import mock_snakemake
from scripts._helpers import mock_snakemake

snakemake = mock_snakemake("add_transmission_projects_and_dlr")
configure_logging(snakemake)
Expand All @@ -82,11 +83,9 @@ def attach_line_rating(
n = pypsa.Network(snakemake.input.network)

if params["transmission_projects"]["enable"]:

attach_transmission_projects(n, snakemake.input.transmission_projects)

if params["dlr"]["activate"]:

rating = xr.open_dataarray(snakemake.input.dlr).to_pandas().transpose()

s_max_pu = params["s_max_pu"]
Expand Down
11 changes: 8 additions & 3 deletions scripts/base_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,19 @@
import shapely.prepared
import shapely.wkt
import yaml
from _helpers import REGION_COLS, configure_logging, get_snapshots, set_scenario_config
from packaging.version import Version, parse
from scipy.sparse import csgraph
from scipy.spatial import KDTree
from shapely.geometry import LineString, Point

PD_GE_2_2 = parse(pd.__version__) >= Version("2.2")
from scripts._helpers import (
REGION_COLS,
configure_logging,
get_snapshots,
set_scenario_config,
)

PD_GE_2_2 = parse(pd.__version__) >= Version("2.2")
logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -976,7 +981,7 @@ def append_bus_shapes(n, shapes, type):

if __name__ == "__main__":
if "snakemake" not in globals():
from _helpers import mock_snakemake
from scripts._helpers import mock_snakemake

snakemake = mock_snakemake("base_network")
configure_logging(snakemake)
Expand Down
5 changes: 3 additions & 2 deletions scripts/build_ammonia_production.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@
import country_converter as coco
import numpy as np
import pandas as pd
from _helpers import set_scenario_config

from scripts._helpers import set_scenario_config

cc = coco.CountryConverter()


if __name__ == "__main__":
if "snakemake" not in globals():
from _helpers import mock_snakemake
from scripts._helpers import mock_snakemake

snakemake = mock_snakemake("build_ammonia_production")

Expand Down
8 changes: 4 additions & 4 deletions scripts/build_biomass_potentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
import geopandas as gpd
import numpy as np
import pandas as pd
from _helpers import configure_logging, set_scenario_config
from build_energy_totals import build_eurostat

from scripts._helpers import configure_logging, set_scenario_config
from scripts.build_energy_totals import build_eurostat

logger = logging.getLogger(__name__)
AVAILABLE_BIOMASS_YEARS = [2010, 2020, 2030, 2040, 2050]
Expand Down Expand Up @@ -339,8 +340,7 @@ def add_unsustainable_potentials(df):

if __name__ == "__main__":
if "snakemake" not in globals():

from _helpers import mock_snakemake
from scripts._helpers import mock_snakemake

snakemake = mock_snakemake(
"build_biomass_potentials",
Expand Down
3 changes: 2 additions & 1 deletion scripts/build_biomass_transport_costs.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
@author: bw0928
"""

import pandas as pd

ENERGY_CONTENT = 4.8 # unit MWh/t (wood pellets)
Expand Down Expand Up @@ -114,7 +115,7 @@ def build_biomass_transport_costs():

if __name__ == "__main__":
if "snakemake" not in globals():
from _helpers import mock_snakemake
from scripts._helpers import mock_snakemake

snakemake = mock_snakemake("build_biomass_transport_costs")

Expand Down
5 changes: 3 additions & 2 deletions scripts/build_central_heating_temperature_profiles/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@
import numpy as np
import pandas as pd
import xarray as xr
from _helpers import set_scenario_config
from central_heating_temperature_approximator import (
CentralHeatingTemperatureApproximator,
)

from scripts._helpers import set_scenario_config


def extrapolate_missing_supply_temperatures_by_country(
extrapolate_from: dict, extrapolate_to: dict
Expand Down Expand Up @@ -187,7 +188,7 @@ def scale_temperature_to_investment_year(

if __name__ == "__main__":
if "snakemake" not in globals():
from _helpers import mock_snakemake
from scripts._helpers import mock_snakemake

snakemake = mock_snakemake(
"build_cop_profiles",
Expand Down
5 changes: 3 additions & 2 deletions scripts/build_clustered_population_layouts.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
import geopandas as gpd
import pandas as pd
import xarray as xr
from _helpers import set_scenario_config

from scripts._helpers import set_scenario_config

if __name__ == "__main__":
if "snakemake" not in globals():
from _helpers import mock_snakemake
from scripts._helpers import mock_snakemake

snakemake = mock_snakemake("build_clustered_population_layouts", clusters=48)

Expand Down
2 changes: 1 addition & 1 deletion scripts/build_cop_profiles/BaseCopApproximator.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def approximate_cop(self) -> Union[xr.DataArray, np.array]:

@staticmethod
def celsius_to_kelvin(
t_celsius: Union[float, xr.DataArray, np.array]
t_celsius: Union[float, xr.DataArray, np.array],
) -> Union[float, xr.DataArray, np.array]:
"""
Convert temperature from Celsius to Kelvin.
Expand Down
4 changes: 2 additions & 2 deletions scripts/build_cop_profiles/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@
import numpy as np
import pandas as pd
import xarray as xr
from _helpers import set_scenario_config
from CentralHeatingCopApproximator import CentralHeatingCopApproximator
from DecentralHeatingCopApproximator import DecentralHeatingCopApproximator

from scripts._helpers import set_scenario_config
from scripts.definitions.heat_system_type import HeatSystemType


Expand Down Expand Up @@ -100,7 +100,7 @@ def get_country_from_node_name(node_name: str) -> str:

if __name__ == "__main__":
if "snakemake" not in globals():
from _helpers import mock_snakemake
from scripts._helpers import mock_snakemake

snakemake = mock_snakemake(
"build_cop_profiles",
Expand Down
5 changes: 3 additions & 2 deletions scripts/build_cross_border_flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@

import pandas as pd
import pypsa
from _helpers import configure_logging, set_scenario_config
from entsoe import EntsoePandasClient
from entsoe.exceptions import InvalidBusinessParameterError, NoMatchingDataError
from requests import HTTPError

from scripts._helpers import configure_logging, set_scenario_config

logger = logging.getLogger(__name__)

if __name__ == "__main__":
if "snakemake" not in globals():
from _helpers import mock_snakemake
from scripts._helpers import mock_snakemake

snakemake = mock_snakemake("build_cross_border_flows")
configure_logging(snakemake)
Expand Down
5 changes: 3 additions & 2 deletions scripts/build_cutout.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,14 @@
import atlite
import geopandas as gpd
import pandas as pd
from _helpers import configure_logging, set_scenario_config

from scripts._helpers import configure_logging, set_scenario_config

logger = logging.getLogger(__name__)

if __name__ == "__main__":
if "snakemake" not in globals():
from _helpers import mock_snakemake
from scripts._helpers import mock_snakemake

snakemake = mock_snakemake("build_cutout", cutout="europe-2013-sarah3-era5")
configure_logging(snakemake)
Expand Down
5 changes: 3 additions & 2 deletions scripts/build_daily_heat_demand.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,13 @@
import geopandas as gpd
import numpy as np
import xarray as xr
from _helpers import get_snapshots, set_scenario_config
from dask.distributed import Client, LocalCluster

from scripts._helpers import get_snapshots, set_scenario_config

if __name__ == "__main__":
if "snakemake" not in globals():
from _helpers import mock_snakemake
from scripts._helpers import mock_snakemake

snakemake = mock_snakemake(
"build_daily_heat_demands",
Expand Down
Loading

0 comments on commit 7c24e21

Please sign in to comment.