### Install Git on Ubuntu/Debian Source: https://github.com/climada-project/climada_python/blob/main/doc/getting-started/install.rst Installs the Git version control system using APT. This is a prerequisite for cloning repositories on Linux. ```shell apt update apt install git ``` -------------------------------- ### Install Climada Petals from Source Source: https://github.com/climada-project/climada_python/blob/main/doc/development/Guide_Euler.ipynb Installs Climada Petals by cloning its repository and using pip's editable install. This process is similar to installing Climada itself from source. ```bash cd /cluster/project/climate/$USER # or wherever you plan to download the repository git clone https://github.com/CLIMADA-project/climada_petals.git # unless this has been done before cd climada_petals pip install -e . ``` -------------------------------- ### Create Installation and Working Directories Source: https://github.com/climada-project/climada_python/blob/main/doc/development/Guide_Euler.ipynb Creates the necessary directories for Climada installation and data processing on the Euler cluster. Ensure you replace '$USER' with your actual nethz-id. ```bash mkdir -p /cluster/project/climate/$USER \ /cluster/work/climate/$USER ``` -------------------------------- ### Download and Install Miniforge (macOS/Linux) Source: https://github.com/climada-project/climada_python/blob/main/doc/getting-started/install.rst Use curl to download the Miniforge installer script and then execute it with bash. Follow the prompts to accept license terms and initialize conda. ```shell curl -L -O "https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-$(uname)-$(uname -m).sh" bash Miniforge3-$(uname)-$(uname -m).sh ``` -------------------------------- ### Run all CLIMADA unit tests Source: https://github.com/climada-project/climada_python/blob/main/doc/getting-started/install.rst Verify your CLIMADA installation by running all unit tests. This requires an advanced setup. Navigate to the repository, activate the environment, and then execute the tests. ```shell cd /climada_python mamba activate climada_env python -m unittest discover -s climada -p "test*.py" ``` -------------------------------- ### Climada Configuration File Example Source: https://github.com/climada-project/climada_python/blob/main/doc/development/Guide_Euler.ipynb Example content for a climada.conf file, specifying local data directories for system, demo, and results. Replace 'USERNAME' with your nethz-id. ```json { "local_data": { "system": "/cluster/work/climate/USERNAME/climada/data", "demo": "/cluster/project/climate/USERNAME/climada/data/demo", "save_dir": "/cluster/work/climate/USERNAME/climada/results" } } ``` -------------------------------- ### Install pre-commit hooks Source: https://github.com/climada-project/climada_python/blob/main/doc/getting-started/install.rst Install pre-commit hooks to automate code quality checks before committing. This command should be run after installing developer dependencies. ```shell pre-commit install ``` -------------------------------- ### Install make for testing on Windows Source: https://github.com/climada-project/climada_python/blob/main/doc/getting-started/install.rst Install the 'make' utility using Conda, which is required for running test scripts on Windows. Assumes the 'climada_env' is active. ```shell mamba install -n climada_env make ``` -------------------------------- ### Inspecting Set Methods Source: https://github.com/climada-project/climada_python/blob/main/doc/getting-started/0_intro_python.ipynb Provides examples of how to check the documentation and available methods for Python sets. ```python # check set documentation help(set) ``` ```python # check which methods can be applied on set dir(set) ``` -------------------------------- ### Create Hazard Object for Binning Example Source: https://github.com/climada-project/climada_python/blob/main/doc/user-guide/climada_util_local_exceedance_values.ipynb Initializes a CLIMADA Hazard object with specific intensity and frequency data for demonstrating binning effects. This setup is crucial for observing the impact of similar maximal intensities on extrapolation. ```python hazard_binning = Hazard( intensity=sparse.csr_matrix([[80.0], [80.02], [70.0], [70.0], [60.0]]), frequency=np.array([0.1, 0.1, 0.1, 0.1, 0.1]), frequency_unit="1/year", units="m/s", centroids=Centroids(lat=np.array([1]), lon=np.array([2])), ) ``` -------------------------------- ### Initialize Entity with Demo Data Source: https://github.com/climada-project/climada_python/blob/main/doc/user-guide/climada_engine_unsequa_helper.ipynb Loads a predefined entity dataset for demonstration purposes. This is useful for testing and examples. ```python from climada.entity import Entity from climada.util.constants import ENT_DEMO_FUTURE ent = Entity.from_csv(ENT_DEMO_"future") ``` -------------------------------- ### Install Climada from Source Source: https://github.com/climada-project/climada_python/blob/main/doc/development/Guide_Euler.ipynb Installs Climada by cloning the repository and using pip's editable install option. This is useful for development or using specific branches. Remember to checkout your desired branch before installation. ```bash cd /cluster/project/climate/$USER # or wherever you plan to download the repository git clone https://github.com/CLIMADA-project/climada_python.git # unless this has been done before cd climada_python pip install -e . ``` -------------------------------- ### Test Climada Installation on Login Node Source: https://github.com/climada-project/climada_python/blob/main/doc/development/Guide_Euler.ipynb Runs the Climada installation test suite on the Euler login node. A successful test will output 'OK'. ```bash python -m unittest climada.engine.test.test_impact_calc ``` -------------------------------- ### Start JupyterLab Source: https://github.com/climada-project/climada_python/blob/main/doc/getting-started/install.rst Activate the Conda environment and start the JupyterLab server. This will open JupyterLab in your default web browser. ```shell mamba activate climada_env jupyter-lab ``` -------------------------------- ### Create and Activate Virtual Environment Source: https://github.com/climada-project/climada_python/blob/main/doc/development/Guide_Euler.ipynb Sets up a Python virtual environment named 'climada_env' for Climada installation. It also installs the Python kernel for use in JupyterHub. ```bash envname=climada_env # create environment python -m venv --system-site-packages /cluster/project/climate/$USER/venv/$envname # acitvate it . /cluster/project/climate/$USER/venv/$envname/bin/activate # install python kernel (to be used in JupyterHub, s.b.) python -m ipykernel install --user --name $envname ``` -------------------------------- ### JSON Configuration File Structure Source: https://github.com/climada-project/climada_python/blob/main/doc/development/Guide_Configuration.ipynb Provides an example of a JSON configuration file structure for defining module-specific constants, reflecting the repository's file structure. ```json { "util": { "dates_times": { "my_config_value": 42 } } } ``` -------------------------------- ### Multi-dimensional Broadcasting Example Source: https://github.com/climada-project/climada_python/blob/main/doc/development/Guide_Py_Performance.ipynb An example showcasing broadcasting with multi-dimensional arrays, using `None` to specify where new axes should be introduced for alignment. ```python input_arr = np.random.rand(7, 3, 5, 4, 6) factors = np.random.rand(7, 3, 4) output = factors[:, :, None, :, None] * input_arr ``` -------------------------------- ### Load Example Data and Initialize Impact Function Source: https://github.com/climada-project/climada_python/blob/main/doc/user-guide/climada_entity_Exposures_polygons_lines.ipynb Loads example hazard, exposure data (polygons, lines, points), and initializes the impact function set. This is a prerequisite for running impact calculations. ```python from climada.util.api_client import Client import climada.util.lines_polys_handler as u_lp from climada.entity.impact_funcs import ImpactFuncSet from climada.entity.impact_funcs.storm_europe import ImpfStormEurope from climada.entity import Exposures HAZ = Client().get_hazard("storm_europe", name="test_haz_WS_nl", status="test_dataset") EXP_POLY = Client().get_exposures( "base", name="test_polygon_exp", status="test_dataset" ) EXP_LINE = Client().get_exposures("base", name="test_line_exp", status="test_dataset") EXP_POINT = Client().get_exposures("base", name="test_point_exp", status="test_dataset") EXP_MIX = Exposures.concat([EXP_POLY, EXP_LINE, EXP_POINT]) IMPF = ImpfStormEurope.from_welker() IMPF_SET = ImpactFuncSet([IMPF]) ``` -------------------------------- ### Start a New Feature Branch (Vanilla Git) Source: https://github.com/climada-project/climada_python/blob/main/doc/development/Guide_CLIMADA_Development.ipynb Create a new branch for feature development using standard Git commands. ```bash git checkout -b feature/feature_name ``` -------------------------------- ### Setting up a Review Environment Source: https://github.com/climada-project/climada_python/blob/main/doc/development/Guide_Review.ipynb Commands to fetch changes, checkout a branch, create a new Conda environment, update it with CLIMADA requirements, activate it, and install CLIMADA in editable mode. ```bash git fetch && git checkout mamba create -n # restrict python version here with "python==3.x.*" mamba env update -n -f requirements/env_climada.yml mamba activate pip install -e ./ ``` -------------------------------- ### Python List Comprehension Example Source: https://github.com/climada-project/climada_python/blob/main/doc/development/Guide_PythonDos-n-Donts.ipynb Demonstrates list comprehension, a compact syntax for creating lists. This example creates a list of numbers less than 5 from a range of 0 to 9. ```python newlist = [x for x in range(10) if x < 5] ``` -------------------------------- ### Test Climada Installation on Compute Node Source: https://github.com/climada-project/climada_python/blob/main/doc/development/Guide_Euler.ipynb Submits a job to the Euler compute node to test the Climada installation. The output will be in a 'slurm-[XXXXXXX].out' file. ```bash sbatch --wrap="python -m unittest climada.engine.test.test_impact_calc" ``` -------------------------------- ### Accessing Dictionary Keys Source: https://github.com/climada-project/climada_python/blob/main/doc/getting-started/0_intro_python.ipynb Illustrates how to get a list of all keys in a dictionary and how to sort them. ```python list(tel.keys()) ``` ```python sorted(tel.keys()) ``` -------------------------------- ### Set Storm Severity Index (SSI) Source: https://github.com/climada-project/climada_python/blob/main/doc/user-guide/climada_hazard_StormEurope.ipynb Calculates and sets the Storm Severity Index (SSI) for the storm instance. This example uses default arguments for demonstration. ```python storm_instance.set_ssi( method="wind_gust", intensity=storm_instance.intensity, # the above is just a more explicit way of passing the default on_land=True, threshold=25, sel_cen=None, # None is default. sel_cen could be used to subset centroids ) ``` -------------------------------- ### Catching all exceptions (Bad Practice) Source: https://github.com/climada-project/climada_python/blob/main/doc/development/Guide_Exception_Logging.ipynb Avoid using a bare 'except:' clause as it can hide errors. This example demonstrates a common anti-pattern. ```python # Bad (1) x = 1 try: l = len(events) if l < 1: print("l is too short") except: pass ``` -------------------------------- ### Build HTML Documentation Source: https://github.com/climada-project/climada_python/blob/main/doc/README.md Execute the 'make html' command to build the documentation. This process may take a while on the first run. ```bash make html ``` -------------------------------- ### Save Tropical Cyclone Animation as MP4 Source: https://github.com/climada-project/climada_python/blob/main/doc/user-guide/climada_hazard_TropCyclone.ipynb Generates the same tropical cyclone animation as the GIF example but saves it in MP4 format for reduced file size. Requires the 'ffmpeg' package to be installed. ```python tc_video.video_intensity(track_name, tr_irma, centr_video, file_format='mp4', file_name='results/irma_tc_fl.mp4') ``` -------------------------------- ### Navigate to Documentation Directory Source: https://github.com/climada-project/climada_python/blob/main/doc/README.md Activate the conda environment and change the directory to the 'doc' folder to prepare for building the documentation. ```bash conda activate climada_env cd climada_python/doc ``` -------------------------------- ### Define Calibration Input and Run Optimizer Source: https://github.com/climada-project/climada_python/blob/main/doc/user-guide/climada_util_calibrate.ipynb Sets up the input parameters for calibration and runs the TragedyEnsembleOptimizer with a BayesianOptimizer. Requires hazard, exposure, data, impact function creator, cost function, impact-to-dataframe function, and bounds. ```python from climada.util.calibrate import log_level from climada.engine import Input, TragedyEnsembleOptimizer from climada.engine.optimizer import BayesianOptimizer, BayesianOptimizerController # Define calibration input with log_level("WARNING", name_prefix="climada.util.calibrate"): input = Input( hazard=hazard, exposure=exposure, data=data, impact_func_creator=impact_func_tc, cost_func=msle, impact_to_dataframe=lambda imp: imp.impact_at_reg(exposure.gdf["region_id"]), bounds=bounds, ) # Create and run the optimizer opt_eot = TragedyEnsembleOptimizer(input, optimizer_type=BayesianOptimizer) controller = BayesianOptimizerController.from_input(input) output_eot = opt_eot.run(controller=controller) ``` -------------------------------- ### Install CLIMADA using Mamba Source: https://github.com/climada-project/climada_python/blob/main/README.md Installs CLIMADA using the Mamba package manager. It is highly recommended to install CLIMADA into a separate Conda environment. ```shell mamba install -c conda-forge climada ``` -------------------------------- ### Install a package using Mamba Source: https://github.com/climada-project/climada_python/blob/main/doc/getting-started/install.rst Prefer installing additional packages via Conda using the conda-forge channel. Use this command to install a specific package. ```shell mamba install -n climada_env -c conda-forge ``` -------------------------------- ### Install CLIMADA Petals Source: https://github.com/climada-project/climada_python/blob/main/doc/getting-started/install.rst Optional step to install CLIMADA Petals into the activated Conda environment. ```shell mamba install -n climada_env -c conda-forge climada-petals ``` -------------------------------- ### Get Signature and Docstring for download_dataset Source: https://github.com/climada-project/climada_python/blob/main/doc/user-guide/climada_util_api_client.ipynb Displays the signature and docstring for the `download_dataset` method, which downloads dataset files. This helps in understanding the parameters for specifying the target directory and path organization. ```python client.download_dataset ``` -------------------------------- ### Define Calibration Input and Run Optimizer Source: https://github.com/climada-project/climada_python/blob/main/doc/user-guide/climada_util_calibrate.ipynb Sets up the necessary input data for calibration and initiates the optimization process using a Bayesian optimizer. ```python with log_level("WARNING", name_prefix="climada.util.calibrate"): input = Input( hazard=hazard, exposure=exposure, data=data, impact_func_creator=impact_func_tc, cost_func=msle, impact_to_dataframe=lambda imp: imp.impact_at_reg(exposure.gdf["region_id"]), bounds=bounds, ) # Create and run the optimizer opt_ae = AverageEnsembleOptimizer(input, optimizer_type=BayesianOptimizer) controller = BayesianOptimizerController.from_input(input) output_ae = opt_ae.run(controller=controller) ``` -------------------------------- ### Install JupyterLab Source: https://github.com/climada-project/climada_python/blob/main/doc/getting-started/install.rst Install JupyterLab into the active Conda environment. This provides a web-based interactive development environment. ```shell mamba install -n climada_env -c conda-forge jupyterlab ``` -------------------------------- ### Install Spyder Kernels in CLIMADA Environment Source: https://github.com/climada-project/climada_python/blob/main/doc/getting-started/install.rst Install the same version of spyder-kernels into the CLIMADA environment as found in the Spyder environment. ```shell mamba install -n climada_env spyder-kernels=X.Y.Z ``` -------------------------------- ### Initialize LitPop from Countries (GDP, with admin1) Source: https://github.com/climada-project/climada_python/blob/main/doc/user-guide/climada_entity_LitPop.ipynb Initiates GDP-based exposure for a country, enabling sub-national GDP data for improved downscaling. ```python ent_adm1 = LitPop.from_countries( "CHE", res_arcsec=120, fin_mode="gdp", admin1_calc=True ) ent_adm1.check() print("Done.") ``` -------------------------------- ### Build Docker Image for Documentation Environment Source: https://github.com/climada-project/climada_python/blob/main/doc/README.md Build a Docker image from the specified Dockerfile to create an isolated environment for building CLIMADA documentation. ```bash docker build -f doc/create_env_doc.dockerfile -t climada_env_doc ./ ``` -------------------------------- ### Install a package using Pip Source: https://github.com/climada-project/climada_python/blob/main/doc/getting-started/install.rst If a package is not available via Conda, activate the CLIMADA environment and use pip to install it. ```shell mamba activate climada_env python -m pip install ``` -------------------------------- ### Initialize LitPop from Countries (GDP, no admin1) Source: https://github.com/climada-project/climada_python/blob/main/doc/user-guide/climada_entity_LitPop.ipynb Initiates GDP-based exposure for a country without using sub-national GDP data for downscaling. ```python # Initiate GDP-Entity for Switzerland, with and without admin1_calc: ent_adm0 = LitPop.from_countries( "CHE", res_arcsec=120, fin_mode="gdp", admin1_calc=False ) ent_adm0.check() ``` -------------------------------- ### Install CLIMADA with developer extras Source: https://github.com/climada-project/climada_python/blob/main/doc/getting-started/install.rst Install CLIMADA in editable mode with developer dependencies. This is required for running tests and pre-commit hooks. ```shell python -m pip install -e "./[dev]" ``` -------------------------------- ### Quickstart: Calibrate TC Impact Function for Mexico Source: https://github.com/climada-project/climada_python/blob/main/doc/user-guide/climada_util_calibrate.ipynb Demonstrates a quick calibration of a tropical cyclone impact function for Mexico using historical data. Requires loading hazard and exposure data, defining impact data, and setting up the calibration input and optimizer. ```python import pandas as pd from climada.util import log_level from climada.util.api_client import Client from climada.entity import ImpactFuncSet, ImpfTropCyclone from climada.util.calibrate import ( Input, BayesianOptimizer, BayesianOptimizerController, OutputEvaluator, msle, ) # Load hazard and exposure from Data API client = Client() exposure = client.get_litpop("MEX") exposure.gdf["impf_TC"] = 1 all_tcs = client.get_hazard( "tropical_cyclone", properties={"event_type": "observed", "spatial_coverage": "global"}, ) hazard = all_tcs.select(event_names=["2010176N16278"]) # Impact data (columns: region ID, index: hazard event ID) data = pd.DataFrame(data=[[2.485465e09]], columns=[484], index=list(hazard.event_id)) # Create input inp = Input( hazard=hazard, exposure=exposure, data=data, # Generate impact function from estimated parameters impact_func_creator=lambda v_half: ImpactFuncSet( [ImpfTropCyclone.from_emanuel_usa(v_half=v_half, impf_id=1)] ), # Estimated parameter bounds bounds={"v_half": (26, 100)}, # Cost function cost_func=msle, # Transform impact to pandas Dataframe with same structure as data impact_to_dataframe=lambda impact: impact.impact_at_reg(exposure.gdf["region_id"]), ) # Set up optimizer (with controller) controller = BayesianOptimizerController.from_input(inp) opt = BayesianOptimizer(inp) # Run optimization with log_level("WARNING", "climada.engine.impact_calc"): output = opt.run(controller=controller) # Analyse results output.plot_p_space() out_eval = OutputEvaluator(inp, output) out_eval.impf_set.plot() # Optimal value output.params ``` -------------------------------- ### Verify CLIMADA Installation Source: https://github.com/climada-project/climada_python/blob/main/doc/getting-started/install.rst Executes a single test to verify that CLIMADA has been installed correctly. This may take some time on the first run. ```shell python -m unittest climada.engine.test.test_impact ``` -------------------------------- ### Accessing Instance-Specific Tricks Source: https://github.com/climada-project/climada_python/blob/main/doc/getting-started/0_intro_python.ipynb Illustrates how each instance of the 'Dog' class maintains its own list of tricks. ```python d.tricks ``` ```python e.tricks ``` -------------------------------- ### Install Specific Climada Version Source: https://github.com/climada-project/climada_python/blob/main/doc/development/Guide_Euler.ipynb Installs a specific released version of Climada using pip. Replace '5.0' with your desired version number. ```bash pip install climada=5.0 ``` -------------------------------- ### Dictionary Creation and Modification Source: https://github.com/climada-project/climada_python/blob/main/doc/getting-started/0_intro_python.ipynb Demonstrates how to create a dictionary, add new key-value pairs, and access existing values. ```python tel = {"jack": 4098, "sape": 4139} tel["guido"] = 4127 tel ``` ```python tel["jack"] ``` -------------------------------- ### Install CLIMADA Environment Source: https://github.com/climada-project/climada_python/blob/main/doc/getting-started/index.rst Use this command to create a new conda environment named 'climada_env' and install CLIMADA with its dependencies from the conda-forge channel. ```bash mamba create -n climada_env -c conda-forge climada ``` -------------------------------- ### Define LitPop Instance Parameters Source: https://github.com/climada-project/climada_python/blob/main/doc/user-guide/climada_engine_unsequa_helper.ipynb Sets up the parameters for creating LitPop instances, including total population, impact function ID, value unit, and other keyword arguments for LitPop.from_countries. Ensure hazard data is loaded. ```python # Define the parameters of the LitPop instances tot_pop = 11.317e6 impf_id = 1 value_unit = "people" litpop_kwargs = { "countries": ["CUB"], "res_arcsec": 150, "reference_year": 2020, "fin_mode": "norm", "total_values": [tot_pop], } assign_centr_kwargs = {} # The hazard is needed to assign centroids from climada.util.constants import HAZ_DEMO_H5 from climada.hazard import Hazard haz = Hazard.from_hdf5(HAZ_DEMO_H5) ``` -------------------------------- ### Install Local CLIMADA Source Source: https://github.com/climada-project/climada_python/blob/main/doc/getting-started/install.rst Installs the local CLIMADA source files as an editable Python package. Changes to source files will be reflected immediately. ```shell python -m pip install -e ./ ``` -------------------------------- ### Initialize LitPop Exposure for Florida using from_shape_and_countries Source: https://github.com/climada-project/climada_python/blob/main/doc/user-guide/climada_entity_LitPop.ipynb Initiates a LitPop exposure for Florida by first creating exposure data for the entire USA and then cropping it to Florida's shape. This method is computationally more intensive than `from_shape` but does not require manual estimation of `total_value`. ```python # `from_shape_and_countries` does not require `total_value`, but is slower to compute than `from_shape`, # because first, the exposure for the whole USA is initiated: start = time.process_time() exp = lp.LitPop.from_shape_and_countries( admin1_shapes[idx], country_iso3a, res_arcsec=600, reference_year=2020 ) print( f"\n Runtime `from_shape_and_countries` : {time.process_time() - start:1.2f} sec.\n" ) exp.plot_scatter(vmin=100, buffer=0.5) ``` -------------------------------- ### Mocking External Downloads for Unit Tests Source: https://github.com/climada-project/climada_python/blob/main/doc/development/Guide_Testing.ipynb Demonstrates how to mock external resource downloads in unit tests. A dummy download function is provided to replace the actual download method, allowing tests to run without external network access. ```python import climada def x(download_file=climada.util.files_handler.download_file): filepath = download_file("http://real_data.ch") return Path(filepath).stat().st_size import unittest class TestX(unittest.TestCase): def download_file_dummy(url): return "phony_data.ch" def test_x(self): self.assertEqual(44, x(download_file=self.download_file_dummy)) ``` -------------------------------- ### Python Lambda Function Example Source: https://github.com/climada-project/climada_python/blob/main/doc/development/Guide_PythonDos-n-Donts.ipynb A concise example of a lambda function, which is a small, anonymous function. This lambda function is used as an argument to a higher-order function. ```python high_ord_func(2, lambda x: x * x) ``` -------------------------------- ### Install Python Dependencies for Climada Source: https://github.com/climada-project/climada_python/blob/main/doc/development/Guide_Euler.ipynb Installs various Python packages required by Climada using pip. Ensure your virtual environment is activated before running this command. ```bash pip install \ dask[dataframe] \ fiona==1.9 \ gdal==3.6 \ netcdf4==1.6.2 \ rasterio==1.4 \ pyproj==3.7 \ geopandas==1.0 \ xarray==2024.9 \ sparse==0.15 ``` -------------------------------- ### Instantiating and Accessing Dog Object Attributes Source: https://github.com/climada-project/climada_python/blob/main/doc/getting-started/0_intro_python.ipynb Shows how to create an instance of the 'Dog' class and access its instance variables like 'name'. ```python d = Dog( "Fido" ) d.name ``` -------------------------------- ### Initialize LitPop with Custom Parameters Source: https://github.com/climada-project/climada_python/blob/main/doc/user-guide/climada_entity_LitPop.ipynb Initiates a LitPop exposure entity for Costa Rica with a specified resolution ('income_group' fin_mode, 120 arcsec resolution, and (1, 1) exponents). This demonstrates how to customize the financial mode, resolution, and exponents for exposure data generation. ```python # Initiate a LitPop exposure entity for Costa Rica with varied resolution, fin_mode, and exponents: exp = LitPop.from_countries( "Costa Rica", fin_mode="income_group", res_arcsec=120, exponents=(1, 1) ) # change the parameters and see what happens... # exp = LitPop.from_countries('Costa Rica', fin_mode='gdp', res_arcsec=90, exponents=(3,0)) # example of variation exp.plot_raster() ``` -------------------------------- ### Create Entity Objects Source: https://github.com/climada-project/climada_python/blob/main/doc/user-guide/climada_engine_CostBenefit.ipynb Initializes Entity objects for present and future scenarios using the defined exposures, discount rates, impact functions, and adaptation measures. This example uses zero discount rates. ```python from climada.entity import Entity entity_present = Entity( exposures=exp_present, disc_rates=discount_zero, impact_func_set=impf_set, measure_set=meas_set, ) entity_future = Entity( exposures=exp_future, disc_rates=discount_zero, impact_func_set=impf_set, measure_set=meas_set, ) ``` -------------------------------- ### Raising a ValueError after catching TypeError (Better but insufficient) Source: https://github.com/climada-project/climada_python/blob/main/doc/development/Guide_Exception_Logging.ipynb This example shows catching a TypeError and then raising a ValueError. It's better than the previous examples but can be improved by chaining exceptions. ```python # Better, but still unsufficient (3) try: l = len(events) if l < 1: raise ValueError("To compute an impact there must be at least one event.") except TypeError: raise TypeError("The provided variable events is not a list") ``` -------------------------------- ### Net Present Values Table Example (Second Instance) Source: https://github.com/climada-project/climada_python/blob/main/doc/user-guide/climada_engine_unsequa.ipynb Presents a second instance of Net Present Values, showing updated figures for cost, benefit, and benefit/cost ratio. This may represent a different scenario or updated data. ```text Measure Cost (USD bn) Benefit (USD bn) Benefit/Cost ----------------- --------------- ------------------ -------------- Mangroves 1.44426 2.77518 1.92153 Beach nourishment 1.90253 2.24443 1.17971 Seawall 9.77553 0.00264385 0.000270456 Building code 10.1292 1.68273 0.166127 -------------------- -------- -------- Total climate risk: 6.73092 (USD bn) Average annual risk: 0.678263 (USD bn) Residual risk: 0.0259328 (USD bn) -------------------- -------- -------- ``` -------------------------------- ### Define LitPop Instance Parameters Source: https://github.com/climada-project/climada_python/blob/main/doc/user-guide/climada_engine_unsequa_helper.ipynb Sets up the parameters required for creating LitPop instances, including country, resolution, reference year, and hazard information. Imports necessary CLIMADA modules. ```python # Define the parameters of the LitPop instances impf_id = 1 value_unit = None litpop_kwargs = { "countries": ["CUB"], "res_arcsec": 300, "reference_year": 2040, } assign_centr_kwargs = {} # The hazard is needed to assign centroids from climada.util.constants import HAZ_DEMO_H5 from climada.hazard import Hazard haz = Hazard.from_hdf5(HAZ_DEMO_H5) ``` -------------------------------- ### Import CLIMADA Calibration Utilities Source: https://github.com/climada-project/climada_python/blob/main/doc/user-guide/climada_util_calibrate.ipynb Imports necessary classes and functions for CLIMADA calibration, including Input, BayesianOptimizer, BayesianOptimizerController, and the msle metric. Also imports log_level for logging configuration. ```python from climada.util.calibrate import ( Input, BayesianOptimizer, BayesianOptimizerController, msle, ) from climada.util import log_level ``` -------------------------------- ### Net Present Values Table Example (Fourth Instance) Source: https://github.com/climada-project/climada_python/blob/main/doc/user-guide/climada_engine_unsequa.ipynb This table shows Net Present Values with significantly different benefit figures, particularly for 'Building code'. It serves as another example of potential outcomes under varying conditions. ```text Measure Cost (USD bn) Benefit (USD bn) Benefit/Cost ----------------- --------------- ------------------ -------------- Mangroves 1.259 10.4834 8.32677 Beach nourishment 1.65849 8.23954 4.96809 Seawall 8.52163 0.415328 0.0487381 Building code 8.82993 36.8908 4.17793 -------------------- -------- -------- Total climate risk: 147.563 (USD bn) Average annual risk: 17.0232 (USD bn) Residual risk: 91.5342 (USD bn) -------------------- -------- -------- ``` -------------------------------- ### Define and Run Bayesian Optimizer Source: https://github.com/climada-project/climada_python/blob/main/doc/user-guide/climada_util_calibrate.ipynb Sets up the input for the Bayesian Optimizer and initiates the optimization process. Requires hazard, exposure, data, an impact function creator, a cost function, an impact-to-dataframe function, and bounds. ```python with log_level("INFO", name_prefix="climada.util.calibrate"): input = Input( hazard=hazard, exposure=exposure, data=data, impact_func_creator=impact_func_tc, cost_func=msle, impact_to_dataframe=lambda imp: imp.impact_at_reg(exposure.gdf["region_id"]), bounds=bounds, ) # Create and run the optimizer opt = BayesianOptimizer(input) controller = BayesianOptimizerController.from_input(input) bayes_output = opt.run(controller=controller) bayes_output.params # The optimal parameters ``` -------------------------------- ### Order Samples for Efficient Loading Source: https://github.com/climada-project/climada_python/blob/main/doc/user-guide/climada_engine_unsequa.ipynb Generates samples and then orders them by hazard ('f_haz') and then by exposures ('f_exp') to minimize file loading times. ```python # Ordering of the samples by hazard first and exposures second output_imp = calc_imp.make_sample(N=2**2, sampling_kwargs={"skip_values": 2**3}) output_imp.order_samples(by=["f_haz", "f_exp"]) ``` -------------------------------- ### Export Spyder Environment Source: https://github.com/climada-project/climada_python/blob/main/doc/getting-started/install.rst Export the conda environment for Spyder to inspect installed packages, specifically spyder-kernels. ```shell mamba env export -n spyder-env | grep spyder-kernels ``` -------------------------------- ### Remove Conda Environment Source: https://github.com/climada-project/climada_python/blob/main/doc/getting-started/install.rst Removes a specified Conda environment. Use this if you need to switch installation methods. ```shell mamba env remove -n climada_env ``` -------------------------------- ### Initialize GEE and Load an Image Source: https://github.com/climada-project/climada_python/blob/main/doc/user-guide/climada_util_earth_engine.ipynb Initializes the Earth Engine API and loads a sample image. This is a fundamental step for interacting with GEE. ```python import webbrowser import ee ee.Initialize() image = ee.Image("srtm90_v4") print(image.getInfo()) ``` -------------------------------- ### Python String Slicing Source: https://github.com/climada-project/climada_python/blob/main/doc/getting-started/0_intro_python.ipynb Shows how to extract substrings from a string using slicing with start and end indices. ```python print("Characters from position 0 (included) to 2 (excluded): word[0:2] =", word[0:2]) print("Characters from position 2 (included) to 5 (excluded): word[2:5] =", word[2:5]) ``` -------------------------------- ### Download a Dataset File Source: https://github.com/climada-project/climada_python/blob/main/doc/user-guide/climada_util_api_client.ipynb Downloads a specific dataset file using the `download_dataset` method. This example demonstrates how to download a litpop dataset and shows the resulting file path and its existence. The method handles caching to avoid redundant downloads. ```python # Let's have a look at an example for downloading a litpop dataset first ds = litpop_datasets[ 0 ] # litpop_datasets is a list and download_dataset expects a single object as argument. download_dir, ds_files = client.download_dataset(ds) ds_files[0], ds_files[0].is_file() ``` -------------------------------- ### Load and Prepare Entity Data for Future Scenarios Source: https://github.com/climada-project/climada_python/blob/main/script/applications/eca_san_salvador/San_Salvador_Parametric.ipynb Loads entity data and prepares it for future scenarios by adjusting the reference year and applying economic growth to exposure values. Checks the validity of the prepared data. ```python ent_2015 = Entity.from_excel("FL_entity_Acelhuate_houses.xlsx") ent_2015.exposures.ref_year = 2015 # fix reference year ent_2015.check() ent_2040 = copy.deepcopy(ent_2015) # Exposures: yearly economic growth of 2% in exposures ent_2040.exposures.ref_year = 2040 growth = 0.02 ent_2040.exposures.gdf["value"] = ent_2040.exposures.gdf.value.values * (1 + growth) ** (ent_2040.exposures.ref_year - ent_2015.exposures.ref_year) ent_2040.check() # check values are well set and assignes default values # flood as for 2040 with extreme climate change HAZ_FILE = "Salvador_hazard_FL_2040_extreme_cc.hdf5" haz_2040 = Hazard.from_hdf5(HAZ_FILE) # load file ``` -------------------------------- ### Get Help for Hazard Plotting Functions Source: https://github.com/climada-project/climada_python/blob/main/doc/user-guide/climada_hazard_Hazard.ipynb Retrieves help documentation for the `plot_intensity` and `plot_rp_intensity` methods of a Hazard object. ```python help(haz_tc_fl.plot_intensity) help(haz_tc_fl.plot_rp_intensity) ``` -------------------------------- ### Checkout a specific branch Source: https://github.com/climada-project/climada_python/blob/main/doc/getting-started/install.rst Switch to a specific Git branch for CLIMADA development. Ensure CLIMADA is installed in editable mode. ```shell git fetch git checkout git pull ``` -------------------------------- ### Initialize CLIMADA API Client Source: https://github.com/climada-project/climada_python/blob/main/doc/user-guide/climada_util_api_client.ipynb Instantiates the CLIMADA API client. This is the first step before making any API requests. ```python from climada.util.api_client import Client client = Client() ``` -------------------------------- ### Calling Functions with Keyword Arguments Source: https://github.com/climada-project/climada_python/blob/main/doc/getting-started/0_intro_python.ipynb Demonstrates calling a function using keyword arguments for clarity and flexibility. This is useful when a function has many parameters or when you want to specify only a subset of them. ```python ask_ok("OK to overwrite the file?", reminder="Come on, only yes or no!") ``` -------------------------------- ### Start a New Feature Branch (Git Flow) Source: https://github.com/climada-project/climada_python/blob/main/doc/development/Guide_CLIMADA_Development.ipynb Initiate a new feature branch using the Git Flow convention. ```bash git flow feature start feature_name ``` -------------------------------- ### Initialize LitPop Exposure for Florida using from_shape Source: https://github.com/climada-project/climada_python/blob/main/doc/user-guide/climada_entity_LitPop.ipynb Initiates a LitPop exposure for the state of Florida using its shape and an estimated total value. This method is resource-efficient but requires manual estimation of `total_value`. It first retrieves administrative shape data, estimates the total value as a percentage of the country's total, and then creates the exposure object. ```python import time import climada.util.coordinates as u_coord import climada.entity.exposures.litpop as lp country_iso3a = "USA" state_name = "Florida" reslution_arcsec = 600 """First, we need to get the shape of Florida:""" admin1_info, admin1_shapes = u_coord.get_admin1_info(country_iso3a) admin1_info = admin1_info[country_iso3a] admin1_shapes = admin1_shapes[country_iso3a] admin1_names = [record["name"] for record in admin1_info] print(admin1_names) for idx, name in enumerate(admin1_names): if admin1_names[idx] == state_name: break print("Florida index: " + str(idx)) """Secondly, we estimate the `total_value`""" # `total_value` required user input for `from_shape`, here we assume 5% of total value of the whole USA: total_value = 0.05 * lp._get_total_value_per_country(country_iso3a, "pc", 2020) """Then, we can initiate the exposures for Florida:""" start = time.process_time() exp = lp.LitPop.from_shape( admin1_shapes[idx], total_value, res_arcsec=600, reference_year=2020 ) print(f"\n Runtime `from_shape` : {time.process_time() - start:1.2f} sec.\n") exp.plot_scatter(vmin=100, buffer=0.5); ``` -------------------------------- ### Overdoing Constant Definitions Source: https://github.com/climada-project/climada_python/blob/main/doc/development/Guide_Configuration.ipynb Shows an example where defining too many strings as constants can lead to unnecessary complexity and reduced readability. ```python # possibly overdoing it X = "x" Y = "this doesn't mean that every string must be a constant" my_dict = {X: 4} if my_dict[X] > 3: msg = Y msg ``` -------------------------------- ### Creating and Displaying a Set Source: https://github.com/climada-project/climada_python/blob/main/doc/getting-started/0_intro_python.ipynb Demonstrates how to create a set from a list and how duplicates are automatically removed. ```python basket = {"apple", "orange", "apple", "pear", "orange", "banana"} basket # show that duplicates have been removed ``` -------------------------------- ### Get CLIMADA Environment Python Interpreter Path Source: https://github.com/climada-project/climada_python/blob/main/doc/getting-started/install.rst Activate the CLIMADA conda environment and print the path to its Python executable. ```shell mamba activate climada_env python -c "import sys; print(sys.executable)" ``` -------------------------------- ### Memory Profiling with %memit Source: https://github.com/climada-project/climada_python/blob/main/doc/development/Guide_Py_Performance.ipynb Use the %memit magic command to profile memory usage of a single expression. Ensure 'memory_profiler' is installed. ```python %load_ext memory_profiler ``` ```python # long expression in one line %memit arr_d = arr_c * (arr_a + arr_b) - arr_a + arr_c ``` -------------------------------- ### Create Dummy Impact Object Source: https://github.com/climada-project/climada_python/blob/main/doc/user-guide/climada_util_yearsets.ipynb Initializes a dummy Impact object with sample event data for demonstration purposes. This object is used as input for subsequent calculations. ```python import numpy as np import climada.util.yearsets as yearsets from climada.engine import Impact # dummy event_impacts object containing 12 event impacts imp = Impact( event_id=np.arange(6) + 10, event_name=np.arange(6) + 10, date=np.arange(6), coord_exp=np.array([[1, 2], [1.5, 2.5]]), eai_exp=np.array([13.4, 13.4]), at_event=np.array([0, 2, 4, 6, 60, 62]), frequency=np.full(6, 0.2), aai_agg=26.8, ) ``` -------------------------------- ### Get Uncertainty Data Source: https://github.com/climada-project/climada_python/blob/main/doc/user-guide/climada_engine_unsequa.ipynb Retrieves uncertainty data for specified metrics. Use tail() to view the last few rows of the resulting DataFrame. ```python output_cb.get_uncertainty(metric_list=["imp_meas_present"]).tail() ``` -------------------------------- ### Generate Plots for Risk Assessment Source: https://github.com/climada-project/climada_python/blob/main/script/applications/eca_san_salvador/San_Salvador_Risk.ipynb Generates various plots used throughout the risk assessment script. This snippet is for setup and visualization. ```python %%capture # generate plots used in this script import functions_ss fig_ma, fig_point, fig_houses, fig_if = functions_ss.generate_plots_risk() ```