Skip to content

Commit 5cf8bbf

Browse files
authored
Merge pull request #201 from andthum/feat/scripts/discretization/back-jump-probability
`back_jump_prob*.py`: Return normalization factors
2 parents b1c75dd + 8a16315 commit 5cf8bbf

File tree

2 files changed

+68
-11
lines changed

2 files changed

+68
-11
lines changed

scripts/discretization/back_jump_prob.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,12 @@
242242
print("Last frame to read: {:>8d}".format(END - 1))
243243
print("Read every n-th frame: {:>8d}".format(EVERY))
244244
timer = datetime.now()
245-
prob = mdt.dtrj.back_jump_prob(
245+
prob, norm = mdt.dtrj.back_jump_prob(
246246
dtrj,
247247
continuous=args.CONTINUOUS,
248248
discard_neg=args.DISCARD_NEG,
249249
discard_neg_btw=args.DISCARD_NEG_BTW,
250+
return_norm=True,
250251
verbose=True,
251252
)
252253
del dtrj
@@ -260,6 +261,10 @@
260261
"Back-jump probability: Probability to return to the initial state\n"
261262
+ "after a state transition as function of the time that has passed\n"
262263
+ "since the state transition averaged over all states.\n"
264+
+ "\n"
265+
+ "The total number of back jumps at a given lag time can be\n"
266+
+ "obtained by multiplying the back-jump probabilities with the\n"
267+
+ "normalization factors.\n"
263268
+ "\n\n"
264269
+ "intermittency: {}\n".format(args.INTERMITTENCY)
265270
+ "continuous: {}\n".format(args.CONTINUOUS)
@@ -272,13 +277,14 @@
272277
header += (
273278
"The columns contain:\n"
274279
+ " 1 Lag time (in trajectory steps)\n"
275-
+ " 2 back-jump probability\n"
280+
+ " 2 Back-jump probability\n"
281+
+ " 3 Normalization factors\n"
276282
+ "\n"
277283
+ "Column number:\n"
278284
+ "{:>14d} {:>16d}".format(1, 2)
279285
)
280286
lag_times = np.arange(0, len(prob) * EVERY, EVERY, dtype=np.uint32)
281-
data = np.column_stack([lag_times, prob])
287+
data = np.column_stack([lag_times, prob, norm])
282288
mdt.fh.savetxt(args.OUTFILE, data, header=header)
283289
print("Created {}".format(args.OUTFILE))
284290
print("Elapsed time: {}".format(datetime.now() - timer))

scripts/discretization/back_jump_prob_discrete.py

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@
4141
second discrete trajectory must have the same shape as the first
4242
discrete trajectory.
4343
-o
44-
Output filename.
44+
Output filename of the file containing the back-jump probabilities.
45+
--norm-out
46+
Output filename of the file containing the normalization factors
47+
(optional). Multiplying the back-jump probabilities with the
48+
normalization factors yields the total number of back jumps.
4549
-b
4650
First frame to read from the discrete trajectory. Frame numbering
4751
starts at zero. Default: ``0``.
@@ -148,7 +152,21 @@
148152
dest="OUTFILE",
149153
type=str,
150154
required=True,
151-
help="Output filename.",
155+
help=(
156+
"Output filename of the file containing the back-jump"
157+
" probabilities."
158+
),
159+
)
160+
parser.add_argument(
161+
"--norm-out",
162+
dest="OUTFILE_NORM",
163+
type=str,
164+
required=False,
165+
default=None,
166+
help=(
167+
"Output filename of the file containing the normalization factors"
168+
" (optional)."
169+
),
152170
)
153171
parser.add_argument(
154172
"-b",
@@ -300,31 +318,37 @@
300318
continuous=args.CONTINUOUS,
301319
discard_neg=args.DISCARD_NEG,
302320
discard_neg_btw=args.DISCARD_NEG_BTW,
321+
return_norm=args.OUTFILE_NORM is not None,
303322
verbose=True,
304323
)
324+
if args.OUTFILE_NORM is not None:
325+
prob, norm = prob
305326
del dtrj1, dtrj2
306327
print("Elapsed time: {}".format(datetime.now() - timer))
307328
print("Current memory usage: {:.2f} MiB".format(mdt.rti.mem_usage(proc)))
308329

309330
print("\n")
310331
print("Creating output...")
311332
timer = datetime.now()
312-
header = (
333+
header_prob = (
313334
"Back-jump probability: Probability to return to the initial state\n"
314335
+ "after a state transition as function of the time that has passed\n"
315336
+ "since the state transition resolved with respect to the states in\n"
316337
+ "a second discrete trajectory.\n"
317-
+ "\n\n"
338+
)
339+
header_base = (
340+
"\n\n"
318341
+ "intermittency_1: {}\n".format(args.INTERMITTENCY1)
319342
+ "intermittency_2: {}\n".format(args.INTERMITTENCY2)
320343
+ "continuous: {}\n".format(args.CONTINUOUS)
321344
+ "discard_neg: {}\n".format(args.DISCARD_NEG)
322345
+ "discard_neg_btw: {}\n".format(args.DISCARD_NEG_BTW)
323346
+ "\n\n"
324347
)
325-
header += dtrj1_trans_info_str
326-
header += "\n\n"
327-
header += (
348+
header_base += dtrj1_trans_info_str
349+
header_base += "\n\n"
350+
header_prob = header_prob + header_base
351+
header_prob += (
328352
"The first column contains the lag times (in trajectory steps).\n"
329353
"The first row contains the states of the second discrete trajectory\n"
330354
"that were used to discretize the back-jump probability.\n"
@@ -333,9 +357,36 @@
333357
)
334358
lag_times = np.arange(0, prob.shape[1] * EVERY, EVERY, dtype=np.uint32)
335359
mdt.fh.savetxt_matrix(
336-
args.OUTFILE, prob.T, var1=lag_times, var2=dtrj2_states, header=header
360+
args.OUTFILE,
361+
prob.T,
362+
var1=lag_times,
363+
var2=dtrj2_states,
364+
header=header_prob,
337365
)
338366
print("Created {}".format(args.OUTFILE))
367+
if args.OUTFILE_NORM is not None:
368+
header_norm = (
369+
"Normalization factors used to normalize the back-jump\n"
370+
"probabilities.\n"
371+
"Multiplying the back-jump probabilities with the normalization\n"
372+
"factors yields the total number of back jumps.\n"
373+
)
374+
header_norm = header_norm + header_base
375+
header_norm += (
376+
"The first column contains the lag times (in trajectory steps).\n"
377+
"The first row contains the states of the second discrete"
378+
" trajectory\n"
379+
"that were used to discretize the back-jump probability.\n"
380+
"The remaining matrix elements are the normalization factors.\n"
381+
)
382+
mdt.fh.savetxt_matrix(
383+
args.OUTFILE_NORM,
384+
norm.T,
385+
var1=lag_times,
386+
var2=dtrj2_states,
387+
header=header_norm,
388+
)
389+
print("Created {}".format(args.OUTFILE_NORM))
339390
print("Elapsed time: {}".format(datetime.now() - timer))
340391
print("Current memory usage: {:.2f} MiB".format(mdt.rti.mem_usage(proc)))
341392

0 commit comments

Comments
 (0)