-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathread_write.ml
More file actions
101 lines (88 loc) · 3.39 KB
/
read_write.ml
File metadata and controls
101 lines (88 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
(* read_write.ml: Reading and writing MCMC samples to/from files.
Copyright (C) 2011 Will M. Farr <w-farr@northwestern.edu>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. *)
open Mcmc
let write_sample to_coords chan
{value = v;
like_prior = {log_likelihood = ll;
log_prior = lp}} =
Array.iter (fun x -> Printf.fprintf chan "%g " x) (to_coords v);
Printf.fprintf chan "%g %g\n" ll lp
let write to_coords chan mcmcs =
Array.iter
(fun samp -> write_sample to_coords chan samp)
mcmcs
let read_one_line from_coords string =
let ea = Earray.make 0 0.0 in
let buf = Scanf.Scanning.from_string string in
(try
while true do
Scanf.bscanf buf " %g " (fun x -> Earray.append ea x)
done
with
| End_of_file -> ());
let pts = Earray.to_array ea in
let n = Array.length pts in
{value = from_coords (Array.sub pts 0 (n-2));
like_prior = {log_likelihood = pts.(n-2);
log_prior = pts.(n-1)}}
let read_sample from_coords chan =
let line = input_line chan in
read_one_line from_coords line
let read from_coords chan =
let ea = Earray.of_array [||] in
try
while true do
Earray.append ea (read_sample from_coords chan)
done;
Earray.to_array ea
with
| End_of_file -> Earray.to_array ea
let write_nested to_coords chan (log_ev, log_dev, all_pts, wts) =
Printf.fprintf chan "%g %g\n" log_ev log_dev;
Array.iteri (fun i sample ->
let wt = wts.(i) in
Array.iter (fun x -> Printf.fprintf chan "%g " x) (to_coords sample.value);
Printf.fprintf chan "%g %g " sample.like_prior.log_likelihood sample.like_prior.log_prior;
Printf.fprintf chan "%g\n" wt)
all_pts
let read_nested_log_ev_dev string =
let buf = Scanf.Scanning.from_string string in
Scanf.bscanf buf " %g %g " (fun x y -> (x,y))
let read_nested_sample from_coord string =
let buf = Scanf.Scanning.from_string string in
let ea = Earray.of_array [||] in
(try
while true do
Earray.append ea (Scanf.bscanf buf " %g " (fun x -> x))
done;
()
with
| End_of_file -> ());
let pt = Earray.to_array ea in
let n = Array.length pt in
({value = from_coord (Array.sub pt 0 (n-3));
like_prior = {log_likelihood = pt.(n-3);
log_prior = pt.(n-2)}},
pt.(n-1))
let read_nested from_coord chan =
let (log_ev, log_dev) = read_nested_log_ev_dev (input_line chan) in
let ea = Earray.of_array [||] in
(try
while true do
Earray.append ea (read_nested_sample from_coord (input_line chan))
done;
()
with
| End_of_file -> ());
let pts_and_wts = Earray.to_array ea in
(log_ev, log_dev, Array.map fst pts_and_wts, Array.map snd pts_and_wts)