ararpy 0.1.34__tar.gz → 0.1.36__tar.gz

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.
Files changed (81) hide show
  1. {ararpy-0.1.34 → ararpy-0.1.36}/PKG-INFO +1 -1
  2. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/__init__.py +2 -2
  3. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/calc/basic.py +3 -4
  4. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/calc/raw_funcs.py +3 -3
  5. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/calc/regression.py +94 -82
  6. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/files/basic.py +0 -22
  7. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/files/calc_file.py +35 -34
  8. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/files/raw_file.py +27 -70
  9. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/smp/diffusion_funcs.py +1 -1
  10. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/smp/export.py +32 -32
  11. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/smp/raw.py +7 -11
  12. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/smp/sample.py +55 -55
  13. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy.egg-info/PKG-INFO +1 -1
  14. {ararpy-0.1.34 → ararpy-0.1.36}/setup.py +1 -1
  15. {ararpy-0.1.34 → ararpy-0.1.36}/LICENSE +0 -0
  16. {ararpy-0.1.34 → ararpy-0.1.36}/README.md +0 -0
  17. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/Example - Check arr.py +0 -0
  18. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/Example - Granite Cooling History.py +0 -0
  19. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/Example - Plot temperature calibration.py +0 -0
  20. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/Example - Show MDD results.py +0 -0
  21. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/Example - Show all Kfs age spectra.py +0 -0
  22. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/Example - Show random walk results.py +0 -0
  23. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/Example - Tc calculation.py +0 -0
  24. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/calc/__init__.py +0 -0
  25. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/calc/age.py +0 -0
  26. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/calc/arr.py +0 -0
  27. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/calc/corr.py +0 -0
  28. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/calc/err.py +0 -0
  29. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/calc/histogram.py +0 -0
  30. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/calc/isochron.py +0 -0
  31. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/calc/jvalue.py +0 -0
  32. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/calc/plot.py +0 -0
  33. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/calc/spectra.py +0 -0
  34. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/examples/022_VU124-M11a.ahd +0 -0
  35. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/examples/20WHA0103.age +0 -0
  36. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/examples/22WHA0078.xls +0 -0
  37. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/examples/22WHA0433.age +0 -0
  38. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/examples/22WHA0433.arr +0 -0
  39. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/examples/22WHA0433.full.xls +0 -0
  40. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/examples/24WHN0001-51-592.XLS +0 -0
  41. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/examples/AHD.input-filter +0 -0
  42. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/examples/ArAr.calc +0 -0
  43. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/examples/ArArCALC.age +0 -0
  44. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/examples/NGX-600 - Copy.TXT +0 -0
  45. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/examples/NGX-600.TXT +0 -0
  46. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/examples/NGX-XLS.input-filter +0 -0
  47. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/examples/Qtegra-exported-xls.input-filter +0 -0
  48. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/examples/S01-239.csv +0 -0
  49. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/examples/WH01.irra +0 -0
  50. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/examples/WHA.pdf +0 -0
  51. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/examples/raw_example.xls +0 -0
  52. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/examples/sample-default.smp +0 -0
  53. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/files/__init__.py +0 -0
  54. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/files/arr_file.py +0 -0
  55. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/files/new_file.py +0 -0
  56. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/files/xls.py +0 -0
  57. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/smp/EXPORT_TO_PDF_DATA_PROPERTIES.py +0 -0
  58. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/smp/__init__.py +0 -0
  59. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/smp/basic.py +0 -0
  60. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/smp/calculation.py +0 -0
  61. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/smp/consts.py +0 -0
  62. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/smp/corr.py +0 -0
  63. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/smp/info.py +0 -0
  64. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/smp/initial.py +0 -0
  65. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/smp/json.py +0 -0
  66. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/smp/plots.py +0 -0
  67. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/smp/style.py +0 -0
  68. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/smp/table.py +0 -0
  69. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/test.py +0 -0
  70. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/thermo/__init__.py +0 -0
  71. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/thermo/arrhenius.py +0 -0
  72. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/thermo/atomic_level_random_walk.py +0 -0
  73. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy/thermo/basic.py +0 -0
  74. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy.egg-info/SOURCES.txt +0 -0
  75. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy.egg-info/dependency_links.txt +0 -0
  76. {ararpy-0.1.34 → ararpy-0.1.36}/ararpy.egg-info/top_level.txt +0 -0
  77. {ararpy-0.1.34 → ararpy-0.1.36}/setup.cfg +0 -0
  78. {ararpy-0.1.34 → ararpy-0.1.36}/tests/test.py +0 -0
  79. {ararpy-0.1.34 → ararpy-0.1.36}/tests/test2.py +0 -0
  80. {ararpy-0.1.34 → ararpy-0.1.36}/tests/test_error_correlation.py +0 -0
  81. {ararpy-0.1.34 → ararpy-0.1.36}/tests/test_regression_methods.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ararpy
