Skip to content

Commit

Permalink
Fix a bug related to z1 out of range issue
Browse files Browse the repository at this point in the history
  • Loading branch information
jsh9 committed Sep 27, 2019
1 parent 2b52ac9 commit 6726bf2
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 58 deletions.
2 changes: 1 addition & 1 deletion PySeismoSoil/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Author: Jian Shi

__version__ = '0.2.6'
__version__ = 'v0.2.7'
56 changes: 24 additions & 32 deletions PySeismoSoil/class_site_factors.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,24 @@ def __init__(self, Vs30_in_meter_per_sec, z1_in_m, PGA_in_g,

status = Site_Factors._range_check(Vs30_in_meter_per_sec, z1_in_m,
PGA_in_g)
if 1 in status:
if 'Vs30 out of range' in status:
if not lenient:
raise ValueError('Vs30 should be between [175, 950] m/s')
else:
Vs30_in_meter_per_sec = 175 if Vs30_in_meter_per_sec < 175 else 950
if 2 in status:
if 'z1 out of range' in status:
if not lenient:
raise ValueError('z1_in_m should be between [8, 900] m')
else:
z1_in_m = 8 if z1_in_m < 8 else 900
if 3 in status:
if 'PGA out of range' in status:
if not lenient:
raise ValueError('PGA should be between [0.01g, 1.5g]')
else:
PGA_in_g = 0.01 if PGA_in_g < 0.01 else 1.5
if 4 in status: # TODO: add leniency for out-of-bound combinations
raise ValueError('Vs30 and z1 combination not valid')
if 'Invalid Vs30-z1 combination' in status: # TODO: think about whether to add leniency
raise ValueError('Vs30 and z1 combination not valid. (The `lenient` '
'option does not apply to this type of issue.)')

self.Vs30 = Vs30_in_meter_per_sec
self.z1 = z1_in_m
Expand All @@ -80,7 +81,7 @@ def get_amplification(self, method='nl_hh', Fourier=True,
---------
method : {'nl_hh', 'eq_hh'}
Which site response simulation method was used to calculate the
amplification factors. 'nl_hh' uses the results from nonlinear site
amplification factors. 'nl_hh' uses the results from nonlinear site
response simulation, which is recommended.
Fourier : bool
Whether or not to return Fourier-spectra-based amplification
Expand Down Expand Up @@ -323,7 +324,6 @@ def _find_neighbors(Vs30_in_mps, z1_in_m, PGA_in_g):
The three inputs need to already within the correct range.
'''

Vs30_loc = Site_Factors._search_sorted(Vs30_in_mps, Site_Factors.Vs30_array)
z1_loc = Site_Factors._search_sorted(z1_in_m, Site_Factors.z1_array)
PGA_loc = Site_Factors._search_sorted(PGA_in_g, Site_Factors.PGA_array)
Expand All @@ -345,13 +345,11 @@ def _search_sorted(value, array):
In: _search_sorted(0, [0, 1, 2, 3, 4, 5])
Out: [0, 1]
It is assumed that ``value`` is already within ``array``. If you
want to add robustness for cases where value < min(array) or
value > max(array), just add two "``if``" statements::
if value < array[0] or value > array[-1]
'''

if value < array[0] or value > array[-1]:
raise ValueError('You have encountered an internal bug. Please '
'copy the whole error message, and contact '
'the author of this library for help.')
if value == array[0]:
return [0, 1]
if value == array[-1]:
Expand Down Expand Up @@ -522,13 +520,9 @@ def _range_check(Vs30_in_mps, z1_in_m, PGA_in_g):
Check if the provided Vs30, z1_in_m, and PGA_in_g values are within
the pre-computed range.
Meanings of different return values:
1 - Vs30 outside of range [175, 950] m/s
2 - z1_in_m outside of range [8, 900] m
3 - PGA outside of range [0.01g, 1.5g]
4 - Vs30 and z1 combination is not valid
The return value (``status``) indicates the kind(s) of errors
associated with the given input parameters.
'''

if not isinstance(Vs30_in_mps, (float, int, np.number)):
raise TypeError('Vs30 must be int, float, or numpy.number.')
if not isinstance(z1_in_m, (float, int, np.number)):
Expand All @@ -539,30 +533,28 @@ def _range_check(Vs30_in_mps, z1_in_m, PGA_in_g):
status = []

if Vs30_in_mps < 175 or Vs30_in_mps > 950:
status.append(1)
return status
status.append('Vs30 out of range')
if z1_in_m < 8 or z1_in_m > 900:
status.append(2)
return status
status.append('z1 out of range')
if PGA_in_g < 0.01 or PGA_in_g > 1.5:
status.append(3)
status.append('PGA out of range')

if Vs30_in_mps > 400 and z1_in_m > 750:
status.append(4)
status.append('Invalid Vs30-z1 combination')
elif Vs30_in_mps > 450 and z1_in_m > 600:
status.append(4)
status.append('Invalid Vs30-z1 combination')
elif Vs30_in_mps > 550 and z1_in_m > 450:
status.append(4)
status.append('Invalid Vs30-z1 combination')
elif Vs30_in_mps > 600 and z1_in_m > 300:
status.append(4)
status.append('Invalid Vs30-z1 combination')
elif Vs30_in_mps > 650 and z1_in_m > 150:
status.append(4)
status.append('Invalid Vs30-z1 combination')
elif Vs30_in_mps > 750 and z1_in_m > 75:
status.append(4)
status.append('Invalid Vs30-z1 combination')
elif Vs30_in_mps > 800 and z1_in_m > 36:
status.append(4)
status.append('Invalid Vs30-z1 combination')
elif Vs30_in_mps > 850 and z1_in_m > 16:
status.append(4)
status.append('Invalid Vs30-z1 combination')
else:
pass

Expand Down
40 changes: 20 additions & 20 deletions PySeismoSoil/tests/test_class_site_factors.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,35 @@ class Test_Class_Site_Factors(unittest.TestCase):
Unit test for Site_Factors class
'''
def test_range_check(self):
self.assertEqual(SF._range_check(174, 300, 0.6), [1])
self.assertEqual(SF._range_check(951, 300, 0.6), [1])
self.assertEqual(SF._range_check(300, 7, 0.6), [2])
self.assertEqual(SF._range_check(300, 901, 0.6), [2])
self.assertEqual(SF._range_check(300, 600, 0.0009), [3])
self.assertEqual(SF._range_check(300, 600, 1.501), [3])
self.assertEqual(SF._range_check(174, 10, 0.6), ['Vs30 out of range'])
self.assertEqual(SF._range_check(951, 10, 0.6), ['Vs30 out of range'])
self.assertEqual(SF._range_check(300, 7, 0.6), ['z1 out of range'])
self.assertEqual(SF._range_check(300, 901, 0.6), ['z1 out of range'])
self.assertEqual(SF._range_check(300, 600, 0.0009), ['PGA out of range'])
self.assertEqual(SF._range_check(300, 600, 1.501), ['PGA out of range'])
self.assertEqual(SF._range_check(300, 900, 0.5), [])
self.assertEqual(SF._range_check(400, 900, 0.5), [])
self.assertEqual(SF._range_check(450, 750, 0.5), [])
self.assertEqual(SF._range_check(450, 751, 0.5), [4])
self.assertEqual(SF._range_check(451, 750, 0.5), [4])
self.assertEqual(SF._range_check(450, 751, 0.5), ['Invalid Vs30-z1 combination'])
self.assertEqual(SF._range_check(451, 750, 0.5), ['Invalid Vs30-z1 combination'])
self.assertEqual(SF._range_check(550, 600, 0.5), [])
self.assertEqual(SF._range_check(551, 600, 0.5), [4])
self.assertEqual(SF._range_check(550, 601, 0.5), [4])
self.assertEqual(SF._range_check(551, 600, 0.5), ['Invalid Vs30-z1 combination'])
self.assertEqual(SF._range_check(550, 601, 0.5), ['Invalid Vs30-z1 combination'])
self.assertEqual(SF._range_check(600, 450, 0.5), [])
self.assertEqual(SF._range_check(601, 450, 0.5), [4])
self.assertEqual(SF._range_check(600, 451, 0.5), [4])
self.assertEqual(SF._range_check(601, 450, 0.5), ['Invalid Vs30-z1 combination'])
self.assertEqual(SF._range_check(600, 451, 0.5), ['Invalid Vs30-z1 combination'])
self.assertEqual(SF._range_check(650, 300, 0.5), [])
self.assertEqual(SF._range_check(651, 300, 0.5), [4])
self.assertEqual(SF._range_check(650, 301, 0.5), [4])
self.assertEqual(SF._range_check(651, 300, 0.5), ['Invalid Vs30-z1 combination'])
self.assertEqual(SF._range_check(650, 301, 0.5), ['Invalid Vs30-z1 combination'])
self.assertEqual(SF._range_check(750, 150, 0.5), [])
self.assertEqual(SF._range_check(751, 150, 0.5), [4])
self.assertEqual(SF._range_check(750, 151, 0.5), [4])
self.assertEqual(SF._range_check(751, 150, 0.5), ['Invalid Vs30-z1 combination'])
self.assertEqual(SF._range_check(750, 151, 0.5), ['Invalid Vs30-z1 combination'])
self.assertEqual(SF._range_check(800, 75, 0.5), [])
self.assertEqual(SF._range_check(801, 75, 0.5), [4])
self.assertEqual(SF._range_check(800, 76, 0.5), [4])
self.assertEqual(SF._range_check(801, 75, 0.5), ['Invalid Vs30-z1 combination'])
self.assertEqual(SF._range_check(800, 76, 0.5), ['Invalid Vs30-z1 combination'])
self.assertEqual(SF._range_check(850, 36, 0.5), [])
self.assertEqual(SF._range_check(851, 36, 0.5), [4])
self.assertEqual(SF._range_check(850, 37, 0.5), [4])
self.assertEqual(SF._range_check(851, 36, 0.5), ['Invalid Vs30-z1 combination'])
self.assertEqual(SF._range_check(850, 37, 0.5), ['Invalid Vs30-z1 combination'])

def test_search_sorted(self):
z1000_array = [8, 16, 24, 36, 75, 150, 300, 450, 600, 750, 900]
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ The authors of this library are the current and past members of the [Geoquake Re
Install most recent stable version:

```bash
pip install git+https://github.com/jsh9/[email protected].6
pip install git+https://github.com/jsh9/[email protected].7
```

Or, install with latest changes (may contain features ahead of the stable version):
Expand Down
2 changes: 1 addition & 1 deletion doc/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
author = 'Jian Shi'

# The full version, including alpha/beta/rc tags
release = '0.2.6'
release = 'v0.2.7'


# -- General configuration ---------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion doc/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Install most recent stable version:

.. code-block:: bash
pip install git+https://github.com/jsh9/[email protected].5
pip install git+https://github.com/jsh9/[email protected].7
Or, install with latest changes (may contain features ahead of the stable version):

Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

setup(
name='PySeismoSoil',
version='0.2.6',
version='v0.2.7',
description='PySeismoSoil',
author='Jian Shi',
license='GPL v3.0',
license='BSD 3',
url='https://github.com/jsh9/PySeismoSoil',
packages=['PySeismoSoil'],
classifiers=['Intended Audience :: Science/Research',
Expand Down
6 changes: 6 additions & 0 deletions update_version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
echo "Updating version number in files: from $1 to $2"
sed -i "s/$1/$2/g" README.md
sed -i "s/$1/$2/g" setup.py
sed -i "s/$1/$2/g" ./doc/source/index.rst
sed -i "s/$1/$2/g" ./doc/source/conf.py
sed -i "s/$1/$2/g" ./PySeismoSoil/__init__.py

0 comments on commit 6726bf2

Please sign in to comment.