From 654579b953c75687dd0bd9f7c1bc510f17255093 Mon Sep 17 00:00:00 2001 From: Anthony Scemama Date: Fri, 19 Feb 2016 11:20:34 +0100 Subject: [PATCH] Added range in qmcchem_result --- ocaml/Qmcchem_dataserver.ml | 2 +- ocaml/Qmcchem_result.ml | 66 ++++++++++++++-------- ocaml/Random_variable.ml | 106 ++++++++++++++++++++++++------------ 3 files changed, 117 insertions(+), 57 deletions(-) diff --git a/ocaml/Qmcchem_dataserver.ml b/ocaml/Qmcchem_dataserver.ml index e424676..0509d1b 100644 --- a/ocaml/Qmcchem_dataserver.ml +++ b/ocaml/Qmcchem_dataserver.ml @@ -826,7 +826,7 @@ let run ?(daemon=true) ezfio_filename = with | _ -> () end; - Qmcchem_result.display_summary (); + Qmcchem_result.display_summary ~range:(0.,100.); in (** {3 Main function} *) diff --git a/ocaml/Qmcchem_result.ml b/ocaml/Qmcchem_result.ml index 6420391..23e0660 100644 --- a/ocaml/Qmcchem_result.ml +++ b/ocaml/Qmcchem_result.ml @@ -2,9 +2,9 @@ open Core.Std open Qptypes (** Display a table that can be plotted by gnuplot *) -let display_table property = +let display_table ~range property = let p = Property.of_string property - |> Random_variable.of_raw_data + |> Random_variable.of_raw_data ~range in let conv = Random_variable.convergence p and rconv = Random_variable.rev_convergence p @@ -21,16 +21,16 @@ let display_table property = (** Display a convergence plot of the requested property *) -let display_plot property = +let display_plot ~range property = print_string ("display_plot "^property^".\n") ;; (** Display a convergence table of the error *) -let display_err_convergence property = +let display_err_convergence ~range property = let p = Property.of_string property - |> Random_variable.of_raw_data + |> Random_variable.of_raw_data ~range in let rec aux n p = match Random_variable.ave_error p with @@ -53,10 +53,10 @@ let display_err_convergence property = ;; (** Display the centered cumulants of a property *) -let display_cumulants property = +let display_cumulants ~range property = let p = Property.of_string property - |> Random_variable.of_raw_data + |> Random_variable.of_raw_data ~range in let cum = Random_variable.centered_cumulants p @@ -74,10 +74,10 @@ let display_cumulants property = (** Display a table for the autocovariance of the property *) -let display_autocovariance property = +let display_autocovariance ~range property = let p = Property.of_string property - |> Random_variable.of_raw_data + |> Random_variable.of_raw_data ~range in Random_variable.autocovariance p |> List.iteri ~f:(fun i x -> @@ -85,10 +85,10 @@ let display_autocovariance property = ;; (** Display a histogram of the property *) -let display_histogram property = +let display_histogram ~range property = let p = Property.of_string property - |> Random_variable.of_raw_data + |> Random_variable.of_raw_data ~range in let histo = Random_variable.histogram p @@ -132,12 +132,12 @@ let display_histogram property = (** Display a summary of all the cmoputed quantities *) -let display_summary () = +let display_summary ~range = let properties = Lazy.force Block.properties and print_property property = - let p = Random_variable.of_raw_data property + let p = Random_variable.of_raw_data ~range property in Printf.printf "%20s : %s\n" (Property.to_string property) @@ -147,10 +147,10 @@ let display_summary () = let cpu = - Random_variable.of_raw_data Property.Cpu + Random_variable.of_raw_data ~range Property.Cpu |> Random_variable.sum and wall = - Random_variable.of_raw_data Property.Wall + Random_variable.of_raw_data ~range Property.Wall |> Random_variable.max_value_per_compute_node |> Random_variable.sum in @@ -162,13 +162,25 @@ let display_summary () = ;; -let run ?a ?c ?e ?h ?t ?p ezfio_file = +let run ?a ?c ?e ?h ?t ?p ?rmin ?rmax ezfio_file = Qputils.set_ezfio_filename ezfio_file; - let f (x,func) = - match x with - | Some property -> func property - | None -> () + + let rmin = + match rmin with + | None -> 0. + | Some x when (x<0) -> failwith "rmin should be >= 0" + | Some x when (x>100) -> failwith "rmin should be <= 100" + | Some x -> Float.of_int x + and rmax = + match rmax with + | None -> 100. + | Some x when (x<0) -> failwith "rmax should be >= 0" + | Some x when (x>100) -> failwith "rmax should be <= 100" + | Some x -> Float.of_int x + in + let range = + (rmin, rmax) in let l = @@ -181,6 +193,12 @@ let run ?a ?c ?e ?h ?t ?p ezfio_file = ] in + let f (x,func) = + match x with + | Some property -> func ~range property + | None -> () + in + List.iter ~f l ; @@ -190,7 +208,7 @@ let run ?a ?c ?e ?h ?t ?p ezfio_file = | (Some _,_) -> false ) l ) then - display_summary () + display_summary ~range ;; @@ -207,6 +225,10 @@ let spec = ~doc:"property Display the histogram of the property blocks" +> flag "p" (optional string) ~doc:"property Display a convergence plot for a property" + +> flag "rmin" (optional int) + ~doc:"int Lower bound of the percentage of the total weight to consider (default 0)" + +> flag "rmax" (optional int) + ~doc:"int Upper bound of the percentage of the total weight to consider (default 100)" +> flag "t" (optional string) ~doc:"property Print a table for the convergence of a property" +> anon ("ezfio_file" %: string) @@ -217,7 +239,7 @@ let command = ~summary: "Displays the results computed in an EZFIO directory." ~readme:(fun () -> "Displays the results computed in an EZFIO directory.") spec - (fun a c e h p t ezfio_file () -> run ?a ?c ?e ?h ?t ?p ezfio_file ) + (fun a c e h p rmin rmax t ezfio_file () -> run ?a ?c ?e ?h ?t ?p ?rmin ?rmax ezfio_file ) ;; diff --git a/ocaml/Random_variable.ml b/ocaml/Random_variable.ml index 34efff3..01fc04a 100644 --- a/ocaml/Random_variable.ml +++ b/ocaml/Random_variable.ml @@ -1,5 +1,5 @@ -open Core.Std;; -open Qptypes;; +open Core.Std +open Qptypes type t = { property : Property.t ; @@ -75,14 +75,52 @@ end -(** Build from raw data *) -let of_raw_data ?(locked=true) property = - let data = - Block.raw_data ~locked () - |> List.filter ~f:(fun x -> x.Block.property = property) - in - { property ; data } -;; +(** Build from raw data. Range values are given in percent. *) +let of_raw_data ?(locked=true) ~range property = + let data = + Block.raw_data ~locked () + |> List.filter ~f:(fun x -> x.Block.property = property) + in + + let data_in_range rmin rmax = + + let total_weight = + List.fold_left data ~init:0. ~f:(fun accu x -> + (Weight.to_float x.Block.weight) +. accu + ) + in + + let wmin, wmax = + rmin *. total_weight *. 0.01, + rmax *. total_weight *. 0.01 + in + + let (_, new_data) = + List.fold_left data ~init:(0.,[]) ~f:(fun (wsum, l) x -> + if (wsum > wmax) then + (wsum,l) + else + begin + let wsum_new = + wsum +. (Weight.to_float x.Block.weight) + in + if (wsum_new > wmin) then + (wsum_new, x::l) + else + (wsum_new, l) + end + ) + in + List.rev new_data + in + + let result = + match range with + | (0.,100.) -> { property ; data } + | (rmin,rmax) -> { property ; data=data_in_range rmin rmax } + in + result + (** Compute average *) @@ -121,7 +159,7 @@ let average { property ; data } = in Array.map num ~f:(fun x -> x *. denom_inv) |> Average.of_float_array ~dim -;; + (** Compute sum (for CPU/Wall time) *) @@ -130,7 +168,7 @@ let sum { property ; data } = let num = (Weight.to_float x.Block.weight) *. (Sample.to_float x.Block.value) in accu +. num ) -;; + (** Calculation of the average and error bar *) @@ -212,7 +250,7 @@ let ave_error { property ; data } = | (_,None) -> 0.) |> Average.of_float_array ~dim) ) -;; + @@ -226,7 +264,7 @@ let fold_blocks ~f { property ; data } = let x = Sample.to_float block.Block.value in f accu x ) -;; + (** Convergence plot *) @@ -278,13 +316,13 @@ let convergence { property ; data } = ~n:0. ~accu:[ (s /. w, 0.) ] end -;; + let rev_convergence { property ; data } = let p = { property=property ; data = List.rev data } in convergence p |> List.rev -;; + (** Min and max of block *) @@ -293,14 +331,14 @@ let min_block = if (x < accu) then x else accu ) -;; + let max_block = fold_blocks ~f:(fun accu x -> if (x > accu) then x else accu ) -;; + (** Create a hash table for merging *) @@ -359,7 +397,7 @@ let create_hash ~hashable ~create_key ?(update_block_id=(fun x->x)) t = ) ); table -;; + (** Genergic merge function *) @@ -375,7 +413,7 @@ let merge ~hashable ~create_key ?update_block_id t = else 0) |> List.map ~f:(fun (x,y) -> y) } -;; + (** Merge per block id *) @@ -383,7 +421,7 @@ let merge_per_block_id = merge ~hashable:Int.hashable ~create_key:(fun block -> Block_id.to_int block.Block.block_id) -;; + (** Merge per compute_node *) let merge_per_compute_node = @@ -392,7 +430,7 @@ let merge_per_compute_node = ~create_key:(fun block -> Printf.sprintf "%s" (Compute_node.to_string block.Block.compute_node) ) -;; + (** Merge per Compute_node and PID *) @@ -403,7 +441,7 @@ let merge_per_compute_node_and_pid = Printf.sprintf "%s %10.10d" (Compute_node.to_string block.Block.compute_node) (Pid.to_int block.Block.pid) ) -;; + (** Merge per Compute_node and BlockId *) @@ -414,7 +452,7 @@ let merge_per_compute_node_and_block_id = Printf.sprintf "%s %10.10d" (Compute_node.to_string block.Block.compute_node) (Block_id.to_int block.Block.block_id) ) -;; + @@ -428,7 +466,7 @@ let compress = ~update_block_id:(fun block_id -> ((Block_id.to_int block_id)+1)/2 |> Block_id.of_int ) -;; + @@ -465,7 +503,7 @@ let max_value_per_compute_node t = else 0) |> List.map ~f:(fun (x,y) -> y) } -;; + @@ -514,7 +552,7 @@ let to_string p = Average.to_float ave |> Printf.sprintf "%16.10f" end -;; + @@ -568,13 +606,13 @@ let compress_files () = match p with | Property.Cpu | Property.Accep -> - of_raw_data ~locked:false p + of_raw_data ~locked:false ~range:(0.,100.) p |> merge_per_compute_node | Property.Wall -> - of_raw_data ~locked:false p + of_raw_data ~locked:false ~range:(0.,100.) p |> max_value_per_compute_node | _ -> - of_raw_data ~locked:false p + of_raw_data ~locked:false ~range:(0.,100.) p |> merge_per_compute_node_and_block_id in List.iter l.data ~f:(fun x -> @@ -587,7 +625,7 @@ let compress_files () = List.iter files ~f:Unix.remove ; Unix.rename ~src:(out_channel_dir^out_channel_name) ~dst:(dir_name^out_channel_name); Unix.rmdir out_channel_dir -;; + (** Autocovariance function (not weighted) *) @@ -615,7 +653,7 @@ let autocovariance { property ; data } = in Array.init ~f (Array.length data) |> Array.to_list -;; + (** Computes the first 4 centered cumulants (zero mean) *) @@ -666,7 +704,7 @@ let centered_cumulants { property ; data } = ( cum3 /. denom, cum4 /. denom -. 3. ) in [| ave ; var ; cum3 ; cum4 |] -;; + @@ -708,5 +746,5 @@ let histogram { property ; data } = in Array.mapi ~f:(fun i x -> (min +. (Float.of_int i)*.delta_x, x *. norm) ) result |> Array.to_list -;; +