3
- Version: 0.1.34
3
+ Version: 0.1.36
4
4
  Summary: A project for Ar-Ar geochronology
5
5
  Home-page: https://github.com/wuyangchn/ararpy.git
6
6
  Author: Yang Wu
@@ -16,10 +16,10 @@ from . import calc, smp, files, thermo, test
16
16
  """ Information """
17
17
 
18
18
  name = 'ararpy'
19
- version = '0.1.34'
19
+ version = '0.1.36'
20
20
  __version__ = version
21
21
  full_version = version
22
- last_update = '2025-10-22'
22
+ last_update = '2025-12-08'
23
23
 
24
24
  """ ArArPy Functions """
25
25
 
@@ -10,8 +10,7 @@
10
10
  #
11
11
  """
12
12
  import copy
13
- import random
14
- import string
13
+ import secrets
15
14
  import numpy as np
16
15
  from scipy import stats
17
16
  from datetime import datetime, timezone, timedelta
@@ -100,8 +99,8 @@ def update_dicts(a: dict, b: dict):
100
99
  return res
101
100
 
102
101
 
103
- def get_random_digits(length: int = 7) -> str:
104
- return ''.join(random.choices(string.digits, k=length))
102
+ def get_random_string(length: int = 8) -> str:
103
+ return secrets.token_hex(max(int(length/2), 1))
105
104
 
106
105
 
107
106
  def monte_carlo(func, inputs, confidence_level, **kwargs):
@@ -30,8 +30,8 @@ def get_raw_data_regression_results(points_data, unselected: list = None):
30
30
 
31
31
  """
32
32
  def power(a0, a1):
33
- return regression.power(a0, a1)
34
- # raise ValueError("Deprecated regression")
33
+ # return regression.power(a0, a1)
34
+ raise ValueError("Deprecated regression")
35
35
  # if unselected is None:
36
36
  # unselected = []
37
37
  # linesData = []
@@ -42,9 +42,9 @@ def get_raw_data_regression_results(points_data, unselected: list = None):
42
42
  power, regression.average]
43
43
  # size = 50
44
44
  # lines_x = [(max(x + un_x) - 0) / size * i for i in range(size + 1)]
45
+ x, y = transpose(points_data, ignore=False)
45
46
  for i in range(len(reg_handler)):
46
47
  try:
47
- x, y = transpose(points_data, ignore=False)
48
48
  res = reg_handler[i](a0=y, a1=x)
49
49
  # line_data = transpose([lines_x, res[7](lines_x)])
50
50
  line_results = res[0:4]
@@ -16,7 +16,7 @@ import traceback
16
16
  import numpy as np
17
17
  import pandas as pd
18
18
  from scipy.stats import distributions
19
- from scipy.optimize import fsolve
19
+ from scipy.optimize import minimize_scalar
20
20
  import warnings
21
21
  from scipy.optimize import minimize
22
22
  warnings.simplefilter(action="ignore", category=RuntimeWarning)
@@ -495,16 +495,19 @@ def linest(a0: list, a1: list, *args):
495
495
  # calculate Y values base on the fitted formula
496
496
  estimate_y = np.matmul(x, beta)
497
497
  resid = (estimate_y - y) ** 2
