Skip to content

Commit

Permalink
Use isinstance to check for types
Browse files Browse the repository at this point in the history
Signed-off-by: Benjamin Maier <[email protected]>
  • Loading branch information
maierbn committed Oct 10, 2023
1 parent fe87fa5 commit c501da0
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 28 deletions.
19 changes: 10 additions & 9 deletions src/pylife/materiallaws/notch_approximation_law.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def _neuber_strain(self, stress, load):

# bad conditioned problem for stress approximately 0 (divide by 0), use factor 1 instead
# convert data from int to float
if type(load) is not float:
if not isinstance(load, float):
load = load.astype(float)
# factor = load / stress, avoid division by 0
factor = np.divide(load, stress, out=np.ones_like(load), where=stress!=0)
Expand Down Expand Up @@ -195,7 +195,7 @@ def _neuber_strain_secondary(self, delta_stress, delta_load):

# bad conditioned problem for delta_stress approximately 0 (divide by 0), use factor 1 instead
# convert data from int to float
if type(delta_load) is not float:
if not isinstance(delta_load, float):
delta_load = delta_load.astype(float)
# factor = load / stress, avoid division by 0
factor = np.divide(delta_load, delta_stress, out=np.ones_like(delta_load), where=delta_stress!=0)
Expand Down Expand Up @@ -277,6 +277,7 @@ def stress(self, load, *, rtol=1e-4, tol=1e-4):
elastic-plastic stress (load), from a FE computation.
This is done by solving for the root of f(sigma) in eq. 2.5-45 of FKM nonlinear.
Parameters
----------
load : array-like float
Expand Down Expand Up @@ -463,7 +464,7 @@ def _create_bins(self):


# for multiple assessment points at once use a DataFrame with MultiIndex
if type(self._maximum_absolute_load) == pd.core.frame.DataFrame:
if isinstance(self._maximum_absolute_load, pd.DataFrame):
assert self._maximum_absolute_load.index.name == "node_id"

self._create_bins_multiple_assessment_points()
Expand Down Expand Up @@ -560,12 +561,12 @@ def _create_bins_multiple_assessment_points(self):

