Skip to content

fit_HH_x_single_layer() and fit_H4_x_single_layer() subtract the initial damping in place, changing the Damping_Curve's data and the caller's array #84

Open

Description

@jsh9

Found while reviewing #74. Pre-existing on main (b764057); #74 does not change the behavior.

Problem

fit_HH_x_single_layer() and fit_H4_x_single_layer() offset the damping curve with damping_data_in_pct[:, 1] -= init_damping. This writes into the array that the caller passed in.

init_damping = damping_data_in_pct[0, 1] # small-strain damping
damping_data_in_pct[:, 1] -= init_damping # offset all dampings
damping_data_in_unit_1 = damping_data_in_pct / 100 # unit: percent --> 1

init_damping = damping_data_in_pct[0, 1] # small-strain damping
damping_data_in_pct[:, 1] -= init_damping # offset all dampings
damping_data_in_unit_1 = damping_data_in_pct / 100 # unit: percent --> 1

Damping_Curve.get_HH_x_param() and get_H4_x_param() pass self.raw_data. Multiple_Damping_Curves.get_all_HH_x_params() and get_all_H4_x_params() pass each curve's raw_data through fit_all_damping_curves(), which uses the arrays as is.

HH_x_param = hh.fit_HH_x_single_layer(
self.raw_data,
use_scipy=use_scipy,

H4_x_param = mkz.fit_H4_x_single_layer(
self.raw_data,
use_scipy=use_scipy,

list_of_np_array = [_.raw_data for _ in self.curves]

list_of_np_array = [_.raw_data for _ in self.curves]

When the curve is not interpolated, Curve.__init__() stores raw_data as the caller's array, and damping is a view of its second column. So after a fit, the curve's damping, its raw_data, the caller's array, and get_curve_matrix() are all shifted down by the initial damping.

else:
strain, values = data[:, 0], data[:, 1]

self.raw_data = data

self.damping = self.values

import numpy as np

from PySeismoSoil.class_curves import Damping_Curve, Multiple_Damping_Curves

data = np.genfromtxt('tests/files/curve_FKSH14.txt')[:, 2:4]  # strain [%], damping [%]

user_array = data.copy()
curve = Damping_Curve(user_array)
print(curve.damping[:3], user_array[:3, 1])
# [1.6683 1.8386 2.4095] [1.6683 1.8386 2.4095]
curve.get_HH_x_param(pop_size=1, n_gen=1)
print(curve.damping[:3], curve.raw_data[:3, 1], user_array[:3, 1])
# [0.     0.1703 0.7412] [0.     0.1703 0.7412] [0.     0.1703 0.7412]
# (expected [1.6683 1.8386 2.4095] for all three)

curve_2 = Damping_Curve(data.copy())
curve_2.get_H4_x_param(pop_size=1, n_gen=1)
print(curve_2.damping[:3])
# [0.     0.1703 0.7412]

mdc = Multiple_Damping_Curves('tests/files/curve_FKSH14.txt')[:2]
mdc.get_all_HH_x_params(pop_size=1, n_gen=1)
print(mdc[0].damping[:3], mdc.get_curve_matrix()[:3, 3])
# [0.     0.1703 0.7412] [0.     0.1703 0.7412]

mdc_2 = Multiple_Damping_Curves('tests/files/curve_FKSH14.txt')[:2]
mdc_2.get_all_H4_x_params(pop_size=1, n_gen=1)
print(mdc_2[0].damping[:3])
# [0.     0.1703 0.7412]

Fix

  1. In fit_HH_x_single_layer() and fit_H4_x_single_layer(), work on a float copy: for example damping_data_in_pct = np.array(damping_data_in_pct, dtype=float) before the offset. This keeps the caller's array, and so the curve objects, unchanged.
  2. Add tests that Damping_Curve.get_HH_x_param() / get_H4_x_param() and Multiple_Damping_Curves.get_all_HH_x_params() / get_all_H4_x_params() leave curve.damping, curve.raw_data and the input array unchanged.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions