From c501da04a104cb754969c35df78b63c0586e3ecc Mon Sep 17 00:00:00 2001 From: Benjamin Maier Date: Tue, 10 Oct 2023 17:24:41 +0200 Subject: [PATCH] Use isinstance to check for types Signed-off-by: Benjamin Maier --- .../materiallaws/notch_approximation_law.py | 19 ++++++++++--------- .../notch_approximation_law_seegerbeste.py | 16 ++++++++-------- src/pylife/strength/fkm_load_distribution.py | 2 +- src/pylife/stress/rainflow/fkm_nonlinear.py | 10 +++++----- src/pylife/stress/rainflow/general.py | 4 ++-- src/pylife/stress/rainflow/recorders.py | 6 +++--- 6 files changed, 29 insertions(+), 28 deletions(-) diff --git a/src/pylife/materiallaws/notch_approximation_law.py b/src/pylife/materiallaws/notch_approximation_law.py index 6a379412..6ab35c95 100644 --- a/src/pylife/materiallaws/notch_approximation_law.py +++ b/src/pylife/materiallaws/notch_approximation_law.py @@ -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) @@ -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) @@ -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 @@ -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() @@ -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:: @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/src/pylife/materiallaws/notch_approximation_law_seegerbeste.py b/src/pylife/materiallaws/notch_approximation_law_seegerbeste.py index 7ed6734a..8d692af2 100644 --- a/src/pylife/materiallaws/notch_approximation_law_seegerbeste.py +++ b/src/pylife/materiallaws/notch_approximation_law_seegerbeste.py @@ -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) @@ -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)) @@ -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) @@ -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) @@ -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)) @@ -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) @@ -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) @@ -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) diff --git a/src/pylife/strength/fkm_load_distribution.py b/src/pylife/strength/fkm_load_distribution.py index dbf4c5d1..622f4270 100644 --- a/src/pylife/strength/fkm_load_distribution.py +++ b/src/pylife/strength/fkm_load_distribution.py @@ -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 diff --git a/src/pylife/stress/rainflow/fkm_nonlinear.py b/src/pylife/stress/rainflow/fkm_nonlinear.py index 169e2a69..34577de8 100644 --- a/src/pylife/stress/rainflow/fkm_nonlinear.py +++ b/src/pylife/stress/rainflow/fkm_nonlinear.py @@ -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 @@ -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 "()" @@ -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 @@ -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) diff --git a/src/pylife/stress/rainflow/general.py b/src/pylife/stress/rainflow/general.py index 79f1e50a..57757520 100644 --- a/src/pylife/stress/rainflow/general.py +++ b/src/pylife/stress/rainflow/general.py @@ -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 @@ -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 diff --git a/src/pylife/stress/rainflow/recorders.py b/src/pylife/stress/rainflow/recorders.py index 933539ee..7f57f8d5 100644 --- a/src/pylife/stress/rainflow/recorders.py +++ b/src/pylife/stress/rainflow/recorders.py @@ -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 @@ -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 @@ -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]