498
- reg = (estimate_y - np.mean(estimate_y)) ** 2
499
- ssresid = resid.sum()
500
- ssreg = reg.sum()
501
- sstotal = ssreg + ssresid
498
+ reg = (estimate_y - np.mean(y)) ** 2
499
+ ssresid = resid.sum() # 残差平方和
500
+ ssreg = reg.sum() # 回归平方和
501
+ sstotal = ((y - np.mean(y)) ** 2).sum()
502
+ r2 = ssreg / sstotal if sstotal != 0 else np.inf
503
+
502
504
  df = m - n
503
- m_ssresid = ssresid / df
504
- se_beta = (m_ssresid * np.diagonal(inv_xtx)) ** .5
505
- beta = beta.transpose()[0]
505
+ m_ssresid = ssresid / df # 均方残差,与加权平均中的MSWD对应
506
+ cov_beta = m_ssresid * inv_xtx
507
+ se_beta = np.diagonal(cov_beta) ** .5
508
+
509
+ beta = beta.flatten()
506
510
  rse_beta = se_beta / beta
507
- r2 = ssreg / sstotal if sstotal != 0 else np.inf
508
511
 
509
512
  def get_adjusted_y(*args):
510
513
  args = [[1] * len(args[0]), *args]
@@ -538,7 +541,7 @@ def average(a0: list, a1=None):
538
541
  k1 = pow(sum([(i - k0) ** 2 for i in a0]) / df, 0.5) # standard deviation
539
542
  k2 = k1 / k0 * 100 if k0 != 0 else 0 # relative standard error
540
543
  k3 = r2 # determination coefficient
541
- k4 = 'MSWD'
544
+ k4 = m_ssresid # 'MSWD'
542
545
  k5 = [k0]
543
546
  k6 = [k1]
544
547
  k8 = m_ssresid
@@ -869,92 +872,101 @@ def power(a0: list, a1: list):
869
872
 
870
873
 
871
874
  def exponential(a0: list, a1: list):
872
- """
873
- :param a0: known_y's, y = a * b ^ x + c
874
- :param a1: known_x's
875
- :return: intercept | standard error of intercept | relative error | R2 | MSWD | [m, c, b] | [sem, sec, seb]
876
- """
877
875
 
878
- def _exp_func(x, a, b, c):
879
- return a * b ** x + c
876
+ X = np.array(a1)
877
+ Y = np.array(a0)
878
+ y = np.array([a0]).transpose()
880
879
 
881
- def _residuals(params):
882
- a, b, c = params
883
- return [_exp_func(xi, a, b, c) - yi for xi, yi in zip(a1, a0)]
880
+ def _exp_func(xi, a, b, c):
881
+ return a * np.exp(b * xi) + c
884
882
 
885
- def _sum_squared_error(params):
886
- return sum(r**2 for r in _residuals(params))
883
+ @np.vectorize
884
+ def _get_s(b):
885
+ x = np.concatenate(([np.ones(len(a1))], [np.exp(b * np.array(a1))]), axis=0).transpose()
886
+ try:
887
+ inv_xtx = np.linalg.inv(np.matmul(x.transpose(), x))
888
+ except np.linalg.LinAlgError:
889
+ raise np.linalg.LinAlgError(f"The determinant of the given matrix must not be zero ")
890
+ beta = np.matmul(inv_xtx, np.matmul(x.transpose(), y))
891
+ c, a = beta.flatten()
892
+ reg_y = _exp_func(X, a, b, c)
893
+ resid = (reg_y - Y) ** 2
894
+ ssresid = sum(resid)
895
+ return ssresid
887
896
 
888
- def _get_ac(b):
889
- f = linest(a0, [b ** xi for xi in a1])
890
- return f[5][1], b, f[0]
891
897
 
892
- def _get_init():
893
- f = linest(np.log(np.array(a0)), np.array(a1))
894
- return np.exp(f[0]), np.exp(f[5][1]), 0
898
+ ini_f = linest(np.log(Y), X)
899
+ ini_b = ini_f[5][1]
895
900
 
901
+ # 替换原来的 minimize_scalar 部分
896
902
  try:
