Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions pyhdx/fileIO.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ def read_dynamx(*file_paths, intervals=('inclusive', 'inclusive'), time_unit='mi

Parameters
----------
file_paths: :obj:`iterable`
file_paths : :obj:`iterable`
File path of the .csv file or StringIO object
intervals: :obj:`tuple`
intervals : :obj:`tuple`
Format of how start and end intervals are specified.
time_unit :obj:`str`
time_unit : :obj:`str`
Not implemented

Returns
Expand Down Expand Up @@ -170,4 +170,4 @@ def _get_f_width(data, sign):
width = int(np.floor(w))
except OverflowError: # all zeros
width = 0
return width
return width
56 changes: 28 additions & 28 deletions pyhdx/fitting.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,17 @@ def __init__(self, bounds):

def make_parameter(self, name, value=None, min=None, max=None):
"""
Create a new :class:~symfit.Parameter.
Create a new :class:`~symfit.Parameter`.

Parameters
----------
name: :obj:`str`
name : :obj:`str`
Human-readable name for the parameter
value: :obj:`float`
value : :obj:`float`
Initial guess value
min: :obj:`float`
min : :obj:`float`
Lower bound value. If `None`, the value from `bounds` is used.
max: :obj:`float`
max : :obj:`float`
Lower bound value. If `None`, the value from `bounds` is used.

Returns
Expand All @@ -77,11 +77,11 @@ def make_parameter(self, name, value=None, min=None, max=None):

def make_variable(self, name):
"""
Create a new :class:~symfit.Variable.
Create a new :class:`~symfit.Variable`.

Parameters
----------
name: :obj:`str`
name : :obj:`str`
Human-readable name for the variable

Returns
Expand Down Expand Up @@ -258,9 +258,9 @@ def initial_guess(self, t, d):

Parameters
----------
t : :class:~`numpy.ndarray`
t : :class:`~numpy.ndarray`
Array with time points
d : :class:~`numpy.ndarray`
d : :class:`~numpy.ndarray`
Array with uptake values

"""
Expand Down Expand Up @@ -303,9 +303,9 @@ def initial_guess(self, t, d):

Parameters
----------
t : :class:~`numpy.ndarray`
t : :class:`~numpy.ndarray`
Array with time points
d : :class:~`numpy.ndarray`
d : :class:`~numpy.ndarray`
Array with uptake values

"""
Expand Down Expand Up @@ -409,9 +409,9 @@ def initial_guess(self, t, d):

Parameters
----------
t : :class:~`numpy.ndarray`
t : :class:`~numpy.ndarray`
Array with time points
d : :class:~`numpy.ndarray`
d : :class:`~numpy.ndarray`
Array with uptake values

"""
Expand Down Expand Up @@ -463,7 +463,7 @@ def func_long_dis(k, tt, A, k1):
Selected time point
A : :obj:`float`
Target amplitude
k1: : obj:`float`
k1 : :obj:`float`
Rate of fast time component

Returns
Expand Down Expand Up @@ -509,7 +509,7 @@ def func_long_ass(k, tt, A, k1):
Selected time point
A : :obj:`float`
Target amplitude
k1: : obj:`float`
k1 : :obj:`float`
Rate of fast time component

Returns
Expand All @@ -535,7 +535,7 @@ def fit_kinetics(t, d, model, chisq_thd):
Array of time points
d : :class:`~numpy.ndarray`
Array of uptake values
chisq_thd: :obj:`float`
chisq_thd : :obj:`float`
Threshold chi squared above which the fitting is repeated with the Differential Evolution algorithm.

Returns
Expand Down Expand Up @@ -793,7 +793,7 @@ def global_fit_torch(self, initial_result, regularizer=2, epochs=100000, patienc

optimizer_klass = getattr(torch.optim, optimizer)
optimizer_obj = optimizer_klass(model.parameters(), **kwargs)

mse_loss = [torch.tensor(np.inf)] # Mean squared loss only
reg_loss = [torch.tensor(np.inf)] # Loss including regularization loss
stop = 0
Expand Down Expand Up @@ -829,8 +829,6 @@ def closure():

for pname, param in model.named_parameters():
loss = loss + regularizer * torch.mean(torch.abs(param[:-1] - param[1:]))


reg_loss.append(loss)
diff = reg_loss[-2] - loss
if diff < stop_loss:
Expand All @@ -842,10 +840,10 @@ def closure():

loss.backward()
optimizer_obj.step()

mse_loss = np.array([val.detach().numpy() for val in mse_loss])
reg_loss = np.array([val.detach().numpy() for val in reg_loss])

result = TorchFitResult(self.series, model, temperature=temperature,
mse_loss=mse_loss, reg_loss=reg_loss)

Expand All @@ -859,7 +857,7 @@ def weighted_avg_t50(self):
Returns
-------

output: :~class:np.ndarray
output: :class:`~np.ndarray`
array with fields r_number, rate

"""
Expand Down Expand Up @@ -1252,6 +1250,7 @@ def setup_fit(self):
timepoints[i, -Nti:] = kf.series.timepoints
D[i, 0: Npi, -Nti:] = kf.series.uptake_corrected.T


# Create pytorch tensors from input data, assign final shapes for matrix batch multiplication by tf.matmul
dtype = torch.float64
temperature_T = torch.tensor(temperature, dtype=dtype).unsqueeze(-1).unsqueeze(-1) # Ns x 1 x 1
Expand Down Expand Up @@ -1289,7 +1288,7 @@ def global_fit(self, r1=2, r2=5, epochs=100000, patience=50, stop_loss=0.05,
optimizer_obj = optimizer_klass(model.parameters(), **kwargs)

criterion = torch.nn.MSELoss(reduction='sum')

mse_loss = [torch.tensor(np.inf)] # Mean squared loss only
reg_loss = [torch.tensor(np.inf)] # Loss including regularization loss
stop = 0
Expand Down Expand Up @@ -1317,7 +1316,7 @@ def global_fit(self, r1=2, r2=5, epochs=100000, patience=50, stop_loss=0.05,

loss.backward()
optimizer_obj.step()

mse_loss = np.array([val.detach().numpy() for val in mse_loss])
reg_loss = np.array([val.detach().numpy() for val in reg_loss])

Expand All @@ -1344,8 +1343,8 @@ def global_fit_aligned(self, alignment_array, r1=2, r2=5, epochs=100000, patienc
-------

"""
r_numbers = np.cumsum(alignment_array != '-', axis=1) # residue numbers in alignment array
aligned_bool = np.all(alignment_array != '-', axis=0) # Array True where residues align
r_numbers = np.cumsum(alignment_array != '-', axis=1) #residue numbers in alignment array
aligned_bool = np.all(alignment_array != '-', axis=0) # Array True where residues align
aligned_residues = np.array([row[aligned_bool] for row in r_numbers]) # Residue numbers of aligned residues

try:
Expand Down Expand Up @@ -1412,9 +1411,10 @@ def global_fit_aligned(self, alignment_array, r1=2, r2=5, epochs=100000, patienc

loss.backward()
optimizer_obj.step()



mse_loss = np.array([val.detach().numpy() for val in mse_loss])
reg_loss = np.array([val.detach().numpy() for val in reg_loss])
reg_loss = np.array([val.detach().numpy() for val in reg_loss])

result = TorchBatchFitResult(self, model, mse_loss=mse_loss, reg_loss=reg_loss)
return result
Expand Down
Loading