Skip to content

Commit 015da51

Browse files
authored
Add more flake8 linters (#10)
* Add flake8-clean-block * Add indent-in-def & picky-parentheses * Add implicit-str-concat * Add flake8-return * Add flake8-broken-line * Remove unnecessary blank line * Add Python 3.11 checks * Remove Python 3.11 checks * Change incorrect unpacking * v0.5.2 -> v0.5.3
1 parent 5744b35 commit 015da51

30 files changed

+320
-104
lines changed

PySeismoSoil/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Author: Jian Shi
22

3-
__version__ = 'v0.5.2'
3+
__version__ = 'v0.5.3'

PySeismoSoil/class_Vs_profile.py

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ def __init__(
9292

9393
if damping_unit not in ['1', '%']:
9494
raise ValueError("`damping_unit` must be '1' or '%'.")
95+
9596
if density_unit not in ['kg/m^3', 'g/cm^3', 'kg/m3', 'g/cm3']:
9697
raise ValueError("`density_unit` must be 'kg/m^3' or 'g/cm^3'.")
9798

@@ -105,6 +106,7 @@ def __init__(
105106
material_number = np.append(np.arange(1, n_layer_tmp), [0])
106107
else:
107108
material_number = np.arange(1, n_layer_tmp + 1)
109+
108110
full_data = np.column_stack((thk, vs, xi, rho, material_number))
109111
elif n_col == 5:
110112
xi = data_[:, 2]
@@ -128,6 +130,7 @@ def __init__(
128130

129131
if density_unit in ['g/cm^3', 'g/cm3']:
130132
data_[:, 3] *= 1000.0 # g/cm^3 --> kg/m^3
133+
131134
if damping_unit == '%':
132135
data_[:, 2] /= 100.0 # percent --> 1
133136

@@ -352,8 +355,10 @@ def truncate(self, depth=None, Vs=1000.0):
352355
"""
353356
if depth is None or depth <= 0:
354357
raise ValueError('`depth` needs to be a positive number.')
358+
355359
if Vs is None or Vs <= 0:
356360
raise ValueError('`Vs` needs to be a positive number.')
361+
357362
profile_ = []
358363
total_depth = 0
359364
for j in range(len(self._vs)):
@@ -363,9 +368,9 @@ def truncate(self, depth=None, Vs=1000.0):
363368
last_row[0] = last_thk
364369
profile_.append(last_row)
365370
break
366-
else:
367-
profile_.append(self.vs_profile[j, :])
368-
total_depth += self._thk[j]
371+
372+
profile_.append(self.vs_profile[j, :])
373+
total_depth += self._thk[j]
369374
else: # `depth` > total depth of the current profile
370375
last_thk = profile_[-1][0] # thickness of the original last layer
371376
profile_[-1][0] = depth + last_thk - total_depth # extend to `depth`
@@ -375,6 +380,7 @@ def truncate(self, depth=None, Vs=1000.0):
375380
bedrock = [0, Vs, xi[0], rho[0], 0]
376381
else: # just numbers
377382
bedrock = [0, Vs, xi, rho, 0]
383+
378384
profile_.append(bedrock) # add half space whose Vs is `Vs`
379385
profile_ = np.array(profile_)
380386

@@ -413,6 +419,7 @@ def query_Vs_at_depth(self, depth, as_profile=False, show_fig=False):
413419
'If `as_profile` is set to True, the given '
414420
'`depth` needs to be monotonically increasing.',
415421
)
422+
416423
if has_duplicate_values:
417424
raise ValueError(
418425
'If `as_profile` is set to True, the given '
@@ -425,6 +432,7 @@ def query_Vs_at_depth(self, depth, as_profile=False, show_fig=False):
425432
vs_queried = np.append(vs_queried[0:1], vs_queried)
426433
else: # `depth` has been guarenteed to be sorted with no duplicates
427434
thk_array = sr.dep2thk(depth)
435+
428436
vs_ = np.column_stack((thk_array, vs_queried))
429437

430438
if show_fig:
@@ -433,13 +441,14 @@ def query_Vs_at_depth(self, depth, as_profile=False, show_fig=False):
433441

434442
# A halfspace is already implicitly added by sr.depth2thk()
435443
return Vs_Profile(vs_, add_halfspace=False)
436-
else:
437-
if show_fig:
438-
self._plot_queried_Vs(vs_queried, depth)
439-
if is_scalar:
440-
return float(vs_queried)
441-
else:
442-
return vs_queried
444+
445+
if show_fig:
446+
self._plot_queried_Vs(vs_queried, depth)
447+
448+
if is_scalar:
449+
return float(vs_queried)
450+
451+
return vs_queried
443452

444453
def query_Vs_given_thk(
445454
self,
@@ -482,6 +491,7 @@ def query_Vs_given_thk(
482491
"""
483492
if n_layers is None and isinstance(thk, (int, float, np.number)):
484493
n_layers = int(np.ceil(self.z_max / thk))
494+
485495
vs_queried, thk_array = sr.query_Vs_given_thk(
486496
self.vs_profile,
487497
thk,
@@ -493,13 +503,15 @@ def query_Vs_given_thk(
493503
if show_fig:
494504
depth = sr.thk2dep(thk_array, midpoint=at_midpoint)
495505
self._plot_queried_Vs(vs_queried, depth)
506+
496507
return vs_queried
497-
else:
498-
vs_ = np.column_stack((thk_array, vs_queried))
499-
if show_fig:
500-
fig, ax, _ = self.plot()
501-
sr.plot_Vs_profile(vs_, fig=fig, ax=ax, c='orange', alpha=0.75)
502-
return Vs_Profile(vs_, add_halfspace=add_halfspace)
508+
509+
vs_ = np.column_stack((thk_array, vs_queried))
510+
if show_fig:
511+
fig, ax, _ = self.plot()
512+
sr.plot_Vs_profile(vs_, fig=fig, ax=ax, c='orange', alpha=0.75)
513+
514+
return Vs_Profile(vs_, add_halfspace=add_halfspace)
503515

504516
def _plot_queried_Vs(self, vs_queried, depth, dpi=100):
505517
"""
@@ -518,8 +530,6 @@ def _plot_queried_Vs(self, vs_queried, depth, dpi=100):
518530
if np.max(y_lim) <= np.max(depth):
519531
ax.set_ylim((np.max(depth), np.min(y_lim)))
520532

521-
return None
522-
523533
def get_basin_depth(self, bedrock_Vs=1000.0):
524534
"""
525535
Query the depth of the basin as indicated in the Vs profile data.
@@ -604,6 +614,7 @@ def to_txt(
604614
"""
605615
if not isinstance(precision, list):
606616
raise TypeError('precision must be a list.')
617+
607618
if len(precision) != 5:
608619
raise ValueError('Length of precision must be 5.')
609620

PySeismoSoil/class_batch_simulation.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ class Batch_Simulation:
3535
def __init__(self, list_of_simulations):
3636
if not isinstance(list_of_simulations, list):
3737
raise TypeError('`list_of_simulations` should be a list.')
38+
3839
if len(list_of_simulations) == 0:
3940
raise ValueError('`list_of_simulations` should have at least one element.')
41+
4042
sim_0 = list_of_simulations[0]
4143
if not isinstance(
4244
sim_0,
@@ -47,10 +49,12 @@ def __init__(self, list_of_simulations):
4749
'type `Linear_Simulation`, `Equiv_Linear_Simulation`, '
4850
'or `Nonlinear_Simulation`.',
4951
)
52+
5053
if not all(isinstance(i, type(sim_0)) for i in list_of_simulations):
5154
raise TypeError(
5255
'All the elements of `list_of_simulations` should be of the same type.',
5356
)
57+
5458
n_simulations = len(list_of_simulations)
5559

5660
self.list_of_simulations = list_of_simulations
@@ -103,6 +107,7 @@ def run(self, parallel=False, n_cores=1, base_output_dir=None, options=None):
103107
# Because no outputs can be printed to stdout in the parellel pool
104108
if options.get('verbose', True): # default value is `True`
105109
print('Parallel computing in progress...', end=' ')
110+
106111
p = mp.Pool(n_cores)
107112
sim_results = p.map(
108113
self._run_single_sim,

PySeismoSoil/class_curves.py

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,12 @@ def plot(
149149
self.raw_data[:, 1],
150150
**kwargs_to_matplotlib,
151151
)
152+
152153
ax.grid(ls=':')
153154
ax.set_xlabel(xlabel)
154155
if ylabel:
155156
ax.set_ylabel(ylabel)
157+
156158
if title:
157159
ax.set_title(title)
158160

@@ -619,13 +621,16 @@ def __len__(self):
619621
def __setitem__(self, i, item):
620622
if not isinstance(item, self.element_class):
621623
raise TypeError('The new `item` must be of type %s.' % self.element_class)
624+
622625
self.curves[i] = item
623626

624627
def __getitem__(self, i):
625628
if isinstance(i, int):
626629
return self.curves[i]
630+
627631
if isinstance(i, slice): # return an object of the same class
628632
return self.__class__(self.curves[i]) # filled with the sliced data
633+
629634
raise TypeError('Indices must be integers or slices, not %s' % type(i))
630635

631636
def __delitem__(self, i):
@@ -636,6 +641,7 @@ def append(self, item):
636641
"""Append another curve item to the curves."""
637642
if not isinstance(item, self.element_class):
638643
raise TypeError('The new `item` must be of type %s.' % self.element_class)
644+
639645
self.curves.append(item)
640646
self.n_layer += 1
641647

@@ -962,6 +968,7 @@ def get_all_HH_x_params(
962968
if save_txt:
963969
if txt_filename is None:
964970
txt_filename = self._produce_output_file_name('HH', 'txt')
971+
965972
if sep is None:
966973
sep = self._sep
967974

@@ -1081,6 +1088,7 @@ def get_all_H4_x_params(
10811088
if save_txt:
10821089
if txt_filename is None:
10831090
txt_filename = self._produce_output_file_name('H4', 'txt')
1091+
10841092
if sep is None:
10851093
sep = self._sep
10861094

@@ -1365,26 +1373,32 @@ def __init__(self, *, mgc_and_mdc=None, data=None):
13651373
'Both parameters are `None`. Please provide '
13661374
'one and only one input parameter.',
13671375
)
1376+
13681377
if mgc_and_mdc is not None and data is not None:
13691378
raise ValueError(
13701379
'Both parameters are not `None`. Please provide '
13711380
'one and only one input parameter.',
13721381
)
1382+
13731383
if mgc_and_mdc is not None:
13741384
if not isinstance(mgc_and_mdc, tuple):
13751385
raise TypeError('`mgc_and_mdc` needs to be a tuple.')
1386+
13761387
if len(mgc_and_mdc) != 2:
13771388
raise ValueError('Length of `mgc_and_mdc` needs to be 2.')
1389+
13781390
if not isinstance(mgc_and_mdc[0], Multiple_GGmax_Curves):
13791391
raise TypeError(
13801392
'The 0th element of `mgc_and_mdc` needs to '
13811393
'be of type `Multiple_GGmax_Curves`.',
13821394
)
1395+
13831396
if not isinstance(mgc_and_mdc[-1], Multiple_Damping_Curves):
13841397
raise TypeError(
13851398
'The last element of `mgc_and_mdc` needs to '
13861399
'be of type `Multiple_Damping_Curves`.',
13871400
)
1401+
13881402
self.mgc = mgc_and_mdc[0]
13891403
self.mdc = mgc_and_mdc[-1]
13901404
self.data = None
@@ -1394,12 +1408,15 @@ def __init__(self, *, mgc_and_mdc=None, data=None):
13941408
'the ``Multiple_Damping_Curves`` instance '
13951409
'need to have the same number of soil layers.',
13961410
)
1411+
13971412
self.n_layer = self.mgc.n_layer
13981413
else: # `data` is not `None`
13991414
if not isinstance(data, (np.ndarray, str)):
14001415
raise TypeError('`data` must be a 2D numpy array or a file name.')
1416+
14011417
if isinstance(data, str):
14021418
data = np.genfromtxt(data)
1419+
14031420
self.mgc = None
14041421
self.mdc = None
14051422
hlp.assert_2D_numpy_array(data, name='`data`')
@@ -1409,6 +1426,7 @@ def __init__(self, *, mgc_and_mdc=None, data=None):
14091426
'to be a multiple of 4. However, your '
14101427
'`data` has %d columns.' % data.shape[1],
14111428
)
1429+
14121430
self.data = data
14131431
self.n_layer = data.shape[1] // 4
14141432

@@ -1425,14 +1443,15 @@ def get_MGC_MDC_objects(self):
14251443
"""
14261444
if self.mgc is not None:
14271445
return self.mgc, self.mdc
1428-
else: # the user provides a matrix containing curve information
1429-
GGmax_curve_list, damping_curves_list = hlp.extract_from_curve_format(
1430-
self.data,
1431-
ensure_non_negative=False,
1432-
)
1433-
mgc = Multiple_GGmax_Curves(GGmax_curve_list)
1434-
mdc = Multiple_Damping_Curves(damping_curves_list)
1435-
return mgc, mdc
1446+
1447+
# the user provides a matrix containing curve information
1448+
GGmax_curve_list, damping_curves_list = hlp.extract_from_curve_format(
1449+
self.data,
1450+
ensure_non_negative=False,
1451+
)
1452+
mgc = Multiple_GGmax_Curves(GGmax_curve_list)
1453+
mdc = Multiple_Damping_Curves(damping_curves_list)
1454+
return mgc, mdc
14361455

14371456
def get_curve_matrix(self):
14381457
"""
@@ -1450,10 +1469,10 @@ def get_curve_matrix(self):
14501469
"""
14511470
if self.data is not None:
14521471
return self.data
1453-
else:
1454-
mgc_matrix = self.mgc.get_curve_matrix()
1455-
mdc_matrix = self.mdc.get_curve_matrix()
1456-
return hlp.merge_curve_matrices(mgc_matrix, mdc_matrix)
1472+
1473+
mgc_matrix = self.mgc.get_curve_matrix()
1474+
mdc_matrix = self.mdc.get_curve_matrix()
1475+
return hlp.merge_curve_matrices(mgc_matrix, mdc_matrix)
14571476

14581477
def plot(self):
14591478
"""Plot the G/Gmax and damping curves."""

PySeismoSoil/class_damping_calibration.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class Damping_Calibration:
1818
def __init__(self, vs_profile):
1919
if not isinstance(vs_profile, Vs_Profile):
2020
raise TypeError('`vs_profile` must be of type Vs_Profile.')
21+
2122
self.vs_profile = vs_profile
2223

2324
def get_damping_curves(
@@ -77,6 +78,7 @@ def get_damping_curves(
7778
if not use_Darendeli_Dmin:
7879
xi_j -= xi_j[0]
7980
xi_j += self.vs_profile.vs_profile[j, 2]
81+
8082
dc = Damping_Curve(
8183
np.column_stack((strain_in_pct, xi_j)),
8284
strain_unit='%',
@@ -85,6 +87,7 @@ def get_damping_curves(
8587
check_values=True,
8688
)
8789
curve_list.append(dc)
90+
8891
mdc = Multiple_Damping_Curves(curve_list)
8992

9093
if show_fig:

PySeismoSoil/class_frequency_spectrum.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ def __init__(
112112
data_[:, 1],
113113
log_scale=log_scale,
114114
)
115+
115116
self.raw_df = df
116117
self.raw_data = data_
117118
self.n_pts = n_pts
@@ -183,13 +184,16 @@ def plot(
183184
ax.plot(self.freq, self.amplitude, **kwargs_plot)
184185
else:
185186
ax.plot(self.freq, self.spectrum, **kwargs_plot)
187+
186188
ax.set_xlabel('Frequency [Hz]')
187189
ax.set_ylabel('Amplitude or phase')
188190
ax.grid(ls=':')
189191
if logx:
190192
ax.set_xscale('log')
193+
191194
if logy:
192195
ax.set_yscale('log')
196+
193197
if self._file_name:
194198
ax.set_title(self._file_name)
195199

0 commit comments

Comments
 (0)