897
- a, b, c = _get_init()
898
- # a, b, c = minimize(_sum_squared_error, [a, b, c], method="Nelder-Mead").x # 优化方法
899
- count = 0
900
- step = 0.01
901
- while count < 100:
902
- a, b, c = _get_ac(b)
903
- s = _sum_squared_error([a, b, c])
904
- b_left, b_right = b - step * b, b + step * b
905
- s_left = _sum_squared_error(_get_ac(b_left))
906
- s_right = _sum_squared_error(_get_ac(b_right))
907
- if s_left > s > s_right:
908
- b = b_right
909
- continue
910
- elif s_left < s < s_right:
911
- b = b_left
912
- continue
913
- elif s_left < s_right:
914
- b = (b + b_left) / 2
915
- else:
916
- b = (b + b_right) / 2
917
- count += 1
918
- step = step * 0.5
919
- if step < 0.000001:
920
- break
903
+ b, count = newton_minimize_1d(_get_s, x0=ini_b, h=1e-5, tol=1e-8, max_iter=30)
904
+ except Exception as e:
905
+ # 使用 scipy 一维优化
906
+ res = minimize_scalar(_get_s, bounds=(-2, 2), method='bounded', options={'xatol': 1e-8})
907
+ b, count = res.x, res.nfev
921
908
 
922
- except RuntimeError:
923
- raise RuntimeError
924
- except np.linalg.LinAlgError:
925
- raise np.linalg.LinAlgError
926
- except TypeError:
927
- raise TypeError
928
- except IndexError:
929
- raise IndexError
930
-
931
- f = linest(a0, [b ** _x for _x in a1])
932
- a, sea, c, sec = f[5][1], f[6][1], f[0], f[1]
909
+ # print(f"{b = }, {count = }")
933
910
 
934
- calculated_y = [_exp_func(i, a, b, c) for i in a1]
935
- resid = [(calculated_y[i] - a0[i]) ** 2 for i in range(len(a0))]
936
- reg = [(i - sum(calculated_y) / len(calculated_y)) ** 2 for i in calculated_y]
937
- ssresid = sum(resid)
938
- ssreg = sum(reg)
939
- sstotal = ssreg + ssresid
940
- dp = len(a1)
941
- df = dp - 1
942
- m_ssresid = ssresid / df
911
+ x = np.concatenate(([np.ones(len(a1))], [np.exp(b * np.array(a1))]), axis=0).transpose()
912
+ m, n = x.shape # number of data, number of unknown x
913
+ try:
914
+ inv_xtx = np.linalg.inv(np.matmul(x.transpose(), x))
915
+ except np.linalg.LinAlgError:
916
+ raise np.linalg.LinAlgError(f"The determinant of the given matrix must not be zero ")
917
+ beta = np.matmul(inv_xtx, np.matmul(x.transpose(), y))
918
+ estimate_y = np.matmul(x, beta)
919
+ resid = (estimate_y - y) ** 2
920
+ reg = (estimate_y - np.mean(y)) ** 2
921
+ ssresid = resid.sum() # 残差平方和
922
+ ssreg = reg.sum() # 回归平方和
923
+ sstotal = ((y - np.mean(y)) ** 2).sum()
943
924
  r2 = ssreg / sstotal if sstotal != 0 else 1
944
925
 
945
- z = [b ** i for i in a1]
926
+ df = m - n
927
+ m_ssresid = ssresid / df # 均方残差,与加权平均中的MSWD对应
928
+ cov_beta = m_ssresid * inv_xtx
929
+
930
+ sc, sa = np.diagonal(cov_beta) ** .5
931
+ c, a = beta.flatten()
946
932
  intercept = a + c
947
- se_intercept_1 = np.sqrt(sea ** 2 + sec ** 2)
948
- # calculate error of intercept
949
- errfz = pow(sum([i ** 2 for i in z]) / (dp * sum([i ** 2 for i in z]) - sum(z) ** 2), 0.5)
950
- errfx = pow(sum([i ** 2 for i in a1]) / (dp * sum([i ** 2 for i in a1]) - sum(a1) ** 2), 0.5)
951
- # seb = errfz * sey = errfz * ssresid / df -> se_intercept = sey * errfx = seb / errfz * errfx
952
- se_intercept = sec / errfz * errfx
953
- rse_intercept = se_intercept / intercept * 100
933
+ se_intercept = pow(sa ** 2 + sc ** 2 + 2 * cov_beta[0][1] , 0.5)
954
934
 