@property
def ramberg_osgood_relation(self):
'''Get the ramberg osgood relation object
'''The ramberg osgood relation object
'''
return self._notch_approximation_law.ramberg_osgood_relation

def stress(self, load, rtol=1e-5, tol=1e-6):
'''Get the stress of the primary path in the stress-strain diagram at a given load
'''The stress of the primary path in the stress-strain diagram at a given load
by using the value of the look-up table.
.. note::
Expand All @@ -592,7 +593,7 @@ def stress(self, load, rtol=1e-5, tol=1e-6):
sign = np.sign(load)

# if the assessment is performed for multiple points at once, i.e. load is a DataFrame with values for every node
if type(load) == pd.core.frame.DataFrame and isinstance(self._lut_primary_branch.index, pd.MultiIndex):
if isinstance(load, pd.DataFrame) and isinstance(self._lut_primary_branch.index, pd.MultiIndex):

# the lut is a DataFrame with MultiIndex with levels class_index and node_id

Expand Down Expand Up @@ -665,7 +666,7 @@ def strain(self, stress, load):
sign = np.sign(load)

# if the assessment is performed for multiple points at once, i.e. load is a DataFrame with values for every node
if type(load) == pd.core.frame.DataFrame and isinstance(self._lut_primary_branch.index, pd.MultiIndex):
if isinstance(load, pd.DataFrame) and isinstance(self._lut_primary_branch.index, pd.MultiIndex):

# the lut is a DataFrame with MultiIndex with levels class_index and node_id

Expand Down Expand Up @@ -745,7 +746,7 @@ def stress_secondary_branch(self, delta_load, rtol=1e-5, tol=1e-6):
sign = np.sign(delta_load)

# if the assessment is performed for multiple points at once, i.e. load is a DataFrame with values for every node
if type(delta_load) == pd.core.frame.DataFrame and isinstance(self._lut_primary_branch.index, pd.MultiIndex):
if isinstance(delta_load, pd.DataFrame) and isinstance(self._lut_primary_branch.index, pd.MultiIndex):

# the lut is a DataFrame with MultiIndex with levels class_index and node_id

Expand Down Expand Up @@ -821,7 +822,7 @@ def strain_secondary_branch(self, delta_stress, delta_load):
sign = np.sign(delta_load)

# if the assessment is performed for multiple points at once, i.e. load is a DataFrame with values for every node
if type(delta_load) == pd.core.frame.DataFrame and isinstance(self._lut_primary_branch.index, pd.MultiIndex):
if isinstance(delta_load, pd.DataFrame) and isinstance(self._lut_primary_branch.index, pd.MultiIndex):

# the lut is a DataFrame with MultiIndex with levels class_index and node_id

Expand Down
16 changes: 8 additions & 8 deletions src/pylife/materiallaws/notch_approximation_law_seegerbeste.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def _neuber_strain(self, stress, load):

# bad conditioned problem for stress approximately 0 (divide by 0), use factor 1 instead
# convert data from int to float
if type(load) is not float:
if not isinstance(load, float):
load = load.astype(float)
# factor = load / stress, avoid division by 0
factor = np.divide(load, stress, out=np.ones_like(load), where=stress!=0)
Expand All @@ -100,7 +100,7 @@ def _u_term(self, stress, load):
(pi/2*(L/Sigma-1/k_p-1))
'''
if type(load) is not float:
if not isinstance(load, float):
load = load.astype(float)
factor = np.divide(load, stress, out=np.ones_like(load), where=stress!=0)
return (np.pi/2)*((factor-1)/(self._K_p-1))
Expand All @@ -120,7 +120,7 @@ def _middle_term(self, stress, load):
'''
# convert stress value to float
if type(stress) is not float:
if not isinstance(stress, float):
stress = stress.astype(float)
factor = np.divide(stress, load, out=np.ones_like(stress), where=load!=0)

Expand Down Expand Up @@ -153,7 +153,7 @@ def _neuber_strain_secondary(self, delta_stress, delta_load):

# bad conditioned problem for delta_stress approximately 0 (divide by 0), use factor 1 instead
# convert data from int to float
if type(delta_load) is not float:
if not isinstance(delta_load, float):
delta_load = delta_load.astype(float)
# factor = load / stress, avoid division by 0
factor = np.divide(delta_load, delta_stress, out=np.ones_like(delta_load), where=delta_stress!=0)
Expand All @@ -167,7 +167,7 @@ def _u_term_secondary(self, delta_stress, delta_load):
(pi/2*(delta_L/delta_Sigma-1/k_p-1))
'''
if type(delta_load) is not float:
if not isinstance(delta_load, float):
delta_load = delta_load.astype(float)
factor = np.divide(delta_load, delta_stress, out=np.ones_like(delta_load), where=delta_stress!=0)
return (np.pi/2)*((factor-1)/(self._K_p-1))
Expand All @@ -186,7 +186,7 @@ def _middle_term_secondary(self, delta_stress, delta_load):
<=> 1/(delta_L/delta_Sigma - 2) < k_p <= 1/(delta_L/delta_Sigma - 1)
'''
if type(delta_stress) is not float:
if not isinstance(delta_stress, float):
delta_stress = delta_stress.astype(float)
factor = np.divide(delta_stress, delta_load, out=np.ones_like(delta_stress), where=delta_load!=0)

Expand Down Expand Up @@ -317,7 +317,7 @@ def strain(self, stress, load):
The resulting strain
'''

if type(stress) is not float:
if not isinstance(stress, float):
stress = stress.astype(float)

