ararpy 0.1.11__py3-none-any.whl → 0.1.13__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.
@@ -14,28 +14,29 @@
14
14
 
15
15
  import os
16
16
  import shutil
17
- import string
18
-
19
17
  import numpy as np
20
- from .sample import Sample
21
- import ararpy as ap
22
- import matplotlib as mpl
23
- import scipy.stats as stats
18
+ np.set_printoptions(precision=18, threshold=10000, linewidth=np.inf)
24
19
  import math
20
+ import random
21
+ import scipy.stats as stats
22
+ from scipy.interpolate import griddata
25
23
  from scipy.special import comb
26
24
  from datetime import datetime as dt
27
25
  import re
28
26
  import time
29
27
  import ctypes
30
28
  import gc
31
- import random
32
-
29
+ import matplotlib as mpl
33
30
  mpl.use('TkAgg')
34
31
  import matplotlib.pyplot as plt
35
32
 
36
- np.set_printoptions(precision=18, threshold=10000, linewidth=np.inf)
33
+ from .sample import Sample
34
+ from .. import calc
35
+ # import ararpy as ap
37
36
 
38
37
 
38
+ import numpy as np
39
+
39
40
  def is_number(s):
40
41
  try:
41
42
  float(s)
@@ -167,7 +168,7 @@ class DiffArrmultiFunc(DiffSample):
167
168
  print(f"{self.telab = }")
168
169
  print(f"{self.tilab = }")
169
170
 
170
- def main(self):
171
+ def main(self, **params):
171
172
 
172
173
  print(f"\n======================================")
173
174
  print(f"Run Arrmulti Main | ")
@@ -286,7 +287,7 @@ class DiffArrmultiFunc(DiffSample):
286
287
  amax = 0.
287
288
  mct = 0
288
289
  chisq = 1.0e30
289
-
290
+ print(f"{mct = } > 30, continue!!!")
290
291
  continue
291
292
 
292
293
  nc = 0
@@ -1436,7 +1437,7 @@ class DiffArrmultiFunc(DiffSample):
1436
1437
  return xran, yran
1437
1438
 
1438
1439
  def arr(self, e, ord, dzx, fmod, f, telab, tilab, xlogd,
1439
- ns=200, r=1.987e-3, pi=3.141592654, ee=0.4342944879, acut=0, b=1, imp=1):
1440
+ ns=200, r=1.987e-3, pi=3.141592654, ee=0.4342944879, acut=0.5, b=8, imp=2):
1440
1441
 
1441
1442
  # print(f"{e = }")
1442
1443
  # print(f"{ord = }")
@@ -3321,11 +3322,11 @@ class DiffDraw(DiffSample):
3321
3322
 
3322
3323
  def get_plot_data(self):
3323
3324
 
3324
- k1 = [x, y1, y2] = ap.calc.spectra.get_data(self.age, self.sage, [i * 100 for i in self.f], cumulative=True)
3325
+ k1 = [x, y1, y2] = calc.spectra.get_data(self.age, self.sage, [i * 100 for i in self.f], cumulative=True)
3325
3326
  k2 = []
3326
3327
  k3 = []
3327
3328
  for cyc in range(self.mcyc):