955
- return intercept, se_intercept, rse_intercept, r2, 'mswd', [a, b, c], 'se', \
956
- lambda x: [_exp_func(i, a, b, c) for i in x], m_ssresid
935
+ return intercept, se_intercept, se_intercept / intercept * 100, r2, m_ssresid, [a, b, c], 'se', \
936
+ lambda x: [_exp_func(xi, a, b, c) for xi in x], m_ssresid
937
+
938
+
939
+ def newton_minimize_1d(func, x0, h=1e-5, tol=1e-8, max_iter=20):
940
+ x = x0
941
+ for i in range(max_iter):
942
+ fx = func(x)
943
+ if not np.isfinite(fx):
944
+ raise ValueError("Newton Minimize: Function returned non-finite value at x={}".format(x))
945
+
946
+ # 一阶导数(中心差分)
947
+ f_plus = func(x + h)
948
+ f_minus = func(x - h)
949
+ if not (np.isfinite(f_plus) and np.isfinite(f_minus)):
950
+ raise ValueError("Newton Minimize: Non-finite values in derivative computation")
951
+
952
+ df = (f_plus - f_minus) / (2 * h)
953
+ d2f = (f_plus - 2 * fx + f_minus) / (h * h)
954
+
955
+ # 牛顿步长
956
+ if abs(d2f) < 1e-12: # 避免除零或平坦区域
957
+ print("Newton Minimize: Second derivative too small, stopping.")
958
+ break
959
+
960
+ step = df / d2f
961
+ x_new = x - step
962
+
963
+ if abs(step) < tol:
964
+ x = x_new
965
+ break
966
+
967
+ x = x_new
957
968
 
969
+ return x, i + 1
958
970
 
959
971
  """ line functions """
960
972
 
@@ -12,31 +12,9 @@
12
12
 
13
13
  import os
14
14
  import pickle
15
- import traceback
16
15
  import json
17
16
 
18
17
 
19
- def upload(file, media_dir):
20
- try:
21
- name, suffix = os.path.splitext(file.name)
22
- if suffix.lower() not in [
23
- '.xls', '.age', '.xlsx', '.arr', '.jpg', '.png', '.txt',
24
- '.log', '.seq', '.json', '.ahd', '.csv', '.ngxdp']:
25
- raise TypeError(f"The imported file is not supported: {suffix}")
26
- web_file_path = os.path.join(media_dir, file.name)
27
- with open(web_file_path, 'wb') as f:
28
- for chunk in file.chunks():
29
- f.write(chunk)
30
- print("File path on the server: %s" % web_file_path)
31
- except PermissionError:
32
- raise ValueError(f'Permission denied')
33
- except (Exception, BaseException) as e:
34
- print(traceback.format_exc())
35
- raise ValueError(f'Error in opening file: {e}')
36
- else:
37
- return web_file_path, name, suffix
38
-
39
-
40
18
  def read(file_path):
