pypharm 1.3.0__py3-none-any.whl → 1.3.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- PyPharm/models.py +101 -9
- {pypharm-1.3.0.dist-info → pypharm-1.3.2.dist-info}/METADATA +1 -1
- pypharm-1.3.2.dist-info/RECORD +8 -0
- pypharm-1.3.0.dist-info/RECORD +0 -8
- {pypharm-1.3.0.dist-info → pypharm-1.3.2.dist-info}/WHEEL +0 -0
- {pypharm-1.3.0.dist-info → pypharm-1.3.2.dist-info}/top_level.txt +0 -0
PyPharm/models.py
CHANGED
|
@@ -322,12 +322,14 @@ class MagicCompartmentModel(BaseCompartmentModel):
|
|
|
322
322
|
class ReleaseCompartmentModel(BaseCompartmentModel):
|
|
323
323
|
|
|
324
324
|
need_v_release_optimization = False
|
|
325
|
+
release_parameters_target = None
|
|
326
|
+
accumulation_parameters_target = None
|
|
325
327
|
|
|
326
328
|
class ReleaseRK45(RK45):
|
|
327
329
|
|
|
328
330
|
def __init__(self, fun, t0, y0, t_bound, release_function, compartment_number, c0, max_step=np.inf,
|
|
329
|
-
rtol=1e-3, atol=1e-6, vectorized=False,
|
|
330
|
-
first_step=None,
|
|
331
|
+
with_accumulate=False, accumulation_function=None, accumulation_type=1, rtol=1e-3, atol=1e-6, vectorized=False,
|
|
332
|
+
first_step=None, **extraneous):
|
|
331
333
|
super().__init__(fun, t0, y0, t_bound, max_step=max_step,
|
|
332
334
|
rtol=rtol, atol=atol, vectorized=vectorized,
|
|
333
335
|
first_step=first_step, **extraneous)
|
|
@@ -335,15 +337,37 @@ class ReleaseCompartmentModel(BaseCompartmentModel):
|
|
|
335
337
|
self.compartment_number = compartment_number
|
|
336
338
|
self.c0 = c0
|
|
337
339
|
self.old_release_correction = 0
|
|
340
|
+
self.old_accumulation_correction = c0
|
|
341
|
+
self.with_accumulate = with_accumulate
|
|
342
|
+
self.accumulation_function = accumulation_function
|
|
343
|
+
self.accumulation_type = accumulation_type
|
|
338
344
|
|
|
339
345
|
def _step_impl(self):
|
|
340
346
|
result = super()._step_impl()
|
|
341
347
|
release_correction = self.release_function(self.t, self.c0)
|
|
342
|
-
|
|
348
|
+
minus_accumulation = 0
|
|
349
|
+
if self.with_accumulate:
|
|
350
|
+
if self.accumulation_type == 1:
|
|
351
|
+
accumulation_correction = self.accumulation_function(self.t, self.c0 )
|
|
352
|
+
minus_accumulation = self.old_accumulation_correction - accumulation_correction
|
|
353
|
+
elif self.accumulation_type == 2:
|
|
354
|
+
coef_accumulation = self.accumulation_function(self.t, self.c0) / self.c0
|
|
355
|
+
plus_release = release_correction - self.old_release_correction
|
|
356
|
+
all_corrections = plus_release
|
|
357
|
+
if self.accumulation_type == 1:
|
|
358
|
+
if plus_release - minus_accumulation > 0:
|
|
359
|
+
all_corrections = plus_release - minus_accumulation
|
|
360
|
+
elif self.accumulation_type == 2:
|
|
361
|
+
all_corrections *= coef_accumulation
|
|
362
|
+
self.y[self.compartment_number] += all_corrections
|
|
343
363
|
self.old_release_correction = release_correction
|
|
364
|
+
if self.with_accumulate and self.accumulation_type == 1:
|
|
365
|
+
self.old_accumulation_correction = accumulation_correction
|
|
344
366
|
return result
|
|
345
367
|
|
|
346
|
-
def __init__(self, v_release, release_parameters, release_compartment,
|
|
368
|
+
def __init__(self, v_release, release_parameters, release_compartment,
|
|
369
|
+
release_function=None, with_accumulate=False, accumulation_function=None,
|
|
370
|
+
accumulation_parameters=[None], accumulation_type=1, *args, **kwargs):
|
|
347
371
|
"""
|
|
348
372
|
Камерная модель с высвобождением для описания фармакокинетики системы
|
|
349
373
|
|
|
@@ -370,6 +394,15 @@ class ReleaseCompartmentModel(BaseCompartmentModel):
|
|
|
370
394
|
self.need_v_release_optimization = True
|
|
371
395
|
self.release_compartment = release_compartment
|
|
372
396
|
self.release_function = release_function
|
|
397
|
+
self.with_accumulate = with_accumulate
|
|
398
|
+
self.accumulation_type = accumulation_type
|
|
399
|
+
|
|
400
|
+
if self.with_accumulate:
|
|
401
|
+
self.accumulation_function = accumulation_function
|
|
402
|
+
self.accumulation_parameters = np.array(accumulation_parameters)
|
|
403
|
+
if np.any(self.accumulation_parameters == None):
|
|
404
|
+
self.accumulation_parameters_target = np.where(self.accumulation_parameters == None)
|
|
405
|
+
self.accumulation_parameters_target_count = np.sum(self.accumulation_parameters == None)
|
|
373
406
|
|
|
374
407
|
if getattr(self, "memory", None):
|
|
375
408
|
self.memory.shm.close()
|
|
@@ -387,6 +420,10 @@ class ReleaseCompartmentModel(BaseCompartmentModel):
|
|
|
387
420
|
self.release_parameters[self.release_parameters_target] = x[s:s + self.release_parameters_target_count]
|
|
388
421
|
if self.need_v_release_optimization:
|
|
389
422
|
self.v_release = x[s + self.release_parameters_target_count]
|
|
423
|
+
if self.with_accumulate:
|
|
424
|
+
if self.accumulation_parameters_target:
|
|
425
|
+
s += self.release_parameters_target_count + int(self.need_v_release_optimization)
|
|
426
|
+
self.accumulation_parameters[self.accumulation_parameters_target] = x[s:s + self.accumulation_parameters_target_count]
|
|
390
427
|
|
|
391
428
|
def _default_release_function(self, t, c0):
|
|
392
429
|
"""
|
|
@@ -401,6 +438,19 @@ class ReleaseCompartmentModel(BaseCompartmentModel):
|
|
|
401
438
|
else:
|
|
402
439
|
return self._default_release_function
|
|
403
440
|
|
|
441
|
+
def _default_accumulation_function(self, t, c0):
|
|
442
|
+
"""
|
|
443
|
+
Функция для поправки на накопление
|
|
444
|
+
"""
|
|
445
|
+
k, = self.accumulation_parameters
|
|
446
|
+
return c0 * np.exp(-k * t)
|
|
447
|
+
|
|
448
|
+
def get_accumulation_function(self):
|
|
449
|
+
if self.accumulation_function is not None:
|
|
450
|
+
return lambda t, c0: self.accumulation_function(t, c0, *self.accumulation_parameters)
|
|
451
|
+
else:
|
|
452
|
+
return self._default_accumulation_function
|
|
453
|
+
|
|
404
454
|
def __call__(self, t_max, c0=None, d=None, max_step=0.01, t_eval=None, **kwargs):
|
|
405
455
|
"""
|
|
406
456
|
Расчет кривых концентраций по фармакокинетической модели
|
|
@@ -425,9 +475,9 @@ class ReleaseCompartmentModel(BaseCompartmentModel):
|
|
|
425
475
|
ts = [0, t_max]
|
|
426
476
|
y0 = np.zeros(self.outputs.shape)
|
|
427
477
|
self.last_result = solve_ivp(
|
|
428
|
-
fun=self.
|
|
478
|
+
fun=self._compartment_model if
|
|
429
479
|
not self.numba_option
|
|
430
|
-
else lambda t, c: self.
|
|
480
|
+
else lambda t, c: self._numba_compartment_model(t, c,
|
|
431
481
|
self.configuration_matrix.astype(
|
|
432
482
|
np.float64),
|
|
433
483
|
self.outputs.astype(
|
|
@@ -441,8 +491,20 @@ class ReleaseCompartmentModel(BaseCompartmentModel):
|
|
|
441
491
|
method=self.ReleaseRK45,
|
|
442
492
|
release_function=self.get_release_function(),
|
|
443
493
|
compartment_number=self.release_compartment,
|
|
494
|
+
with_accumulate=self.with_accumulate,
|
|
495
|
+
accumulation_function=self.get_accumulation_function() if self.with_accumulate else None,
|
|
496
|
+
accumulation_type=self.accumulation_type,
|
|
444
497
|
c0=c0
|
|
445
498
|
)
|
|
499
|
+
self.last_result.model_realized = c0 - self.get_release_function()(self.last_result.t, c0)
|
|
500
|
+
if self.with_accumulate:
|
|
501
|
+
model_accumulation = self.get_accumulation_function()(self.last_result.t, c0)
|
|
502
|
+
if self.accumulation_type == 1:
|
|
503
|
+
self.last_result.model_realized = model_accumulation - self.get_release_function()(self.last_result.t, c0)
|
|
504
|
+
elif self.accumulation_type == 2:
|
|
505
|
+
accumulation_coeffs = model_accumulation / c0
|
|
506
|
+
self.last_result.model_realized= accumulation_coeffs * self.get_release_function()(self.last_result.t, c0)
|
|
507
|
+
self.last_result.model_realized = model_accumulation - self.last_result.model_realized
|
|
446
508
|
return self.last_result
|
|
447
509
|
|
|
448
510
|
def _target_function(self, x, max_step=0.01, metric='R2'):
|
|
@@ -468,13 +530,22 @@ class ReleaseCompartmentModel(BaseCompartmentModel):
|
|
|
468
530
|
)
|
|
469
531
|
target_results = self.last_result.y[tuple(self.know_compartments), :]
|
|
470
532
|
if metric == 'R2':
|
|
471
|
-
|
|
533
|
+
plus = 0
|
|
534
|
+
if self.teoretic_realized is not None:
|
|
535
|
+
model_realized = self.last_result.model_realized
|
|
536
|
+
plus = np.sum(((model_realized - self.teoretic_realized) ** 2) / ((self.teoretic_realized - self.teoretic_realized_avg) ** 2))
|
|
537
|
+
return plus + np.sum(np.sum(self.w * ((target_results - self.teoretic_y) ** 2), axis=1) / np.sum((self.teoretic_avg - self.teoretic_y) ** 2, axis=1))
|
|
472
538
|
elif metric == 'norm':
|
|
473
|
-
|
|
539
|
+
plus = 0
|
|
540
|
+
if self.teoretic_realized is not None:
|
|
541
|
+
model_realized = self.last_result.model_realized
|
|
542
|
+
plus = np.linalg.norm(self.teoretic_realized - model_realized)
|
|
543
|
+
return plus + np.linalg.norm(target_results - self.teoretic_y)
|
|
474
544
|
else:
|
|
475
545
|
return np.sum(np.sum(self.w * ((target_results - self.teoretic_y) ** 2), axis=1) / np.sum((self.teoretic_avg - self.teoretic_y) ** 2, axis=1))
|
|
476
546
|
|
|
477
|
-
def load_optimization_data(self, teoretic_x, teoretic_y, know_compartments,
|
|
547
|
+
def load_optimization_data(self, teoretic_x, teoretic_y, know_compartments,
|
|
548
|
+
w = None, c0=None, d=None, compartment_number=None, teoretic_realized=None):
|
|
478
549
|
"""
|
|
479
550
|
Функция загрузки в модель эксперементальных данных
|
|
480
551
|
|
|
@@ -490,6 +561,9 @@ class ReleaseCompartmentModel(BaseCompartmentModel):
|
|
|
490
561
|
"""
|
|
491
562
|
self.teoretic_x = np.array(teoretic_x)
|
|
492
563
|
self.teoretic_y = np.array(teoretic_y)
|
|
564
|
+
self.teoretic_realized = np.array(teoretic_realized) if teoretic_realized is not None else teoretic_realized
|
|
565
|
+
if teoretic_realized is not None:
|
|
566
|
+
self.teoretic_realized_avg = np.average(self.teoretic_realized)
|
|
493
567
|
self.know_compartments = know_compartments
|
|
494
568
|
self.teoretic_avg = np.average(self.teoretic_y, axis=1)
|
|
495
569
|
self.teoretic_avg = np.repeat(self.teoretic_avg, self.teoretic_x.size)
|
|
@@ -511,4 +585,22 @@ class ReleaseCompartmentModel(BaseCompartmentModel):
|
|
|
511
585
|
if self.need_v_release_optimization:
|
|
512
586
|
self.v_release = x[s:s + self.release_parameters_target_count + 1]
|
|
513
587
|
self.need_v_release_optimization = False
|
|
588
|
+
if self.with_accumulate:
|
|
589
|
+
if self.accumulation_parameters_target:
|
|
590
|
+
s += self.release_parameters_target_count + int(self.need_v_release_optimization)
|
|
591
|
+
self.accumulation_parameters[self.accumulation_parameters_target] = x[s:s + self.accumulation_parameters_target_count]
|
|
514
592
|
return x
|
|
593
|
+
|
|
594
|
+
def plot_model(self, compartment_numbers=None, compartment_names={},
|
|
595
|
+
left=None, right=None, y_lims={}, plot_accumulation=False, **kwargs):
|
|
596
|
+
super().plot_model(compartment_numbers, compartment_names, left, right, y_lims, **kwargs)
|
|
597
|
+
if plot_accumulation:
|
|
598
|
+
if hasattr(self, "teoretic_x") and hasattr(self, "teoretic_realized"):
|
|
599
|
+
plt.plot(self.teoretic_x, self.teoretic_realized, "*r")
|
|
600
|
+
plt.plot(self.last_result.t, self.last_result.model_realized)
|
|
601
|
+
plt.title(compartment_names.get('realized', 'realized'))
|
|
602
|
+
plt.xlim(left=left, right=right)
|
|
603
|
+
if y_lims.get('realized'):
|
|
604
|
+
plt.ylim(y_lims.get('realized'))
|
|
605
|
+
plt.grid()
|
|
606
|
+
plt.show()
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
PyPharm/__init__.py,sha256=hxhMRlWpLMARQV-ZNYkmvhQ9gCYI18an75vlySWjA6s,90
|
|
2
|
+
PyPharm/country_optimization.py,sha256=WsjfAWAWbxRnUUfBiMi1VatCSVbru7F1_G6kQBRVyA0,20452
|
|
3
|
+
PyPharm/country_optimization_v2.py,sha256=3d2mt15DXdr1V3soIJS51xuCv6uzH8pirah1RnI5--8,13156
|
|
4
|
+
PyPharm/models.py,sha256=ecT0cFCbcmDL1jYuQE2IMZ7aPFZXRf63FAuJzJnugxs,31380
|
|
5
|
+
pypharm-1.3.2.dist-info/METADATA,sha256=kVzRdLrOj8_pXPtFgXYhBReVZKju5YTscVzfmDdFJPo,14464
|
|
6
|
+
pypharm-1.3.2.dist-info/WHEEL,sha256=OqRkF0eY5GHssMorFjlbTIq072vpHpF60fIQA6lS9xA,92
|
|
7
|
+
pypharm-1.3.2.dist-info/top_level.txt,sha256=yybfSkKw8q1G3aEcnlfVL7_L9ufGFSAYZnpc7q6oYJk,8
|
|
8
|
+
pypharm-1.3.2.dist-info/RECORD,,
|
pypharm-1.3.0.dist-info/RECORD
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
PyPharm/__init__.py,sha256=hxhMRlWpLMARQV-ZNYkmvhQ9gCYI18an75vlySWjA6s,90
|
|
2
|
-
PyPharm/country_optimization.py,sha256=WsjfAWAWbxRnUUfBiMi1VatCSVbru7F1_G6kQBRVyA0,20452
|
|
3
|
-
PyPharm/country_optimization_v2.py,sha256=3d2mt15DXdr1V3soIJS51xuCv6uzH8pirah1RnI5--8,13156
|
|
4
|
-
PyPharm/models.py,sha256=Lhd_2EUtGQUrratvlqZ115zLeF9fFLDPI1gi-mzjv_o,25663
|
|
5
|
-
pypharm-1.3.0.dist-info/METADATA,sha256=cUh197fi8fP5x7Do-6Is-RpC2X-z6j8ZLo-ZkJOSTrU,14464
|
|
6
|
-
pypharm-1.3.0.dist-info/WHEEL,sha256=OqRkF0eY5GHssMorFjlbTIq072vpHpF60fIQA6lS9xA,92
|
|
7
|
-
pypharm-1.3.0.dist-info/top_level.txt,sha256=yybfSkKw8q1G3aEcnlfVL7_L9ufGFSAYZnpc7q6oYJk,8
|
|
8
|
-
pypharm-1.3.0.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|