Skip to content

Commit

Permalink
Merge pull request #152 from zsarnoczay/master
Browse files Browse the repository at this point in the history
azs - update to pelicun 3.1
  • Loading branch information
fmckenna committed Oct 1, 2022
2 parents 3d24ffa + c8c8723 commit 0570889
Show file tree
Hide file tree
Showing 13 changed files with 1,877 additions and 1,522 deletions.
2 changes: 1 addition & 1 deletion conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class simCenterBackendApps(ConanFile):
"mkl-static/2019.4@simcenter/stable", \
"ipp-static/2019.4@simcenter/stable", \
"nanoflann/1.3.2", \
"nlopt/2.6.2",\
"nlopt/2.7.1",\

# Custom attributes for Bincrafters recipe conventions
_source_subfolder = "source_subfolder"
Expand Down
236 changes: 134 additions & 102 deletions modules/performDL/pelicun3/DL_calculation.py

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions modules/performDL/pelicun3/HDF_to_CSV.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@
# Adam Zsarnóczay

import pandas as pd
import sys, argparse
import sys
import argparse
from pathlib import Path


def convert_HDF(HDF_path):

HDF_ext = HDF_path.split('.')[-1]
Expand All @@ -56,6 +58,7 @@ def convert_HDF(HDF_path):

store.close()


if __name__ == '__main__':

args = sys.argv[1:]
Expand All @@ -65,4 +68,4 @@ def convert_HDF(HDF_path):

args = parser.parse_args(args)

convert_HDF(args.HDF_path)
convert_HDF(args.HDF_path)
9 changes: 5 additions & 4 deletions modules/performDL/pelicun3/pelicun/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@

name = "pelicun"

__version__ = '3.1.b8'
__version__ = '3.1'

__copyright__ = """Copyright (c) 2018 Leland Stanford Junior University and
The Regents of the University of California"""
__copyright__ = ("Copyright (c) 2018 Leland Stanford "
"Junior University and The Regents "
"of the University of California")