41
19
  """ Read text files, default 'r', 'rb'
42
20
  Parameters
@@ -493,40 +493,41 @@ def open_full_xls(file_path: str, sample_name: str = ''):
493
493
  res['Inverse Isochron Table'], rows, [4, 5, 6, 7, 10])
494
494
  total_param = arr.partial(
495
495
  res['Sample Parameters'], rows, [
496
- # 1, 2, # 0-1
497
- -1, -1, -1, -1, # 2-5
498
- -1, -1, -1, -1, # 6-9
499
- -1, -1, -1, -1, -1, -1, # 10-15
500
- -1, -1, -1, -1, # 16-19
501
- -1, -1, # 20-21
502
- 23, 24, -1, -1, -1, -1, # 22-27
503
- -1, -1, # 28-29
504
- 22, -1, -1, -1, # 30-33
505
- -1, -1, # 34-35
506
- -1, -1, # 36-37
507
- -1, -1, # 38-39
508
- -1, -1, # 40-41
509
- -1, -1, # 42-43
510
- -1, -1, # 44-45
511
- -1, -1, # 46-47
512
- -1, -1, # 48-49
513
- -1, -1, # 50-51
514
- -1, -1, # 52-53
515
- -1, -1, # 54-55
516
- -1, -1, # 56-57
517
- -1, -1, # 58-59
518
- -1, -1, -1, -1, -1, -1, -1, -1, -1, # 60-68
519
- 10, 11, 12, 13, # 69-72
520
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, # 73-84
521
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, # 85-94
522
- -1, -1, -1, -1, # 95-98
523
- -1, -1, -1, -1, # 99-102
524
- -1, -1, -1, -1, # 103-106
525
- -1, -1, -1, -1, # 107-110
526
- -1, -1, -1, -1, # 111-114
527
- -1, -1, -1, -1, -1, -1, -1, -1, # 115-122
528
- -1, -1, -1, -1, -1, -1, -1, -1, # 123-130
529
- -1, -1, -1, -1, -1, # 131-135
496
+ # 1, 2, -1, # 0-2
497
+ -1, -1, -1, -1, # 3-6
498
+ -1, -1, -1, -1, # 7-10
499
+ -1, -1, -1, -1, -1, -1, # 11-16
500
+ -1, -1, -1, -1, # 17-20
501
+ -1, -1, # 21-22
502
+ 23, 24, -1, -1, -1, -1, # 23-28
503
+ -1, -1, # 29-30
504
+ 22, -1, -1, -1, # 31-34
505
+ -1, -1, # 35-36
506
+ -1, -1, # 37-38
507
+ -1, -1, # 39-40
508
+ -1, -1, # 41-42
509
+ -1, -1, # 43-44
510
+ -1, -1, # 45-46
511
+ -1, -1, # 47-48
512
+ -1, -1, # 49-50
513
+ -1, -1, # 51-52
514
+ -1, -1, # 53-54
515
+ -1, -1, # 55-56
516
+ -1, -1, # 57-58
517
+ -1, -1, # 59-60
518
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, # 61-69
519
+ 10, 11, 12, 13, # 70-73
520
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, # 74-85
521
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, # 86-95
522
+ -1, -1, -1, -1, # 96-99
523
+ -1, -1, -1, -1, # 100-103
524
+ -1, -1, -1, -1, # 104-107
525
+ -1, -1, -1, -1, # 108-111
526
+ -1, -1, -1, -1, # 112-115
527
+ -1, -1, -1, -1, -1, -1, -1, -1, # 116-123
528
+ -1, -1, -1, -1, -1, -1, -1, -1, # 124-131
529
+ -1, -1, -1, -1, -1, # 132-136
530
+ -1, -1, -1, -1, # 137-140
530
531
  ])
531
532
 
532
533
  month_convert = {'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06',
@@ -65,7 +65,7 @@ DEFAULT_SAMPLE_INFO = {
65
65
 
66
66
 
67
67
 
68
- def open_file(file_path: str, input_filter: List[Union[str, int, bool]]):
68
+ def open_file(file_path: str, input_filter: List[Union[str, int, bool]], name=None):
69
69
  """
70
70
  Parameters
71
71
  ----------
