pypharm 1.3.0__py3-none-any.whl → 1.3.1__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 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
+ release_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, **extraneous):
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
- self.y[self.compartment_number] += release_correction - self.old_release_correction
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, release_function=None, *args, **kwargs):
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._сompartment_model if
478
+ fun=self._compartment_model if
429
479
  not self.numba_option
430
- else lambda t, c: self._numba_сompartment_model(t, c,
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,6 +491,9 @@ 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
  )
446
499
  return self.last_result
@@ -468,13 +521,38 @@ class ReleaseCompartmentModel(BaseCompartmentModel):
468
521
  )
469
522
  target_results = self.last_result.y[tuple(self.know_compartments), :]
470
523
  if metric == 'R2':
471
- 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))
524
+ plus = 0
525
+ if self.teoretic_realized is not None:
526
+ model_realized = c0 - self.get_release_function()(self.teoretic_x, c0)
527
+ if self.with_accumulate:
528
+ model_accumulation = self.get_accumulation_function()(self.teoretic_x, c0)
529
+ if self.accumulation_type == 1:
530
+ model_realized = model_accumulation - self.get_release_function()(self.teoretic_x, c0)
531
+ elif self.accumulation_type == 2:
532
+ accumulation_coeffs = model_accumulation / c0
533
+ model_realized = accumulation_coeffs * self.get_release_function()(self.teoretic_x, c0)
534
+ model_realized = model_accumulation - model_realized
535
+ plus = np.sum(((model_realized - self.teoretic_realized) ** 2) / ((self.teoretic_realized - self.teoretic_realized_avg) ** 2))
536
+ 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
537
  elif metric == 'norm':
473
- return np.linalg.norm(target_results - self.teoretic_y)
538
+ plus = 0
539
+ if self.teoretic_realized is not None:
540
+ model_realized = c0 - self.get_release_function()(self.teoretic_x, c0)
541
+ if self.with_accumulate:
542
+ model_accumulation = self.get_accumulation_function()(self.teoretic_x, c0)
543
+ if self.accumulation_type == 1:
544
+ model_realized = model_accumulation - self.get_release_function()(self.teoretic_x, c0)
545
+ elif self.accumulation_type == 2:
546
+ accumulation_coeffs = model_accumulation / c0
547
+ model_realized = accumulation_coeffs * self.get_release_function()(self.teoretic_x, c0)
548
+ model_realized = model_accumulation - model_realized
549
+ plus = np.linalg.norm(self.teoretic_realized - model_realized)
550
+ return plus + np.linalg.norm(target_results - self.teoretic_y)
474
551
  else:
475
552
  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
553
 
477
- def load_optimization_data(self, teoretic_x, teoretic_y, know_compartments, w = None, c0=None, d=None, compartment_number=None):
554
+ def load_optimization_data(self, teoretic_x, teoretic_y, know_compartments,
555
+ w = None, c0=None, d=None, compartment_number=None, teoretic_realized=None):
478
556
  """
479
557
  Функция загрузки в модель эксперементальных данных
480
558
 
@@ -490,6 +568,9 @@ class ReleaseCompartmentModel(BaseCompartmentModel):
490
568
  """
491
569
  self.teoretic_x = np.array(teoretic_x)
492
570
  self.teoretic_y = np.array(teoretic_y)
571
+ self.teoretic_realized = np.array(teoretic_realized) if teoretic_realized is not None else teoretic_realized
572
+ if teoretic_realized is not None:
573
+ self.teoretic_realized_avg = np.average(self.teoretic_realized)
493
574
  self.know_compartments = know_compartments
494
575
  self.teoretic_avg = np.average(self.teoretic_y, axis=1)
495
576
  self.teoretic_avg = np.repeat(self.teoretic_avg, self.teoretic_x.size)
@@ -511,4 +592,8 @@ class ReleaseCompartmentModel(BaseCompartmentModel):
511
592
  if self.need_v_release_optimization:
512
593
  self.v_release = x[s:s + self.release_parameters_target_count + 1]
513
594
  self.need_v_release_optimization = False
595
+ if self.with_accumulate:
596
+ if self.accumulation_parameters_target:
597
+ s += self.release_parameters_target_count + int(self.need_v_release_optimization)
598
+ self.accumulation_parameters[self.accumulation_parameters_target] = x[s:s + self.accumulation_parameters_target_count]
514
599
  return x
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pypharm
3
- Version: 1.3.0
3
+ Version: 1.3.1
4
4
  Summary: Module for solving pharmacokinetic problems
5
5
  Home-page: https://github.com/Krash13/PyPharm
6
6
  Author: Krash13
@@ -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=i02m8P-R256qbt8oMyB6XccgFoFlx5NnuYk5-nRQYVQ,31145
5
+ pypharm-1.3.1.dist-info/METADATA,sha256=SsNyNCtLgb4s92-PF_wWTOJze9_9j-3uzae3QmEFboA,14464
6
+ pypharm-1.3.1.dist-info/WHEEL,sha256=OqRkF0eY5GHssMorFjlbTIq072vpHpF60fIQA6lS9xA,92
7
+ pypharm-1.3.1.dist-info/top_level.txt,sha256=yybfSkKw8q1G3aEcnlfVL7_L9ufGFSAYZnpc7q6oYJk,8
8
+ pypharm-1.3.1.dist-info/RECORD,,
@@ -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,,