triallength.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. # ------------------------------------------------------------------ #
  2. # checking the dependence of estimated tau vs given tau as a
  3. # function of trial length.
  4. #
  5. # now using higher activity a=1000 without subsampling
  6. #
  7. # also saving the rk to hdf5. want to merge into one large file later
  8. # ------------------------------------------------------------------ #
  9. import os
  10. import numpy as np
  11. import h5py
  12. from itertools import product
  13. import sys
  14. # temp = sys.path[0]
  15. # sys.path[0]='/scratch/nst/projects/paul_toolbox/mrestimator/'
  16. # sys.path.append(temp)
  17. # v0.1.6b2
  18. import mrestimator as mre
  19. import matplotlib.pyplot as plt
  20. from time import gmtime, strftime
  21. import fcntl
  22. # writing to the same file from different threads
  23. numtrials = 50
  24. numboot = 0
  25. numreps = 100
  26. targettau = np.logspace(1, 3, 5)
  27. targetlength = np.logspace(1, 4, 25) # in multiples of tau
  28. targetlength = np.insert(targetlength, 0, [1, 2, 3, 4, 5, 6 ,7, 8, 9])
  29. # let's skip the complex fit here
  30. fitfuncs = ["e", "eo"]
  31. methods = ["ts", "sm"]
  32. ranks = np.arange(numreps)
  33. # set directory to the location of this script file to use relative paths
  34. os.chdir(os.path.dirname(__file__))
  35. path_base = "../dat"
  36. # os.makedirs(path_base, exist_ok=True)
  37. seed = 80000
  38. # call with -t 1-17000
  39. arg_list = product(targettau, targetlength, ranks)
  40. prev = 0
  41. temp = []
  42. for i in arg_list:
  43. i = i + (seed,)
  44. temp.append(i)
  45. seed += 1
  46. arg_list = temp
  47. mre.ut._logstreamhandler.setLevel("ERROR")
  48. mre.ut._logfilehandler.setLevel("ERROR")
  49. mre.ut._logstreamhandler.setFormatter(
  50. mre.ut.CustomExceptionFormatter(
  51. "%(asctime)s %(levelname)8s: %(message)s",
  52. "%Y-%m-%d %H-%M-%S",
  53. force_disable_trace=True,
  54. )
  55. )
  56. my_rank = int(sys.argv[1])
  57. t, numsteps, rdx, my_seed = arg_list[my_rank - 1]
  58. print(f"parameter combinations: {len(arg_list)}")
  59. print(f"this is: {my_rank}")
  60. print(f"writing to {path_base}/{my_rank}.hdf5")
  61. # 1_method 2_fitfunc 3_targettau 4_targetlenth_in_units_of_tau 5_repetition 6_tau_measured 7_seed 8_SGE_TASK_ID
  62. kmax = int(20 * t)
  63. steps = (1, kmax)
  64. dist = 1
  65. print(
  66. f"\ttau: {t:.2e}, kmax: {kmax:.2e}, dist: {dist:.2e}, numsteps: {numsteps:.2e} [times tau]"
  67. )
  68. print(f'Repetition: {rdx}/{numreps} {strftime("%Y-%m-%d %H-%M-%S", gmtime())}')
  69. print(f"branching process ...")
  70. numsteps = int(numsteps)
  71. bpar = np.exp(-1 * 1 / t)
  72. bp = mre.simulate_branching(
  73. bpar, a=1000, seed=my_seed, numtrials=numtrials, length=int(numsteps * t)
  74. )
  75. with h5py.File(path_base + f"/{my_rank}.hdf5", "w") as h5f:
  76. f_rk = h5f.create_group("rks")
  77. f_fits = h5f.create_group("fits")
  78. f_meta = h5f.create_group("meta")
  79. f_meta.create_dataset("tau", data=t)
  80. f_meta.create_dataset("length", data=int(numsteps * t))
  81. f_meta.create_dataset("numsteps", data=int(numsteps))
  82. f_meta.create_dataset("repetition", data=rdx)
  83. for mdx, m in enumerate(methods):
  84. print(f"coefficients for {m} ...")
  85. rk = mre.coefficients(
  86. bp, steps=steps, numboot=numboot, dt=1, dtunit="steps", method=m
  87. )
  88. print(f"writing coefficients ...")
  89. f_rk.create_dataset(m, data=rk.coefficients, compression="gzip")
  90. if mdx == 0:
  91. f_rk.create_dataset("steps", data=rk.steps, compression="gzip")
  92. print(f"fitting ...")
  93. for fdx, f in enumerate(fitfuncs):
  94. f_group = f_fits.create_group(f"{m}_{f}")
  95. try:
  96. fitres = mre.fit(rk, numboot=numboot, fitfunc=f)
  97. except Exception:
  98. fitres = mre.FitResult(np.nan, np.nan, f)
  99. # ugly to write is as string but im afraid things might break elsewise
  100. f_group.create_dataset(f"mdx", data=mdx)
  101. f_group.create_dataset(f"fdx", data=fdx)
  102. f_group.create_dataset(f"target_tau", data=t)
  103. f_group.create_dataset(f"numsteps", data=numsteps)
  104. f_group.create_dataset(f"rdx", data=rdx)
  105. f_group.create_dataset(f"fitres_tau", data=fitres.tau)
  106. f_group.create_dataset(f"my_seed", data=my_seed)
  107. f_group.create_dataset(f"my_rank", data=my_rank)
  108. # this is redundant and not quite needed.
  109. # new_entry = f"{mdx}\t"
  110. # new_entry += f"{fdx}\t"
  111. # new_entry += f"{t:.2e}\t"
  112. # new_entry += f"{numsteps:.2e}\t"
  113. # new_entry += f"{rdx}\t"
  114. # new_entry += f"{fitres.tau}\t"
  115. # new_entry += f"{my_seed}\t"
  116. # new_entry += f"{my_rank}\n"
  117. # with open(path_base+f"/{my_rank}.tsv", "a") as g:
  118. # fcntl.flock(g, fcntl.LOCK_EX)
  119. # g.write(new_entry)
  120. # fcntl.flock(g, fcntl.LOCK_UN)
  121. print(f'{strftime("%Y-%m-%d %H-%M-%S", gmtime())} All done')