@@ -94,63 +94,14 @@ def open_file(file_path: str, input_filter: List[Union[str, int, bool]]):
94
94
  """
95
95
  extension = str(os.path.split(file_path)[-1]).split('.')[-1]
96
96
  try:
97
- handler = {'txt': open_raw_txt, 'excel': open_raw_xls,
98
- 'Qtegra Exported XLS': open_qtegra_exported_xls, 'Seq': open_raw_seq}[
99
- ['txt', 'excel', 'Qtegra Exported XLS', 'Seq'][int(input_filter[1])]]
97
+ handler = {'txt': open_raw_txt, 'excel': open_raw_xls, 'Seq': open_raw_seq}[
98
+ ['txt', 'excel', 'Qtegra Excel (deleted)', 'Seq'][int(input_filter[1])]]
100
99
  except KeyError:
101
100
  raise FileNotFoundError("Wrong File.")
102
- return handler(file_path, input_filter)
101
+ return handler(file_path, input_filter, name)
103
102
 
104
103
 
105
- def open_qtegra_exported_xls(filepath, input_filter=None):
106
- if input_filter is None:
107
- input_filter = []
108
- try:
109
- wb = open_workbook(filepath)
110
- sheets = wb.sheet_names()
111
- sheet = wb.sheet_by_name(sheets[0])
112
- value, step_header, step_list = [], [], []
113
- for row in range(sheet.nrows):
114
- row_set = []
115
- for col in range(sheet.ncols):
116
- if sheet.cell(row, col).value == '':
117
- pass
118
- else:
119
- row_set.append(sheet.cell(row, col).value)
120
- if row_set != [] and len(row_set) > 1:
121
- value.append(row_set)
122
- for each_row in value:
123
- # if the first item of each row is float (1.0, 2.0, ...) this row is the header of a step.
124
- if isinstance(each_row[0], float):
125
- each_row[0] = int(each_row[0])
126
- if "M" in each_row[1].upper():
127
- each_row[1] = datetime.strptime(each_row[1], '%m/%d/%Y %I:%M:%S %p').isoformat(timespec='seconds')
128
- else:
129
- each_row[1] = datetime.strptime(each_row[1], '%m/%d/%Y %H:%M:%S').isoformat(timespec='seconds')
130
- step_header.append(each_row)
131
- for step_index, each_step_header in enumerate(step_header):
132
- row_start_number = value.index(each_step_header)
133
- try:
134
- row_stop_number = value.index(step_header[step_index + 1])
135
- except IndexError:
136
- row_stop_number = len(value) + 1
137
- step_values = [
138
- each_step_header[0:4],
139
- *list(map(
140
- # lambda x: [x[0], x[1], x[2], x[1], x[3], x[1], x[4], x[1], x[5], x[1], x[6]],
141
- # x[1] = time, x[2] = H2:40, x[3] = H1: 39, x[4] = AX: 38, x[5] = L1: 37, x[6] = L2: 36
142
- lambda x: [x[0], x[1], x[6], x[1], x[5], x[1], x[4], x[1], x[3], x[1], x[2]],
143
- # in sequence: Ar36, Ar37, Ar38, Ar39, Ar40
144
- [value[i] for i in range(row_start_number + 2, row_stop_number - 7, 1)]))
145
- ]
146
- step_list.append(step_values)
147
- except Exception as e:
148
- raise ValueError('Error in opening the original file: %s' % str(e))
149
- else:
150
- return {'data': step_list}
151
-
152
-
153
- def open_raw_txt(file_path, input_filter: List[Union[str, int]]):
104
+ def open_raw_txt(file_path, input_filter: List[Union[str, int]], name=None):
154
105
  """
155
106
  Parameters
156
107
  ----------
@@ -173,12 +124,12 @@ def open_raw_txt(file_path, input_filter: List[Union[str, int]]):
173
124
  lines = [line.strip().split(['\t', ';', " ", ",", input_filter[3]][int(input_filter[2])])
174
125
  for line in contents.decode(encoding=encoding["encoding"]).split('\r\n')]
175
126
 
176
- file_name = os.path.basename(file_path).rstrip(os.path.splitext(file_path)[-1])
127
+ file_name = os.path.basename(file_path).rstrip(os.path.splitext(file_path)[-1]) if name is None else name
177
128
  step_list = get_raw_data([lines], input_filter, file_name=file_name)
178
129
  return {'data': step_list}
179
130
 
180
131
 