__license__ = "BSD 3-Clause License"
__license__ = "BSD 3-Clause License"
179 changes: 149 additions & 30 deletions modules/performDL/pelicun3/pelicun/assessment.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,15 @@
"""

from .base import *
from .file_io import *
from .model import *
import json
from datetime import datetime
from . import base
from . import file_io
from . import model
from .__init__ import __version__ as pelicun_version

class Assessment(object):

class Assessment:
"""
Assessment objects manage the models, data, and calculations in pelicun.
Expand All @@ -68,79 +72,86 @@ class Assessment(object):
...
stories: int
Number of stories.
options: Options
Options object.
"""

def __init__(self, config_options=None):

load_default_options()

set_options(merge_default_config(config_options))
self.stories = None
self.options = base.Options(self)

file_io.load_default_options(self.options)

log_msg(f'pelicun {pelicun_version} | \n',
prepend_timestamp=False, prepend_blank_space=False)
self.options.set_options(file_io.merge_default_config(
config_options, self.options))
self.unit_conversion_factors = file_io.parse_units(
self.options.units_file)

print_system_info()
self.log_msg(f'pelicun {pelicun_version} | \n',
prepend_timestamp=False, prepend_blank_space=False)

log_div()
log_msg('Assessement Started')
base.print_system_info(self)

self.stories = None
self.log_div()
self.log_msg('Assessement Started')

@property
def demand(self):
"""
Return a DemandModel object that manages the demand information.
"""
# pylint: disable = access-member-before-definition

if hasattr(self, '_demand'):
return self._demand

else:
self._demand = DemandModel()
return self.demand
self._demand = model.DemandModel(self)
return self.demand

@property
def asset(self):
"""
Return an AssetModel object that manages the asset information.
"""
# pylint: disable = access-member-before-definition

if hasattr(self, '_asset'):
return self._asset

else:
self._asset = AssetModel(self)
return self.asset
self._asset = model.AssetModel(self)
return self.asset

@property
def damage(self):
"""
Return an DamageModel object that manages the damage information.
"""
# pylint: disable = access-member-before-definition

if hasattr(self, '_damage'):
return self._damage

else:
self._damage = DamageModel(self)
return self.damage
self._damage = model.DamageModel(self)
return self.damage

@property
def bldg_repair(self):
"""
Return an BldgRepairModel object that manages the repair information.
"""
# pylint: disable = access-member-before-definition

if hasattr(self, '_bldg_repair'):
return self._bldg_repair

else:
self._bldg_repair = BldgRepairModel(self)
return self.bldg_repair
self._bldg_repair = model.BldgRepairModel(self)
return self.bldg_repair

def get_default_data(self, data_name):
"""
Expand All @@ -153,10 +164,11 @@ def get_default_data(self, data_name):
"""

data_path = str(pelicun_path)+'/resources/'+data_name+'.csv'

return load_data(data_path, orientation=1, reindex=False, convert=[])
data_path = str(base.pelicun_path)+'/resources/'+data_name+'.csv'

return file_io.load_data(
data_path, self.unit_conversion_factors,
orientation=1, reindex=False, convert=[])

def get_default_metadata(self, data_name):
"""
Expand All @@ -169,9 +181,116 @@ def get_default_metadata(self, data_name):
"""

data_path = str(pelicun_path) + '/resources/' + data_name + '.json'
data_path = str(base.pelicun_path) + '/resources/' + data_name + '.json'

with open(data_path, 'r') as f:
with open(data_path, 'r', encoding='utf-8') as f:
data = json.load(f)

return data
return data

def log_div(self, prepend_timestamp=False):
"""
Print a divider line to the log file
"""

if prepend_timestamp:
msg = self.options.log_div

else:
msg = '-' * 80

self.log_msg(msg, prepend_timestamp=prepend_timestamp)


def log_msg(self, msg='', prepend_timestamp=True, prepend_blank_space=True):
"""
Print a message to the screen with the current time as prefix
The time is in ISO-8601 format, e.g. 2018-06-16T20:24:04Z
Parameters
----------
msg: string
Message to print.
"""

# pylint: disable = consider-using-f-string
msg_lines = msg.split('\n')

for msg_i, msg_line in enumerate(msg_lines):

if (prepend_timestamp and (msg_i == 0)):
formatted_msg = '{} {}'.format(
datetime.now().strftime(self.options.log_time_format), msg_line)
elif prepend_timestamp:
formatted_msg = self.options.log_pref + msg_line
elif prepend_blank_space:
formatted_msg = self.options.log_pref + msg_line
else:
formatted_msg = msg_line

if self.options.print_log:
print(formatted_msg)

if self.options.log_file is not None:
with open(self.options.log_file, 'a', encoding='utf-8') as f:
f.write('\n'+formatted_msg)


def calc_unit_scale_factor(self, unit):
"""
Determines the scale factor from input unit to the corresponding SI unit
Parameters
----------
unit: str
Either a unit name, or a quantity and a unit name separated by a space.
For example: 'ft' or '100 ft'.
Returns
-------
scale_factor: float
Scale factor that convert values from unit to SI unit
Raises
------
KeyError:
When an invalid unit is specified
"""

unit_lst = unit.strip().split(' ')

# check if there is a quantity specified; if yes, parse it
if len(unit_lst) > 1:
unit_count, unit_name = unit_lst
unit_count = float(unit_count)

else:
unit_count = 1
unit_name = unit_lst[0]

try:
scale_factor = unit_count * self.unit_conversion_factors[unit_name]

except KeyError as exc:
raise KeyError(f"Specified unit not recognized: "
f"{unit_count} {unit_name}") from exc

return scale_factor

def scale_factor(self, unit):

if unit is not None:

if unit in self.unit_conversion_factors:
scale_factor = self.unit_conversion_factors[unit]

else:
raise ValueError(f"Unknown unit: {unit}")
else:
scale_factor = 1.0

return scale_factor

Loading

0 comments on commit 0570889

Please sign in to comment.