return self._ramberg_osgood_relation.strain(stress)
Expand Down Expand Up @@ -430,7 +430,7 @@ def strain_secondary_branch(self, delta_stress, delta_load):
The resulting strain
'''

if type(delta_stress) is not float:
if not isinstance(delta_stress, float):
delta_stress = delta_stress.astype(float)

return self._ramberg_osgood_relation.delta_strain(delta_stress)
Expand Down
2 changes: 1 addition & 1 deletion src/pylife/strength/fkm_load_distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def maximum_absolute_load(self, max_load_independently_for_nodes=False):
"""

# if the load sequence is a pd.Series
if type(self._obj) == pd.core.series.Series:
if isinstance(self._obj, pd.Series):
L_max = max(abs(self._obj))

# if the load sequence is a pd.DataFrame
Expand Down
10 changes: 5 additions & 5 deletions src/pylife/stress/rainflow/fkm_nonlinear.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ def load(self):

@property
def load_representative(self):
if type(self._load) == pd.core.frame.DataFrame:
if isinstance(self._load, pd.DataFrame):
return self._load.iloc[0].values[0]
else:
return self._load

@property
def strain_representative(self):
if type(self._strain) == pd.core.frame.DataFrame:
if isinstance(self._strain, pd.DataFrame):
return self._strain.iloc[0].values[0]
else:
return self._strain
Expand All @@ -46,7 +46,7 @@ def strain(self):
return self._strain

def __str__(self):
if type(self._load) == pd.core.series.Series or type(self._load) == pd.core.frame.DataFrame:
if isinstance(self._load, pd.Series) or isinstance(self._load, pd.DataFrame):
if self._stress is None:
if self._load is None:
return "()"
Expand Down Expand Up @@ -624,7 +624,7 @@ def _get_scalar_current_load(self, current_load):
or the node from the first assessment point if multiple points are
considered at once."""

if type(current_load) == pd.core.frame.DataFrame:
if isinstance(current_load, pd.DataFrame):
current_load_representative = current_load.iloc[0].values[0]
else:
current_load_representative = current_load
Expand All @@ -637,7 +637,7 @@ def _initialize_epsilon_min_for_hcm_run(self, samples, load_turning_points):
if self._is_load_sequence_start:
self._is_load_sequence_start = False

if type(load_turning_points) != np.ndarray:
if not isinstance(load_turning_points, np.ndarray):
# properly initialize self._epsilon_min_LF and self._epsilon_max_LF
first_sample = samples[samples.index.get_level_values("load_step") == 0].reset_index(drop=True)

Expand Down
4 changes: 2 additions & 2 deletions src/pylife/stress/rainflow/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ def _new_turns(self, samples, flush=False, preserve_start=False):
preserve_start = False

_is_multiple_assessment_points = False
if type(samples) == pd.core.frame.DataFrame:
if isinstance(samples, pd.DataFrame):
_is_multiple_assessment_points = True

# convert to list
Expand Down Expand Up @@ -438,7 +438,7 @@ def _new_turns_multiple_assessment_points(self, samples, flush=False, preserve_s
The values of the turning points as data frames.
"""

assert type(samples[0]) == pd.core.frame.DataFrame
assert isinstance(samples[0], pd.DataFrame)
assert samples[0].index.names == ["load_step", "node_id"]

# extract the representative samples for the first node
Expand Down
6 changes: 3 additions & 3 deletions src/pylife/stress/rainflow/recorders.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ def _get_for_every_node(self, boolean_array):
def is_zero_mean_stress_and_strain(self):

# if the assessment is performed for multiple points at once
if len(self._S_min) > 0 and type(self._S_min[0]) == pd.core.series.Series:
if len(self._S_min) > 0 and isinstance(self._S_min[0], pd.Series):
return self._get_for_every_node(self._is_zero_mean_stress_and_strain)
else:
return self._is_zero_mean_stress_and_strain
Expand All @@ -258,7 +258,7 @@ def is_closed_hysteresis(self):
was recorded as a memory 3 hysteresis, which counts only half the damage in the FKM nonlinear procedure."""

# if the assessment is performed for multiple points at once
if len(self._S_min) > 0 and type(self._S_min[0]) == pd.core.series.Series:
if len(self._S_min) > 0 and isinstance(self._S_min[0], pd.Series):
return self._get_for_every_node(self._is_closed_hysteresis)
else:
return self._is_closed_hysteresis
Expand Down Expand Up @@ -288,7 +288,7 @@ def collective(self):
"""

# if the assessment is performed for multiple points at once
if len(self._S_min) > 0 and type(self._S_min[0]) == pd.core.series.Series:
if len(self._S_min) > 0 and isinstance(self._S_min[0], pd.Series):

n_hystereses = self.R.shape[0]
n_nodes = self.R.shape[1]
Expand Down

0 comments on commit c501da0

Please sign in to comment.