181
- def open_raw_xls(file_path, input_filter: List[Union[str, int]]):
132
+ def open_raw_xls(file_path, input_filter: List[Union[str, int]], name):
182
133
  """
183
134
  Parameters
184
135
  ----------
@@ -205,13 +156,13 @@ def open_raw_xls(file_path, input_filter: List[Union[str, int]]):
205
156
 
206
157
  wb = open_workbook(file_path)
207
158
  contents = [_get_content_from_sheet(i) for i in range(100)]
208
- file_name = os.path.basename(file_path).rstrip(os.path.splitext(file_path)[-1])
159
+ file_name = os.path.basename(file_path).rstrip(os.path.splitext(file_path)[-1]) if name is None else name
209
160
  step_list = get_raw_data(contents, input_filter, file_name=file_name)
210
161
 
211
162
  return {'data': step_list}
212
163
 
213
164
 
214
- def open_raw_seq(file_path, input_filter=None):
165
+ def open_raw_seq(file_path, input_filter=None, name=None):
215
166
 
216
167
  class RenameUnpickler(pickle.Unpickler):
217
168
  def find_class(self, module: str, name: str):
@@ -257,20 +208,29 @@ def get_raw_data(file_contents: List[List[Union[int, float, str, bool, list]]],
257
208
 
258
209
  """
259
210
 
260
- def datetime_parse(string, f):
211
+ def datetime_parse(string: str, f: str):
261
212
  try:
262
213
  return datetime.strptime(string, f)
263
214
  except ValueError as v:
215
+ print(traceback.format_exc())
264
216
  if f.strip() == "":
265
217
  return datetime_parser.parse(string)
266
218
  elif len(v.args) > 0 and v.args[0].startswith('unconverted data remains: '):
267
- string = string[:-(len(v.args[0]) - 26)]
268
- return datetime.strptime(string, f)
219
+ # %f handles microseconds (6 digits), remove the remains
220
+ return datetime_parse(string[:-(len(v.args[0]) - 26)], f)
221
+ # elif f.upper()[-2:] in ['PM', 'AM']:
222
+ # return datetime_parse(string, f[:-2])
269
223
  else:
270
224
  raise
271
225
 
226
+ def step_num_parse(s: str):
227
+ try:
228
+ return f"{round(float(s)):02d}"
229
+ except (ValueError, TypeError):
230
+ return s
231
+
272
232
  step_list = []
273
- idx = step_index = 0
233
+ idx = 0 # step_index
274
234
 
275
235
  header = input_filter[5]
276
236
  isotope_index = input_filter[8:28]
@@ -293,13 +253,10 @@ def get_raw_data(file_contents: List[List[Union[int, float, str, bool, list]]],
293
253
  if res is not None and "en" in res.named.keys():
294
254
  experiment_name = res.named.get("en")
295
255
  if res is not None and "sn" in res.named.keys():
296
- step_index = res.named.get("sn")
297
- if step_index.isnumeric():
298
- step_name = f"{experiment_name}-{int(step_index):02d}"
299
- else:
300
- step_name = f"{experiment_name}-{step_index}"
301
- if step_name == "":
302
- raise ValueError(f"Step name not found")
256
+ step_name = res.named.get("sn")
257
+ if str(step_name) == "":
258
+ raise ValueError(f"Step name not found, break reading steps")
259
+ step_name = f"{experiment_name}-{step_num_parse(step_name)}"
303
260
  except (TypeError, ValueError, IndexError):
304
261
  # When parsing the step name fails, the end of the file has been reached
305
262
  # raise
@@ -417,7 +374,7 @@ def get_raw_data(file_contents: List[List[Union[int, float, str, bool, list]]],
417
374
  step_list.append(current_step)
418
375
  idx = data_index[28] * len(step_list)
419
376
  if not check_box_index[0] or len(step_list) >= 500: # check_box_index[0]: multiple sequences
420
- print(f"Multiple Sequence = {check_box_index[0]}, Step number = {len(step_list)}")
377
+ # print(f"Multiple Sequence = {check_box_index[0]}, Step number = {len(step_list)}")
421
378
  break
422
379
 
423
380
  if not step_list:
@@ -4436,7 +4436,7 @@ def get_random_dir(path: str, length=7, random_index=""):
4436
4436
  try:
4437
4437
  exist_ok = random_index != ""
4438
4438
  if random_index == "":
4439
- random_index = calc.basic.get_random_digits(length=length)
4439
+ random_index = calc.basic.get_random_string(length=length)
4440
4440
  destination_folder = os.path.join(path, random_index)
4441
4441
  os.makedirs(destination_folder, exist_ok=exist_ok)
4442
4442
  return destination_folder, random_index