package omlr
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file utls.ml
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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
(* Copyright (C) 2020, Francois Berenger Yamanishi laboratory, Department of Bioscience and Bioinformatics, Faculty of Computer Science and Systems Engineering, Kyushu Institute of Technology, 680-4 Kawazu, Iizuka, Fukuoka, 820-8502, Japan. *) open Printf module A = BatArray module Ht = BatHashtbl module L = BatList module Log = Dolog.Log type filename = string let tap f x = f x; x let fst3 (a, _, _) = a let create_tmp_filename () = let res = Filename.temp_file "" (* no_prefix *) "" (* no_suffix *) in (* tap (Log.info "create_tmp_filename: %s") res; *) res let mkfifo (fn: filename): unit = Unix.mkfifo fn 0o600 (* abort if condition is not met *) let enforce (condition: bool) (err_msg: string): unit = if not condition then failwith err_msg let enforce_f (condition: bool) (msg_constr: unit -> string): unit = if not condition then failwith (msg_constr ()) let with_out_file (fn: filename) (f: out_channel -> 'a): 'a = let output = open_out_bin fn in let res = f output in close_out output; res let with_temp_out_file (f: filename -> 'a): 'a = let temp_file = Filename.temp_file "" (* no_prefix *) "" (* no_suffix *) in let res = f temp_file in Sys.remove temp_file; res let with_in_file (fn: filename) (f: in_channel -> 'a): 'a = let input = open_in_bin fn in let res = f input in close_in input; res let with_in_file2 fn1 fn2 f = let in1 = open_in_bin fn1 in let in2 = open_in_bin fn2 in let res = f in1 in2 in close_in in1; close_in in2; res let with_in_file3 fn1 fn2 fn3 f = let in1 = open_in_bin fn1 in let in2 = open_in_bin fn2 in let in3 = open_in_bin fn3 in let res = f in1 in2 in3 in close_in in1; close_in in2; close_in in3; res let with_infile_outfile (in_fn: filename) (out_fn: filename) (f: in_channel -> out_channel -> 'a): 'a = let input = open_in_bin in_fn in let output = open_out_bin out_fn in let res = f input output in close_in input; close_out output; res let lines_of_file (fn: filename): string list = with_in_file fn (fun input -> let res, exn = L.unfold_exc (fun () -> input_line input) in if exn <> End_of_file then raise exn else res ) let lines_to_file fn l = with_out_file fn (fun out -> L.iter (fprintf out "%s\n") l ) (* alias *) let string_list_to_file = lines_to_file (* read 'nlines' from 'input' in_channel *) let read_n_lines nlines input = assert(nlines >= 0); let rec loop n acc = if n = 0 then L.rev acc else loop (n - 1) (input_line input :: acc) in loop nlines [] (* if the first line is a comment (starts with '#'); then we extract it separately from other lines; else we treat it as any other line *) let maybe_extract_comment_header (fn: filename): string option * string list = let all_lines = lines_of_file fn in match all_lines with | [] -> (None, []) | fst :: others -> if BatString.starts_with fst "#" then (Some fst, others) else (None, all_lines) (* call f on lines of file *) let iter_on_lines_of_file fn f = let input = open_in_bin fn in try while true do f (input_line input) done with End_of_file -> close_in input let iteri_on_lines_of_file fn f = let i = ref 0 in let input = open_in_bin fn in try while true do f !i (input_line input); incr i done with End_of_file -> close_in input let map_on_file (fn: filename) (f: in_channel -> 'a): 'a list = with_in_file fn (fun input -> let res, exn = L.unfold_exc (fun () -> f input) in if exn = End_of_file then res else raise exn ) (* map f on lines of file *) let map_on_lines_of_file (fn: filename) (f: string -> 'a): 'a list = with_in_file fn (fun input -> let res, exn = L.unfold_exc (fun () -> f (input_line input)) in if exn = End_of_file then res else raise exn ) let mapi_on_lines_of_file (fn: filename) (f: int -> string -> 'a): 'a list = with_in_file fn (fun input -> let i = ref 0 in let res, exn = L.unfold_exc (fun () -> let curr = f !i (input_line input) in incr i; curr ) in if exn = End_of_file then res else raise exn ) (* skip 'nb' blocks from file being read *) let skip_blocks nb read_one input = if nb = 0 then () else let () = assert(nb > 0) in for _ = 1 to nb do ignore(read_one input) done let read_lines = lines_of_file let write_lines (lines: string list) (output_fn: filename): unit = with_out_file output_fn (fun out -> List.iter (fprintf out "%s\n") lines ) let output_lines = write_lines (* keep only lines that satisfy p *) let filter_lines_of_file fn p = L.filter p (lines_of_file fn) (* get the first line (stripped) output by given command *) let get_command_output (cmd: string): string = Log.info "get_command_output: %s" cmd; let _stat, output = BatUnix.run_and_read cmd in match BatString.split_on_char '\n' output with | first_line :: _others -> first_line | [] -> (Log.fatal "get_command_output: no output for: %s" cmd; exit 1) (* run the given command in a sub process (in parallel to the current process) and returns its pid so that we can wait for it later *) let fork_out_cmd (cmd: string): int = Log.info "fork_out_cmd: %s" cmd; match Unix.fork () with | 0 -> (* child process *) exit (Sys.command cmd) | -1 -> (* error *) (Log.fatal "fork_out_cmd: fork failed"; exit 1) | pid -> pid (* return full path of command, if found in PATH, none else *) let command_exists (cmd: string): string option = let where_is_cmd = "which " ^ cmd in if Unix.system (where_is_cmd ^ " 2>&1 > /dev/null") = Unix.WEXITED 0 then Some (get_command_output where_is_cmd) else None let run_command (cmd: string): unit = Log.info "run_command: %s" cmd; match Unix.system cmd with | Unix.WSIGNALED _ -> (Log.fatal "run_command: signaled: %s" cmd; exit 1) | Unix.WSTOPPED _ -> (Log.fatal "run_command: stopped: %s" cmd; exit 1) | Unix.WEXITED i when i <> 0 -> (Log.fatal "run_command: exit %d: %s" i cmd; exit 1) | Unix.WEXITED _ (* i = 0 then *) -> () let get_env (env_var: string): string option = try Some (Sys.getenv env_var) with Not_found -> None (* look for exe in PATH then given env. var *) let find_command (exe: string) (env_var: string): string option = match command_exists exe with | Some cmd -> Some cmd | None -> match get_env env_var with | Some cmd -> Some cmd | None -> (Log.warn "%s not found in PATH; \ put it in your PATH or setup the \ %s env. var. to point to it" exe env_var; None) let filename_is_absolute fn = not (Filename.is_relative fn) let relative_to_absolute fn = if Filename.is_relative fn then let cwd = Sys.getcwd () in Filename.concat cwd fn else fn (* remove the prefix if it is there, or do nothing if it is not *) let remove_string_prefix prfx str = if BatString.starts_with str prfx then let prfx_len = String.length prfx in BatString.tail str prfx_len else str let string_contains_non_binary_digit = Str.regexp "[^01]" let string_contains_only_zeros_or_ones (s: string): bool = not (Str.string_match string_contains_non_binary_digit s 0) let string_contains_non_digits_non_sep = Str.regexp "[^-0123456789;]" let string_is_a_list_of_integers (s: string): bool = BatString.starts_with s "[" && BatString.ends_with s "]" && let chopped = BatString.chop ~l:1 ~r:1 s in not (Str.string_match string_contains_non_digits_non_sep chopped 0) let may_apply_opt f = function | Some x -> Some (f x) | None -> None let may_apply f = function | Some x -> f x | None -> () (* returns true if we could create the file; false else (already there) *) let lock_file_for_writing (fn: filename): bool = try let fd = Unix.(openfile fn [O_CREAT; O_EXCL; O_WRONLY] 0o600) in Unix.close fd; true with Unix.Unix_error _ -> false (* compute all distinct pairs ((a,b) = (b,a)) of elements in 'l' *) let all_pairs (l: 'a list): ('a * 'a) list = let pair x ys = L.map (fun y -> (x, y) ) ys in let rec loop acc = function | [] -> acc | x :: xs -> loop (L.rev_append (pair x xs) acc) xs in loop [] l exception Enough_times (* accumulate the result of calling 'f' 'n' times *) let n_times n f = let i = ref 0 in BatList.unfold_exc (fun () -> if !i = n then raise Enough_times else let res = f () in incr i; res ) let push (x: 'a) (l: 'a list ref): unit = l := x :: !l (* the identity function *) let id x = x let one_or_more_spaces = Str.regexp "[ ]+" let string_of_floats_array fv = let buff = Buffer.create 80 in A.iter (fun f -> Buffer.add_string buff (sprintf "%f " f) ) fv; Buffer.contents buff (* enforce filename uses one of the allowed extensions *) let enforce_file_extension allowed_exts fn = assert(L.exists (BatString.ends_with fn) allowed_exts) (* Pi math constant *) let m_pi = 4.0 *. atan 1.0 let prepend x xs = xs := x :: !xs (* test (lb <= x <= hb) *) let in_bounds lb x hb = x >= lb && x <= hb let list_medianf (l: float list): float = let xs = A.of_list l in A.sort BatFloat.compare xs; let n = A.length xs in if n mod 2 = 1 then xs.(n/2) else 0.5 *. (xs.(n/2) +. xs.(n/2 - 1)) (*$T list_medianf list_medianf [1.;2.;3.;4.;5.] = 3.0 list_medianf [1.;2.;3.;4.] = 2.5 *) let string_of_array ?pre:(pre = "[|") ?sep:(sep = ";") ?suf:(suf = "|]") to_str a = let buff = Buffer.create 80 in Buffer.add_string buff pre; A.iteri (fun i x -> if i > 0 then Buffer.add_string buff sep; Buffer.add_string buff (to_str x) ) a; Buffer.add_string buff suf; Buffer.contents buff let string_of_list ?pre:(pre = "[") ?sep:(sep = ";") ?suf:(suf = "]") to_str l = let buff = Buffer.create 80 in Buffer.add_string buff pre; L.iteri (fun i x -> if i > 0 then Buffer.add_string buff sep; Buffer.add_string buff (to_str x) ) l; Buffer.add_string buff suf; Buffer.contents buff (* marshal x to file *) let save fn x = with_out_file fn (fun out -> Marshal.to_channel out x [Marshal.No_sharing] ) (* unmarshal x from file *) let restore fn = with_in_file fn (fun input -> Marshal.from_channel input ) let is_odd i = i mod 2 = 1 let is_even i = i mod 2 = 0 let get_first_line fn = with_in_file fn input_line (* like the cut unix command *) let cut d f line = let splitted = BatString.split_on_char d line in BatList.at splitted f let get_ncores () = int_of_string (get_command_output "getconf _NPROCESSORS_ONLN") let int_of_bool = function | true -> 1 | false -> 0 let bool_of_int = function | 1 -> true | 0 -> false | _ -> assert(false) (* test that 'y' is within 'x' +/- 'epsilon'; i.e. y \in [x - epsilon, x + epsilon] *) let approx_equal epsilon x y = (y >= x -. epsilon) && (y <= x +. epsilon) (* proper NaN/nan testing *) let is_nan x = match classify_float x with | FP_nan -> true | _ -> false (* some statistics *) let faverage = BatList.favg let fmean = faverage (* population standard deviation *) let stddev (l: float list): float = let n, sx, sx2 = List.fold_left (fun (n, sx, sx2) x -> (n +. 1., sx +. x, sx2 +. (x *.x)) ) (0., 0., 0.) l in sqrt ((sx2 -. (sx *. sx) /. n) /. n) (* stddev [2.; 4.; 4.; 4.; 5.; 5.; 7.; 9.] = 2.0 *) let stddev_a (a: float array): float = let n, sx, sx2 = Array.fold_left (fun (n, sx, sx2) x -> (n +. 1., sx +. x, sx2 +. (x *.x)) ) (0., 0., 0.) a in sqrt ((sx2 -. (sx *. sx) /. n) /. n) (* stddev_a [| 2.; 4.; 4.; 4.; 5.; 5.; 7.; 9. |] = 2.0 *) let transpose_matrix a = let dimx = Array.length a in let dimy = Array.length a.(0) in let res = Array.make_matrix dimy dimx a.(0).(0) in for x = 0 to dimx - 1 do for y = 0 to dimy - 1 do res.(y).(x) <- a.(x).(y) done done; res let fincr_by (xref: float ref) (dx: float): unit = xref := !xref +. dx let int_of_digit_char = function | '0' -> 0 | '1' -> 1 | '2' -> 2 | '3' -> 3 | '4' -> 4 | '5' -> 5 | '6' -> 6 | '7' -> 7 | '8' -> 8 | '9' -> 9 | c -> failwith (sprintf "Utls.int_of_digit_char: not a digit: %c" c) let char_of_digit = function | 0 -> '0' | 1 -> '1' | 2 -> '2' | 3 -> '3' | 4 -> '4' | 5 -> '5' | 6 -> '6' | 7 -> '7' | 8 -> '8' | 9 -> '9' | d -> failwith (sprintf "Utls.char_of_digit: not a digit: %d" d) let string_of_digit d = String.make 1 (char_of_digit d) let make_pair x y = (x, y) (* sort then count duplicates *) let list_uniq_count l = let sorted = L.sort compare l in let groups = L.group_consecutive (=) sorted in L.map (fun l -> L.(hd l, length l)) groups (* coarse grain chronometer *) let time_it f = let start = Unix.gettimeofday () in let res = f () in let stop = Unix.gettimeofday () in (stop -. start, res) let ceili (x: float): int = int_of_float (ceil x) let count_lines_of_file (fn: string): int = let count = ref 0 in iter_on_lines_of_file fn (fun _line -> incr count); !count (* read a file containing only floats, one per line *) let floats_from_file fn = map_on_lines_of_file fn float_of_string (* write all floats to file *) let float_list_to_file fn l = with_out_file fn (fun out -> L.iter (fprintf out "%f\n") l ) let list_rev_sort cmp l = List.sort (fun x y -> cmp y x) l let list_really_take n l = let took = L.take n l in assert(L.length took = n); took let dump_to_csv_file fn sep arr = with_out_file fn (fun out -> let dimx = A.length arr in let dimy = A.length arr.(0) in (* CSV header: "c0, c1, ..." *) for x = 0 to dimx - 1 do if x = 0 then fprintf out "c%d" x else fprintf out "%cc%d" sep x done; fprintf out "\n"; for y = 0 to dimy - 1 do for x = 0 to dimx - 1 do if x = 0 then fprintf out "%f" arr.(x).(y) else fprintf out "%c%f" sep arr.(x).(y) done; fprintf out "\n" done )