3328
- x, y1, y2 = ap.calc.spectra.get_data(
3329
+ x, y1, y2 = calc.spectra.get_data(
3329
3330
  self.mage[cyc, :], np.zeros(self.mage[cyc, :].size), self.mf[cyc, :], cumulative=True)
3330
3331
  k2.append([x, y1, y2])
3331
3332
  k3.append([self.mcage[cyc, :], self.mcte[cyc, :]])
@@ -3355,7 +3356,7 @@ class DiffDraw(DiffSample):
3355
3356
  axs.plot(data[0][0], data[0][1], c='blue')
3356
3357
 
3357
3358
  for cyc in range(self.mcyc):
3358
- # x, y1, y2 = ap.calc.spectra.get_data(self.mage[cyc, :], np.zeros(self.mage[cyc, :].size), self.mf[cyc, :],
3359
+ # x, y1, y2 = calc.spectra.get_data(self.mage[cyc, :], np.zeros(self.mage[cyc, :].size), self.mf[cyc, :],
3359
3360
  # cumulative=True)
3360
3361
  axs.plot(data[1][cyc][0], data[1][cyc][2], c='red')
3361
3362
  axs.plot(data[1][cyc][0], data[1][cyc][1], c='red')
@@ -3501,25 +3502,25 @@ class InsideTemperatureCalibration:
3501
3502
  file_out.writelines(f"{seq.SP = }, {seq.MaxTime = }, conf MaxTime = {max(x_conf)} \n")
3502
3503
  file2_out.writelines(f"SP = {seq.SP}, MaxTime = {max(x_conf)} \n")
3503
3504
 
3504
- beta = ap.calc.regression.polynomial(y1_conf, x_conf, degree=degree)[5]
3505
+ beta = calc.regression.polynomial(y1_conf, x_conf, degree=degree)[5]
3505
3506
  ax.plot(x_conf, np.sum(beta * np.array([x_conf ** i for i in range(degree + 1)]).transpose(), axis=1),
3506
3507
  c='blue')
3507
3508
  file_out.writelines("BetaY1\t" + "\t".join([str(i) for i in beta]) + "\n")
3508
3509
  file2_out.writelines("\t".join([str(i) for i in beta]) + "\n")
3509
3510
 
3510
- beta = ap.calc.regression.polynomial(y2_conf, x_conf, degree=degree)[5]
3511
+ beta = calc.regression.polynomial(y2_conf, x_conf, degree=degree)[5]
3511
3512
  ax.plot(x_conf, np.sum(beta * np.array([x_conf ** i for i in range(degree + 1)]).transpose(), axis=1),
3512
3513
  c='blue')
3513
3514
  file_out.writelines("BetaY2\t" + "\t".join([str(i) for i in beta]) + "\n")
3514
- file2_out.writelines("\t".join([str(i) for i in beta]) + "\n")
3515
+ file2_out.writelines("\t".join([str(i) for fi in beta]) + "\n")
3515
3516
 
3516
- beta = ap.calc.regression.polynomial(y3_conf, x_conf, degree=degree)[5]
3517
+ beta = calc.regression.polynomial(y3_conf, x_conf, degree=degree)[5]
3517
3518
  ax.plot(x_conf, np.sum(beta * np.array([x_conf ** i for i in range(degree + 1)]).transpose(), axis=1),
3518
3519
  c='blue')
3519
3520
  file_out.writelines("BetaY3\t" + "\t".join([str(i) for i in beta]) + "\n")
3520
3521
  file2_out.writelines("\t".join([str(i) for i in beta]) + "\n")
3521
3522
 
3522
- beta = ap.calc.regression.polynomial(y4_conf, x_conf, degree=degree)[5]
3523
+ beta = calc.regression.polynomial(y4_conf, x_conf, degree=degree)[5]
3523
3524
  ax.plot(x_conf, np.sum(beta * np.array([x_conf ** i for i in range(degree + 1)]).transpose(), axis=1),
3524
3525
  c='blue')
3525
3526
  file_out.writelines("BetaY4\t" + "\t".join([str(i) for i in beta]) + "\n")
@@ -3677,13 +3678,10 @@ class InsideTemperatureCalibration:
3677
3678
 
3678
3679
 
3679
3680
  class SmpTemperatureCalibration:
3680
- def __init__(self, arr_path="", helix_log_path="", libano_log_path=None,
3681
+ def __init__(self, helix_log_path="", libano_log_path=None,
3681
3682
  name="smp_example", loc=r'D:\DjangoProjects\webarar\static\settings'):
3682
3683
 
3683
3684
  self.name = name
3684
- self.smp = ap.from_arr(file_path=arr_path)
3685
- if isinstance(self.smp, Sample):
3686
- self.name = self.smp.name()
3687
3685
 
3688
3686
  self.loc = loc
3689
3687
 
@@ -3779,7 +3777,8 @@ class SmpTemperatureCalibration:
3779
3777
  continue
3780
3778
 
3781
3779
  dt_str, helix, system, service, scripting, userinfo, message1, message2 = line.split("|")
3782
- dt_utc = dt_str[:26] + dt_str[27:]
3780
+ # dt_str likes 2024-10-23T19:58:59.6662140+02:00, it is too long for datetime parser
3781
+ dt_utc = dt_str[:26] + dt_str[27:] # dt_utc will be like 2024-10-23T19:58:59.666214+02:00
3783
3782
  dt_utc = dt.fromisoformat(str(dt_utc)).timestamp()
3784
3783
 
3785
3784
  if not (self.start_time <= dt_utc <= self.end_time):
@@ -3858,27 +3857,28 @@ class SmpTemperatureCalibration:
3858
3857
 
3859
3858
  file_out.close()
3860
3859
 
3860
+ np.savetxt(os.path.join(self.loc, f"{self.name}-temp.txt"), libano_log, delimiter=',')
3861
+ heating_out = open(os.path.join(self.loc, f"{self.name}-heated-index.txt"), "w")
3862
+ heating_out.writelines([f'{min(i)},{max(i)}\n' for i in yellow_data_index])
3863
+ heating_out.close()
3861
3864
 
3862
- fig, ax = plt.subplots()
3863
- fig.set_size_inches(w=12, h=10)
3865
+ plt_plot = False
3866
+ if plt_plot:
3864
3867
 
3865
- ax.plot(libano_log[0], libano_log[1], c='green')
3866
- ax.plot(libano_log[0], libano_log[2], c='blue')
3867
- ax.plot(libano_log[0], libano_log[4], c='red')
3868
- ax.plot(libano_log[0], libano_log[5], c='red')
3869
- for i in range(nstep):
3870
- _index = yellow_data_index[i]
3871
- ax.plot(libano_log[0, _index], libano_log[4, _index], c='yellow')
3872
- ax.plot(libano_log[0, _index], libano_log[5, _index], c='yellow')
3868
+ fig, ax = plt.subplots()
3869
+ fig.set_size_inches(w=12, h=10)
3873
3870
 
3874
- fig.tight_layout()
3875
- plt.show()
3871
+ ax.plot(libano_log[0], libano_log[1], c='green')
3872
+ ax.plot(libano_log[0], libano_log[2], c='blue')
3873
+ ax.plot(libano_log[0], libano_log[4], c='red')
3874
+ ax.plot(libano_log[0], libano_log[5], c='red')
3875
+ for i in range(nstep):
3876
+ _index = yellow_data_index[i]
3877
+ ax.plot(libano_log[0, _index], libano_log[4, _index], c='yellow')
3878
+ ax.plot(libano_log[0, _index], libano_log[5, _index], c='yellow')
3876
3879
 
3877
-
3878
- np.savetxt(os.path.join(self.loc, f"{self.name}-temp.txt"), libano_log, delimiter=',')
3879
- heating_out = open(os.path.join(self.loc, f"{self.name}-heated-index.txt"), "w")
3880
- heating_out.writelines([','.join([str(j) for j in i]) for i in yellow_data_index])
3881
- heating_out.close()
3880
+ fig.tight_layout()
3881
+ plt.show()
3882
3882
 
3883
3883
 
3884
3884
  #
@@ -4119,7 +4119,7 @@ class Ran1Generator:
4119
4119
 
4120
4120
  self.count += 1
4121
4121
 
4122
- print(f"{ran1 = }, {self.count = }")
4122
+ # print(f"{ran1 = }, {self.count = }")
4123
4123
 
4124
4124
  return ran1
4125
4125
 
@@ -4445,19 +4445,15 @@ def kahan_sum(input):
4445
4445
  return sum
4446
4446
 
4447
4447
 
4448
- def get_random_index(length: int = 7):
4449
- return ''.join(random.choices(string.digits, k=length))
4450
-
4451
-
4452
- def get_random_dir(path: str, length=7, random_index = ""):
4448
+ def get_random_dir(path: str, length=7, random_index=""):
4453
4449
  try:
4450
+ exist_ok = random_index != ""
4454
4451
  if random_index == "":
4455
- random_index = get_random_index(length=length)
4452
+ random_index = calc.basic.get_random_digits(length=length)
4456
4453
  destination_folder = os.path.join(path, random_index)
4457
- os.makedirs(destination_folder, exist_ok=False)
4454
+ os.makedirs(destination_folder, exist_ok=exist_ok)
4458
4455
  return destination_folder, random_index
4459
4456
  except FileExistsError:
4460
- random_index = ""
4461
4457
  return get_random_dir(path=path, length=length)
4462
4458
 
4463
4459
 
@@ -4475,7 +4471,7 @@ def run_agemon_dll(sample: Sample, source_dll_path: str, loc: str, data, max_age
4475
4471
  agemon.ni = len(data)
4476
4472
  agemon.nit = agemon.ni
4477
4473
  agemon.max_plateau_age = max_age
4478
- data = ap.calc.arr.transpose(data)
4474
+ data = calc.arr.transpose(data)
4479
4475
  agemon.telab = [i + 273.15 for i in data[3]]
4480
4476
  agemon.tilab = [i / 5.256E+11 for i in data[4]] # 1 Ma = 525600000000 minutes
4481
4477
  for i in range(agemon.nit):
@@ -4557,9 +4553,12 @@ def run_agemon_dll(sample: Sample, source_dll_path: str, loc: str, data, max_age
4557
4553
  return
4558
4554
 
4559
4555
 
4560
- def dr2_popov(f, ti):
4556
+ def dr2_popov(f, ti, ar, sar, ln=True):
4561
4557
  """
4562
4558
 
4559
+ Used in Popov 2020. It is related to the sphere model.
4560
+ The calculation of Dr2 is from sphere model. Errors are carefully propagated.
4561
+
4563
4562
  Parameters
4564
4563
  ----------
4565
4564
  f: cumulative 39Ar released, array
@@ -4575,97 +4574,201 @@ def dr2_popov(f, ti):
4575
4574
  # =IF(M5<85,( 2/PI()^2*( SQRT(PI()^2-(PI()^3)*M4/100/3)-SQRT(PI()^2-(PI()^3)*M5/100/3)) +M4/100/3-M5/100/3 )/D5/60, LN((1-M4/100)/(1-M5/100))/PI()^2/D5/60)
4576
4575
 
4577
4576
  f = np.array(f)
4578
- ti = np.array(ti)
4577
+ ti = np.array(ti) # in minute
4578
+ ar = np.array(ar)
4579
+ sar = np.array(sar)
4580
+ sar2 = sar ** 2
4579
4581
  n = min(len(f), len(ti))
4580
4582
  ti = ti * 60 # in seconds
4583
+ sti = [0.5 * 60 for i in range(n)]
4581
4584
  pi = math.pi
4582
- pi = 3.141592654
4585
+ # pi = 3.141592654
4583
4586
  dr2 = np.zeros(n)
4584
4587
 
4585
- if f[-1] >= 1:
4586
- f[-1] = 0.99999999
4587
-
4588
- # Popov 2020
4589
- a, b = 0, 0
4590
- for i in range(len(f)):
4588
+ f = np.where(f >= 1, 0.9999999999999999, f)
4589
+ sf = [math.sqrt((sum(ar[i+1:]) / sum(ar) ** 2) ** 2 * sum(sar[:i+1] ** 2) + (sum(ar[:i+1]) / sum(ar) ** 2) ** 2 * sum(sar[i+1:] ** 2)) for i in range(n)]
4590
+
4591
+ # Popov 2020, sphere model
4592
+ model = geometric_model(geo='sphere')
4593
+ dtr2 = [model(bp=0.85)[1](fi) for fi in f]
4594
+ dr2 = [(dtr2[i] - (dtr2[i - 1] if i > 0 else 0)) / ti[i] for i in range(len(dtr2))]
4595
+ dr2[-1] = math.log((1 - f[-2]) / (1 - f[-1])) / pi ** 2 / ti[-1]
4596
+
4597
+ # =IF(M5<85,((-SUM(H5:$H$35)/3/SUM($H$4:$H$35)^2/SQRT(1-PI()/3*SUM($H$4:H4)/SUM($H$4:$H$35))/(D5*60)+SUM(H6:$H$35)/3/SUM($H$4:$H$35)^2/SQRT(1-PI()/3*SUM($H$4:H5)/SUM($H$4:$H$35))/(D5*60)+H5/3/SUM($H$4:$H$35)^2/(D5*60))^2),0)
4598
+ sar_lt = np.zeros(n) # 偏导数
4599
+ sar_eq = np.zeros(n) # 偏导数
4600
+ sar_gt = np.zeros(n) # 偏导数
4601
+ st = np.zeros(n) # 偏导数
4602
+ sdr2 = np.zeros(n)
4603
+ for i in range(n):
4591
4604
  if i == 0:
4592
- if f[i] < 85:
4593
- a = 2 / pi ** 2 * math.sqrt(pi ** 2 - (pi ** 3) * f[i] / 3) + f[i] / 3
4594
- dr2[i] = ((-6 / (pi ** 1.5) + math.sqrt(36 / pi ** 3 - 4 * f[i] * 3 / pi ** 2)) / (
4595
- -6 / pi ** 2)) ** 2 / pi ** 2 / ti[i]
4605
+ if f[i] < 0.85:
4606
+ sar_lt[i] = 0
4607
+ sar_eq[i] = ((1 / math.sqrt(1 - pi * f[i] / 3) - 1) / (3 * ti[i])) * ((sum(ar) - ar[i] - sum(ar[:i])) / sum(ar) ** 2)
4608
+ sar_gt[i] = ((1 / math.sqrt(1 - pi * f[i] / 3) - 1) / (3 * ti[i])) * ((0 - ar[i] - sum(ar[:i])) / sum(ar) ** 2)
4596
4609
  else:
4597
- a = 1 - f[i]
4598
- dr2[i] = (math.log((f[i] - 1) / (-6 / pi ** 2)) / pi ** 2) / ti[i]
4610
+ sar_lt[i] = 0
4611
+ sar_eq[i] = (1 / (pi ** 2 * ti[i] * (f[i] - 1))) * ((sum(ar) - ar[i] - sum(ar[:i])) / sum(ar) ** 2)
4612
+ sar_gt[i] = (1 / (pi ** 2 * ti[i] * (f[i] - 1))) * ((0 - ar[i] - sum(ar[:i])) / sum(ar) ** 2)
4599
4613
  else:
4600
- if f[i] < 85:
4601
- a = 2 / pi ** 2 * math.sqrt(pi ** 2 - (pi ** 3) * f[i] / 3) + f[i] / 3
4602
- dr2[i] = (b - a) / ti[i]
4614
+ if f[i] < 0.85:
4615
+ sar_lt[i] = ((1 / math.sqrt(1 - pi * f[i] / 3) - 1) / (3 * ti[i])) * ((sum(ar) - ar[i] - sum(ar[:i])) / sum(ar) ** 2) - ((1 / math.sqrt(1 - pi * f[i-1] / 3) - 1) / (3 * ti[i])) * ((sum(ar) - ar[i-1] - sum(ar[:i-1])) / sum(ar) ** 2)
4616
+ sar_eq[i] = ((1 / math.sqrt(1 - pi * f[i] / 3) - 1) / (3 * ti[i])) * ((sum(ar) - ar[i] - sum(ar[:i])) / sum(ar) ** 2) - ((1 / math.sqrt(1 - pi * f[i-1] / 3) - 1) / (3 * ti[i])) * ((0 - ar[i-1] - sum(ar[:i-1])) / sum(ar) ** 2)
4617
+ sar_gt[i] = ((1 / math.sqrt(1 - pi * f[i] / 3) - 1) / (3 * ti[i])) * ((0 - ar[i] - sum(ar[:i])) / sum(ar) ** 2) - ((1 / math.sqrt(1 - pi * f[i-1] / 3) - 1) / (3 * ti[i])) * ((0 - ar[i-1] - sum(ar[:i-1])) / sum(ar) ** 2)
4603
4618
  else:
4604
- dr2[i] = math.log((1 - f[i - 1]) / (1 - f[i])) / pi ** 2 / ti[i]
4605
- b = a
4619
+ sar_lt[i] = (1 / (pi ** 2 * ti[i] * sum(ar[i:]) * sum(ar[i+1:]))) * (0 - 0)
4620
+ sar_eq[i] = (1 / (pi ** 2 * ti[i] * sum(ar[i:]) * sum(ar[i+1:]))) * (sum(ar[i+1:]) - 0)
4621
+ sar_gt[i] = (1 / (pi ** 2 * ti[i] * sum(ar[i:]) * sum(ar[i+1:]))) * (sum(ar[i+1:]) - sum(ar[i:]))
4622
+ st[i] = - dr2[i] / ti[i]
4623
+ sdr2[i] = math.sqrt(sar_lt[i] ** 2 * sum(sar2[:i]) + sar_eq[i] ** 2 * sar2[i] + sar_gt[i] ** 2 * sum(sar2[i+1:]) + st[i] ** 2 * sti[i] ** 2)
4606
4624
 
4607
- print(f"popov {dr2 = }")
4625
+ logdr2 = np.log(dr2) if ln else np.log10(dr2)
4626
+ wt = np.abs(sdr2 / dr2) if ln else np.abs(sdr2 / dr2) / np.log(10)
4608
4627
 
4609
- return dr2, np.log(dr2)
4628
+ return dtr2, logdr2, wt
4610
4629
 
4611
4630
 
4612
- def dr2_lovera(f, ti, ar, sar):
4631
+ def dr2_lovera(f, ti, ar, sar, ln=True):
4632
+ """
4633
+
4634
+ Plane sheet model
4635
+
4636
+ Parameters
4637
+ ----------
4638
+ f
4639
+ ti
4640
+ ar
4641
+ sar
4642
+ ln
4643
+
4644
+ Returns
4645
+ -------
4646
+
4647
+ """
4613
4648
  # Lovera
4614
4649
  f = np.array(f)
4615
4650
  ti = np.array(ti)
4651
+ n = min(len(f), len(ti))
4616
4652
  ar = np.array(ar)
4617
4653
  sar = np.array(sar)
4654
+ sar2 = sar ** 2
4618
4655
  ti = ti * 60 # in seconds
4656
+ sti = [90 for i in range(n)]
4619
4657
  pi = math.pi
4620
4658
  pi = 3.141592654
4621
4659
 
4622
4660
  # sf = sqrt(b ** 2 * siga ** 2 + a ** 2 * sigb ** 2) / (a + b) ** 2
4623
- sf = [math.sqrt(sar[i + 1:].sum() ** 2 * (sar[:i + 1] ** 2).sum() + ar[:i + 1].sum() ** 2 * (sar[i + 1:] ** 2).sum()) / ar.sum() ** 2 for i in range(len(ar) - 1)]
4624
- sf.append(0)
4661
+ # sf = [math.sqrt(sar[i + 1:].sum() ** 2 * (sar[:i + 1] ** 2).sum() + ar[:i + 1].sum() ** 2 * (sar[i + 1:] ** 2).sum()) / ar.sum() ** 2 for i in range(len(ar) - 1)]
4662
+ # sf.append(0)
4625
4663
 
4626
4664
  f = np.where(f >= 1, 0.9999999999999999, f)
4627
4665
 
4628
- imp = 2
4629
- # imp = 1
4630
- dtr2 = [pi * (fi / 4) ** 2 if fi <= 0.5 else math.log((1 - fi) * pi ** 2 / 8) / (- pi ** 2) for fi in f]
4631
- dr2 = [(dtr2[i] - (dtr2[i - 1] if i > 0 else 0)) / ti[i] * imp ** 2 for i in range(len(dtr2))]
4632
- xlogd = [np.log10(i) for i in dr2]
4666
+ model = geometric_model(geo='plane')
4667
+ dtr2 = [model(bp=0.5)[1](fi) for fi in f]
4668
+ dtr2_1 = [model(bp=0)[1](fi) for fi in f]
4669
+ dtr2_2 = [model(bp=2)[1](fi) for fi in f]
4670
+ dtr2_1.insert(0, 0); dtr2_2.insert(0, 0)
4671
+ # dr2 = [(dtr2[i] - (dtr2[i - 1] if i > 0 else 0)) / ti[i] for i in range(len(dtr2))]
4672
+ dr2 = [((dtr2_1[i+1] - dtr2_1[i]) / ti[i]) if f[i] > 0.5 else ((dtr2_2[i+1] - dtr2_2[i]) / ti[i]) for i in range(len(f))]
4673
+
4674
+ # imp = 2
4675
+ # # imp = 1
4676
+ # dtr2 = [pi * (fi / 4) ** 2 if fi <= 0.5 else math.log((1 - fi) * pi ** 2 / 8) / (- pi ** 2) for fi in f]
4677
+ # dr2 = [(dtr2[i] - (dtr2[i - 1] if i > 0 else 0)) / ti[i] * imp ** 2 for i in range(len(dtr2))]
4678
+ # xlogd = [np.log10(i) for i in dr2]
4679
+
4680
+ sar_lt = np.zeros(n) # 偏导数
4681
+ sar_eq = np.zeros(n) # 偏导数
4682
+ sar_gt = np.zeros(n) # 偏导数
4683
+ st = np.zeros(n) # 偏导数
4684
+ sdr2 = np.zeros(n)
4685
+ for i in range(n):
4686
+ if i == 0:
4687
+ if f[i] <= 0.5:
4688
+ sar_lt[i] = 0
4689
+ sar_eq[i] = pi / (4 * ti[i]) * ((sum(ar[:i+1]) * 2 - 0) * sum(ar) ** 2 - (sum(ar[:i+1]) ** 2 - sum(ar[:i]) ** 2) * 2 * sum(ar)) / (sum(ar) ** 4)
4690
+ sar_gt[i] = pi / (4 * ti[i]) * ((0 - 0) * sum(ar) ** 2 - (sum(ar[:i+1]) ** 2 - sum(ar[:i]) ** 2) * 2 * sum(ar)) / (sum(ar) ** 4)
4691
+ else:
4692
+ sar_lt[i] = 0
4693
+ sar_eq[i] = (4 / (pi ** 2 * ti[i] * (f[i] - 1))) * ((sum(ar) - ar[i] - sum(ar[:i])) / sum(ar) ** 2)
4694
+ sar_gt[i] = (4 / (pi ** 2 * ti[i] * (f[i] - 1))) * ((0 - ar[i] - sum(ar[:i])) / sum(ar) ** 2)
4695
+ else:
4696
+ if f[i] <= 0.5:
4697
+ sar_lt[i] = pi / (4 * ti[i]) * ((sum(ar[:i+1]) * 2 - sum(ar[:i]) * 2) * sum(ar) ** 2 - (sum(ar[:i+1]) ** 2 - sum(ar[:i]) ** 2) * 2 * sum(ar)) / (sum(ar) ** 4)
4698
+ sar_eq[i] = pi / (4 * ti[i]) * ((sum(ar[:i+1]) * 2 - 0) * sum(ar) ** 2 - (sum(ar[:i+1]) ** 2 - sum(ar[:i]) ** 2) * 2 * sum(ar)) / (sum(ar) ** 4)
4699
+ sar_gt[i] = pi / (4 * ti[i]) * ((0 - 0) * sum(ar) ** 2 - (sum(ar[:i+1]) ** 2 - sum(ar[:i]) ** 2) * 2 * sum(ar)) / (sum(ar) ** 4)
4700
+ else:
4701
+ sar_lt[i] = (4 / (pi ** 2 * ti[i] * sum(ar[i:]) * sum(ar[i+1:]))) * (0 - 0)
4702
+ sar_eq[i] = (4 / (pi ** 2 * ti[i] * sum(ar[i:]) * sum(ar[i+1:]))) * (sum(ar[i+1:]) - 0)
4703
+ sar_gt[i] = (4 / (pi ** 2 * ti[i] * sum(ar[i:]) * sum(ar[i+1:]))) * (sum(ar[i+1:]) - sum(ar[i:]))
4704
+ if i == n - 1:
4705
+ sar_lt[i] = 0
4706
+ sar_eq[i] = 0
4707
+ sar_gt[i] = 0
4708
+ st[i] = - dr2[i] / ti[i]
4709
+ sdr2[i] = math.sqrt(sar_lt[i] ** 2 * sum(sar2[:i]) + sar_eq[i] ** 2 * sar2[i] + sar_gt[i] ** 2 * sum(sar2[i+1:]) + st[i] ** 2 * sti[i] ** 2)
4710
+
4711
+ # wt = np.abs(sdr2 / dr2)
4712
+ # wt = errcal(f, ti, ar, sar)
4713
+
4714
+ xlogd = logdr2 = np.log(dr2) if ln else np.log10(dr2)
4715
+ #ee = 1 / np.log(10)
4716
+ ee = 0.4342944819
4717
+ wt = np.abs(sdr2 / dr2) if ln else np.abs(sdr2 / dr2) * ee
4718
+ #
4719
+ # print(f"{wt = }")
4720
+ # wt = errcal(f, ti, ar, sar)
4721
+ # print(f"{wt = }")
4633
4722
 
4634
- wt = errcal(f, ti, a39=ar, sig39=sar)
4723
+ """
4724
+ ### errcal(f, ti, ar, sar)和这里的计算有非常少的差异,但这样的差异足以导致迭代次数变化,但不一定导致agemon结果有大的差异
4725
+ """
4635
4726
 
4636
4727
  return dr2, xlogd, wt
4637
4728
 
4638
4729
 
4639
- def dr2_yang(f, ti):
4730
+ def dr2_yang(f, ti, ar, sar, ln=True):
4731
+ """
4732
+
4733
+ Parameters
4734
+ ----------
4735
+ f:
4736
+ ti
4737
+
4738
+ Returns
4739
+ -------
4740
+
4741
+ """
4640
4742
  f = np.array(f)
4641
4743
  ti = np.array(ti)
4744
+ ar = np.array(ar)
4745
+ sar = np.array(sar)
4746
+ sar2 = sar ** 2
4642
4747
  n = min(len(f), len(ti))
4643
4748
  ti = ti * 60 # in seconds
4749
+ sti = [5 for i in range(n)]
4644
4750
  pi = math.pi
4645
4751
  pi = 3.141592654
4646
- dr2 = np.zeros(n)
4647
-
4648
- if f[-1] >= 1:
4649
- f[-1] = 0.99999999
4650
-
4651
- def _dr2(_fi):
4652
- if _fi <= 0.85:
4653
- return (1 - math.sqrt(1 - pi * _fi / 3)) ** 2 / pi
4654
- else:
4655
- return math.log((1 - _fi) / (6 / pi ** 2)) / - (pi ** 2)
4752
+ ee = 0.4342944819
4656
4753
 
4657
- for i in range(len(f)):
4658
- if i == 0:
4659
- dr2[i] = _dr2(f[i]) / ti[i]
4660
- else:
4661
- dr2[i] = _dr2((f[i] - f[i - 1]) / (1 - f[i - 1])) / ti[i]
4754
+ f = np.array([ar[i] / sum(ar[i:]) for i in range(n)])
4755
+ sf = [math.sqrt(sar[i] ** 2 * sum(ar[i+1:]) ** 2 / sum(ar[i:]) ** 4 + sum(sar[i+1:] ** 2) * ar[i] ** 2 / sum(ar[i:]) ** 4) for i in range(n)]
4756
+ f = np.where(f >= 1, 0.9999999999999999, f)
4757
+ # # print(f"{f = }")
4758
+ # f = [(f[i] - (f[i-1] if i > 0 else 0)) / (1 - (f[i-1] if i > 0 else 0)) for i in range(n)]
4662
4759
 
4663
- print(f"yang {dr2 = }")
4760
+ model = geometric_model(geo='sphere')
4761
+ dtr2 = np.array([model(bp=0.85)[1](fi) for fi in f])
4762
+ sdtr2 = [abs((((1 / math.sqrt(1 - pi * f[i] / 3) - 1) / (3 * ti[i])) * sf[i]) if f[i] < 0.85 else (1 / (pi ** 2 * ti[i] * sum(ar[i:]) * sum(ar[i+1:]))) * sf[i]) for i in range(n)]
4763
+ dr2 = dtr2 / ti
4764
+ sdr2 = np.array([math.sqrt((1 / ti[i]) ** 2 * sdtr2[i] ** 2 + dtr2[i] ** 2 / ti[i] ** 4 * sti[i] ** 2) for i in range(n)])
4664
4765
 
4665
- return dr2, np.log(dr2)
4766
+ xlogd = logdr2 = np.log(dr2) if ln else np.log10(dr2)
4767
+ wt = sxlogd = np.abs(sdr2 / dr2) if ln else np.abs(sdr2 / dr2) / np.log(10)
4768
+ return dr2, xlogd, wt
4666
4769
 
4667
4770
 
4668
- def errcal(f, ti, a39, sig39):
4771
+ def errcal(f, ti, ar, sar):
4669
4772
 
4670
4773
  # ns = 200
4671
4774
  # r = 1.987E-3
@@ -4681,9 +4784,9 @@ def errcal(f, ti, a39, sig39):
4681
4784
  sigt0 = 90.
4682
4785
 
4683
4786
  ni = len(f)
4684
- sumat = sum(a39)
4685
- sigsm = [i / sumat for i in sig39]
4686
- siga = [sig39[i] / a39[i] for i in range(ni)]
4787
+ sumat = sum(ar)
4788
+ sigsm = [i / sumat for i in sar]
4789
+ siga = [sar[i] / ar[i] for i in range(ni)]
4687
4790
 
4688
4791
  an1 = math.pi ** 2
4689
4792
  sigat = 0.
@@ -4721,25 +4824,83 @@ def errcal(f, ti, a39, sig39):
4721
4824
 
4722
4825
  print(f"lovera {wt = }")
4723
4826
 
4827
+ return wt
4724
4828
 
4725
- ## 下面是我重新改写 计算的 与lovera的结果接近但不一致
4726
4829
 
4727
- # a39 = np.insert(a39, 0, 0)
4728
- # sig39 = np.insert(sig39, 0, 0)
4729
- #
4730
- # wt = []
4731
- # for i in range(0, ni):
4732
- #
4733
- # if f[i + 1] <= 0.5:
4734
- # sigt = (sigt0 / ti[i]) ** 2
4735
- # fp = (f[i + 1] + f[i])
4736
- # a = sig39[i + 1] / a39[i + 1]
4737
- # wt.append(ee * (sigt + 4 * (a ** 2 - 2 * a / a39.sum() + sigat) / (a39.sum() * fp) ** 2) ** 0.5)
4738
- # else:
4739
- # sigt = (sigt0 / ti[i]) ** 2
4740
- # sigf = (a39[i + 1] ** 2 / a39[i + 2:].sum() ** 2 * sig39[i + 2:].sum() ** 2 + sig39[i + 1] ** 2) / a39[i + 1:].sum() ** 2
4741
- # wt.append(ee * (sigt + (1 / math.log((1 - f[i + 1]) / (1 - f[i]))) ** 2 * sigf) ** 0.5)
4742
- #
4743
- # print(f"new function {wt = }")
4830
+ def geometric_model(geo: str = "sphere"):
4831
+ """
4832
+
4833
+ Parameters
4834
+ ----------
4835
+ geo: str, sphere | cylinder | plane | cube
4836
+ b
4837
+
4838
+ Returns
4839
+ -------
4840
+
4841
+ """
4842
+ if geo.lower()[:3] == 'sphere'[:3]:
4843
+ return sphere
4844
+ if geo.lower()[:3] == 'plane'[:3]:
4845
+ return plane
4846
+
4847
+
4848
+ def sphere(bp=0.85):
4849
+ """
4850
+ Parameters
4851
+ ----------
4852
+ bp: float, breakpoint
4853
+
4854
+ Returns
4855
+ -------
4856
+
4857
+ """
4858
+ pi = math.pi
4859
+ pi = 3.141592654
4860
+
4861
+ def func(dtr2):
4862
+ f = 1 - (6 / pi ** 2) * math.exp(-1 * pi ** 2 * dtr2)
4863
+ f2 = (6 / pi ** 1.5) * (pi ** 2 * dtr2) ** 0.5 - (3 / pi ** 2) * (pi ** 2 * dtr2)
4864
+ return f if bp <= f <= 1 else f2 if 0 <= f2 else np.nan
4865
+
4866
+ def inv_func(f):
4867
+ if 0 <= f < bp:
4868
+ dtr2 = (1 - math.sqrt(1 - pi * f / 3)) ** 2 / pi
4869
+ elif f < 1:
4870
+ dtr2 = - math.log((1 - f) * pi ** 2 / 6) / pi ** 2
4871
+ else:
4872
+ dtr2 = np.nan
4873
+ return dtr2
4874
+
4875
+ return func, inv_func
4876
+
4877
+
4878
+ def plane(bp=0.6):
4879
+ """
4880
+ Parameters
4881
+ ----------
4882
+ bp: float, breakpoint
4883
+
4884
+ Returns
4885
+ -------
4886
+
4887
+ """
4888
+ pi = math.pi
4889
+ pi = 3.141592654
4890
+
4891
+ def func(dtr2):
4892
+ f = 1 - (8 / pi ** 2) * math.exp(- pi ** 2 * dtr2 / 4)
4893
+ f2 = 2 * math.sqrt(dtr2 / pi)
4894
+ return f if bp <= f <= 1 else f2 if 0 <= f2 else np.nan
4895
+
4896
+ def inv_func(f):
4897
+ if 0 <= f < bp:
4898
+ dtr2 = pi * f ** 2 / 4
4899
+ elif f < 1:
4900
+ dtr2 = 4 * math.log((1 - f) * pi ** 2 / 8) / (- pi ** 2)
4901
+ else:
4902
+ dtr2 = np.nan
4903
+ return dtr2
4904
+
4905
+ return func, inv_func
4744
4906
 
4745
- return wt