Skip to content
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: 1 addition & 1 deletion .github/workflows/linux_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ jobs:
run: |
source ~/openquake/bin/activate
cd openquake/vmtk
pytest --doctest-modules -x --color=yes --durations=10 tests/
pytest -v --doctest-modules -x --color=yes --durations=10 tests/
45 changes: 45 additions & 0 deletions .github/workflows/windows_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Windows tests
on:
pull_request:
workflow_dispatch:
inputs:
git-ref:
description: Git Ref
default: master
required: true
debug_enabled:
type: boolean
description: 'tmate debug enabled'
required: false
default: false
schedule:
- cron: "0 2 * * 0"

jobs:
vulnerability_test:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [windows-2022]
python-version: ["3.10", "3.11", "3.12"]
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install Dependencies of OpenQuake engine
run: |
curl.exe -LO https://github.com/gem/oq-engine/raw/master/install.py
python install.py user --version=master
- name: Install vmtk Package
run: |
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
C:\Users\runneradmin\openquake\Scripts\activate.ps1
pip install -e .
- name: Run tests
run: |
C:\Users\runneradmin\openquake\Scripts\activate.ps1
cd openquake\vmtk
pytest -v --doctest-modules -x --color=yes --durations=10 tests/
25 changes: 12 additions & 13 deletions demos/notebooks/example_1.ipynb → demos/example_1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,7 @@
"\n",
"# Import the IMCalculator class\n",
"from openquake.vmtk.im_calculator import IMCalculator \n",
"from openquake.vmtk.utilities import sorted_alphanumeric, export_to_pkl\n",
"\n",
"# Change the working directory to the folder containing your notebook\n",
"os.chdir('/Users/mnafeh/Documents/GitHub/vulnerability-toolkit/demos/notebooks') # Replace this with your desired directory\n",
"\n",
"# Verify the current directory\n",
"print(\"Current working directory:\", os.getcwd())"
"from openquake.vmtk.utilities import sorted_alphanumeric, export_to_pkl"
]
},
{
Expand Down Expand Up @@ -123,7 +117,7 @@
"IMT = ['PGA','PGV','PGD','SA(0.3s)','SA(0.6s)','SA(1.0s)','AvgSA','AvgSA(0.3s)','AvgSA(0.6s)','AvgSA(1.0s)','AI','D595']\n",
"\n",
"# Load the records directory which contains two folders where the time and acceleration arrays of each ground-motion record are stored\n",
"gm_directory = os.path.join(os.getcwd(), 'in', 'records') \n",
"gm_directory = './in/records' \n",
"\n",
"# Fetch the acceleration files\n",
"gmrs = sorted_alphanumeric(os.listdir(os.path.join(gm_directory, 'acc')))\n",
Expand Down Expand Up @@ -281,11 +275,16 @@
" imls[current_imt].append(t595) \n",
"\n",
"# Export to pickle format \n",
"export_to_pkl(os.path.join(gm_directory,'imls_esrm20.pkl'), imls)\n",
"\n",
"# Visualise the dictionary\n",
"print(imls)"
"export_to_pkl(os.path.join(gm_directory,'imls_esrm20.pkl'), imls)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "26a56310-fcf6-4a7a-ae71-22eb85a87bf7",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -304,7 +303,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.7"
"version": "3.11.9"
}
},
"nbformat": 4,
Expand Down
81 changes: 23 additions & 58 deletions demos/notebooks/example_2.ipynb → demos/example_2.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -48,39 +48,23 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"id": "0d7fbfbf-e29a-4403-983f-932d528c6a18",
"metadata": {},
"outputs": [
{
"ename": "ModuleNotFoundError",
"evalue": "No module named 'vmtk'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[1], line 8\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mmatplotlib\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mpyplot\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01mplt\u001b[39;00m\n\u001b[1;32m 7\u001b[0m \u001b[38;5;66;03m# Import the classes necessary for structural analysis\u001b[39;00m\n\u001b[0;32m----> 8\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mvmtk\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mcalibration\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m calibrate_model\n\u001b[1;32m 9\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mvmtk\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mmodeller\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m modeller\n\u001b[1;32m 10\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mvmtk\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mutilities\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m sorted_alphanumeric, import_from_pkl, export_to_pkl\n",
"\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'vmtk'"
]
}
],
"outputs": [],
"source": [
"import os\n",
"import sys\n",
"import shutil\n",
"import numpy as np\n",
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
"\n",
"# Import the classes necessary for structural analysis\n",
"from openquake.vmtk.units import units\n",
"from openquake.vmtk.calibration import calibrate_model\n",
"from openquake.vmtk.modeller import modeller\n",
"from openquake.vmtk.utilities import sorted_alphanumeric, import_from_pkl, export_to_pkl\n",
"\n",
"# Change the working directory to the folder containing your notebook\n",
"os.chdir('/Users/mnafeh/Documents/GitHub/vulnerability-toolkit/demos/notebooks') # Replace this with your desired directory\n",
"\n",
"# Verify the current directory\n",
"print(\"Current working directory:\", os.getcwd())"
"from openquake.vmtk.utilities import sorted_alphanumeric, import_from_pkl, export_to_pkl"
]
},
{
Expand Down Expand Up @@ -131,15 +115,14 @@
"outputs": [],
"source": [
"# Define the directory of the capacities\n",
"capacity_directory = f'{vmtk}/demos/in/capacity' \n",
"capacity_directory = './in/capacity' \n",
"\n",
"# Define the directory of the ground-motion records\n",
"gm_directory = f'{vmtk}/demos/in/records' \n",
"gm_directory = './in/records' \n",
"\n",
"# Define the main output directory\n",
"nltha_directory = f'{vmtk}/demos/out/nltha' \n",
"if not os.path.exists(f'{nltha_directory}'):\n",
" os.makedirs(f'{nltha_directory}')"
"nrha_directory = './out/nltha' \n",
"os.makedirs(nrha_directory, exist_ok=True)"
]
},
{
Expand Down Expand Up @@ -273,10 +256,9 @@
"mdof_max_peak_accel_dir_list = [] # List for maximum peak floor acceleration directions \n",
"mdof_max_peak_accel_loc_list = [] # List for maximum peak floor acceleration locations \n",
"\n",
"# Define directory for temporary analysis outputs\n",
"temp_nrha_outdir = f'{nltha_directory}/temp'\n",
"if not os.path.exists(f'{temp_nrha_outdir}'):\n",
" os.makedirs(f'{temp_nrha_outdir}')"
"# Define directory for temporary analysis outputs: it is used to store temporary .txt files used as accelerations recorders\n",
"temp_nrha_directory = os.path.join(nrha_directory,'temp')\n",
"os.makedirs(temp_nrha_directory, exist_ok=True)"
]
},
{
Expand All @@ -287,8 +269,8 @@
"outputs": [],
"source": [
"# Loop over ground-motion records, compile MDOF model and run NLTHA\n",
"gmrs = sorted_alphanumeric(os.listdir(f'{gm_directory}/acc')) # Sort the ground-motion records alphanumerically\n",
"dts = sorted_alphanumeric(os.listdir(f'{gm_directory}/dts')) # Sort the ground-motion time-step files alphanumerically\n",
"gmrs = sorted_alphanumeric(os.listdir(os.path.join(gm_directory,'acc'))) # Sort the ground-motion records alphanumerically\n",
"dts = sorted_alphanumeric(os.listdir(os.path.join(gm_directory,'dts'))) # Sort the ground-motion time-step files alphanumerically\n",
"\n",
"for i in range(len(gmrs)):\n",
" ### Print post-processing iteration\n",
Expand All @@ -307,19 +289,18 @@
" model.compile_model() # Compile the MDOF model\n",
" \n",
" if i==0:\n",
" model.plot_model() # Visualise the model (only on first iteration)\n",
" \n",
" model.plot_model() # Visualise the model (only on first iteration) \n",
" model.do_gravity_analysis() # Do gravity analysis\n",
"\n",
" if number_storeys == 1:\n",
" num_modes = 1\n",
" else:\n",
" num_modes = 3\n",
" T, _ = model.do_modal_analysis(num_modes = num_modes) # Do modal analysis and get period of vibration\n",
" T, phi = model.do_modal_analysis(num_modes = num_modes) # Do modal analysis and get period of vibration (Essential step for running NLTHA)\n",
"\n",
" ### Define ground motion objects\n",
" fnames = [f'{gm_directory}/acc/{gmrs[i]}'] # Ground-motion record names\n",
" fdts = f'{gm_directory}/dts/{dts[i]}' # Ground-motion time-step names \n",
" fnames = [os.path.join(gm_directory,'acc',f'{gmrs[i]}')] # Ground-motion record names\n",
" fdts = os.path.join(gm_directory,'dts',f'{dts[i]}') # Ground-motion time-step names \n",
" dt_gm = pd.read_csv(fdts, header=None)[pd.read_csv(fdts,header=None).columns[0]].loc[1]-\\\n",
" pd.read_csv(fdts, header=None)[pd.read_csv(fdts,header=None).columns[0]].loc[0] # Ground-motion time-step\n",
" t_max = pd.read_csv(fdts)[pd.read_csv(fdts).columns[0]].iloc[-1] # Ground-motion duration\n",
Expand Down Expand Up @@ -348,6 +329,9 @@
" mdof_max_peak_accel_dir_list.append(max_peak_accel_dir)\n",
" mdof_max_peak_accel_loc_list.append(max_peak_accel_loc)\n",
"\n",
"# Remove the temporary directory\n",
"shutil.rmtree(f'{temp_nrha_directory}')\n",
"\n",
"print('ANALYSIS COMPLETED')"
]
},
Expand Down Expand Up @@ -377,27 +361,8 @@
"for i, label in enumerate(labels):\n",
" ansys_dict[label] = vars()[f'{label}']\n",
"# Export the analysis output variable to a pickle file using the \"export_to_pkl\" function from \"utilities\"\n",
"export_to_pkl(f'{nltha_directory}/ansys_out.pkl', ansys_dict) "
"export_to_pkl(os.path.join(nrha_directory,'ansys_out.pkl'), ansys_dict) "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c597d643-c4a5-4794-85e7-f3bc29094f79",
"metadata": {},
"outputs": [],
"source": [
"end = time.time()\n",
"print('Elapsed Time:', (end - start)/60, 'minutes')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6ca99f93-fd56-4abc-9b17-3a378b258f03",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -416,7 +381,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.7"
"version": "3.11.9"
}
},
"nbformat": 4,
Expand Down
61 changes: 26 additions & 35 deletions demos/notebooks/example_3.ipynb → demos/example_3.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,14 @@
"source": [
"import os\n",
"import sys\n",
"import time\n",
"import numpy as np\n",
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
"\n",
"start = time.time()\n",
"\n",
"# Set paths\n",
"vmtk = os.path.abspath(os.path.join(\"..\",\"..\"))\n",
"sys.path.append(vmtk)\n",
"os.chdir(os.path.join(vmtk,'src'))\n",
"\n",
"# Import the classes necessary for postprocessing and visualising structural analysis results\n",
"from src.postprocessor import postprocessor\n",
"from src.plotter import plotter\n",
"from src.utilities import import_from_pkl\n",
"from openquake.vmtk.postprocessor import postprocessor\n",
"from openquake.vmtk.plotter import plotter\n",
"from openquake.vmtk.utilities import import_from_pkl\n",
"\n",
"# Initialise the postprocessor and plotter classes\n",
"pp = postprocessor()\n",
Expand Down Expand Up @@ -141,12 +133,14 @@
"outputs": [],
"source": [
"# Define the directory of the ground-motion records and import the intensity measure pickle file containing all IMs (processed from example 1)\n",
"gm_directory = f'{vmtk}/demos/in/records' \n",
"ims = import_from_pkl(f'{gm_directory}/imls_esrm20.pkl') \n",
"gm_directory = './in/records' \n",
"\n",
"# Import the intensity measure dictionary (output from example 1)\n",
"ims = import_from_pkl(os.path.join(gm_directory, 'imls_esrm20.pkl')) \n",
"\n",
"# Define the main output directory and import the analysis output from a pickle file using the \"import_from_pkl\" function from \"utilities\"\n",
"nltha_directory = f'{vmtk}/demos/out/nltha' \n",
"ansys_dict = import_from_pkl(f'{nltha_directory}/ansys_out.pkl') # processed from example 2"
"nrha_directory = './out/nltha' \n",
"ansys_dict = import_from_pkl(os.path.join(nrha_directory, 'ansys_out.pkl')) # processed from example 2"
]
},
{
Expand Down Expand Up @@ -196,9 +190,8 @@
" censored_limit) \n",
" \n",
" ## Create a figures directory for each building class\n",
" figures_directory = f'{vmtk}/demos/out/nltha/figures' \n",
" if not os.path.exists(f'{figures_directory}'):\n",
" os.makedirs(f'{figures_directory}')\n",
" figures_directory = './out/figures' \n",
" os.makedirs(figures_directory, exist_ok=True)\n",
" \n",
" ## Visualise the cloud analysis results\n",
" pl.plot_cloud_analysis(cloud_dict, \n",
Expand Down Expand Up @@ -250,35 +243,33 @@
"# To do so, we can use the \"get_vulnerability_function\" method from the \"postprocessor\" class.\n",
"# Setting the uncertainty to True will additionally calculate the coefficient of variation to explicitly\n",
"# consider the uncertainty in the Loss|IM as per Silva et al. (2019)\n",
"\n",
"structural_vulnerability = pp.get_vulnerability_function(cloud_dict['poes'],\n",
" consequence_model,\n",
" uncertainty=True)\n",
"\n",
"\n",
"# The output is a DataFrame with three keys: IMLs (i.e., intensity measure levels), Loss and COV\n",
"print(structural_vulnerability)\n",
"# Plot the structural vulnerability function\n",
"pl.plot_vulnerability_analysis(structural_vulnerability['IMLs'],\n",
" structural_vulnerability['Loss'],\n",
" structural_vulnerability['COV'],\n",
" 'SA(1.0s)',\n",
" 'Structural Loss Ratio',\n",
" figures_directory,\n",
" 'Structural Vulnerability')\n",
"\n",
"\n",
"# Plot the vulnerability function to visualise\n",
"plt.plot(structural_vulnerability['IMLs'], structural_vulnerability['Loss'], label = 'Structural Loss', color = COLOR, lw=LINEWIDTH_1)\n",
"plt.xlabel('Spectral Acceleration, Sa(T=1.0s) [g]', fontsize= FONTSIZE_1) # This is because the latest cloud_dict variable is associated with SA(1.0s)\n",
"plt.ylabel('Expected Loss Ratio', fontsize = FONTSIZE_1)\n",
"plt.legend(loc = 'lower right')\n",
"plt.grid(visible=True, which='major')\n",
"plt.grid(visible=True, which='minor')\n",
"plt.xlim([0.00, 5.00])\n",
"plt.show()"
"# The output is a DataFrame with three keys: IMLs (i.e., intensity measure levels), Loss and COV\n",
"print(structural_vulnerability)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "39c357a7-e6b1-43b3-a58b-f9cee926cdc9",
"id": "a587cb59-faa8-4f1f-bb5a-445322cfff51",
"metadata": {},
"outputs": [],
"source": [
"end = time.time()\n",
"print('Elapsed Time:', (end - start)/60, 'minutes')"
]
"source": []
}
],
"metadata": {
Expand All @@ -297,7 +288,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.7"
"version": "3.11.9"
}
},
"nbformat": 4,
Expand Down
Binary file added demos/in/records/imls_esrm20.pkl
Binary file not shown.
Loading