diff options
author | Franciszek Malinka <franciszek.malinka@gmail.com> | 2023-10-25 03:41:24 +0200 |
---|---|---|
committer | Franciszek Malinka <franciszek.malinka@gmail.com> | 2023-10-25 03:41:24 +0200 |
commit | dbe67427c852d68b7f87a2761d122d5f2d8276bb (patch) | |
tree | cb33801d28d92036185de50fdc4387ec05bf7685 | |
parent | b5d6281bb3d70c431b1e6bb6e1a2843fba8f4244 (diff) |
c
-rw-r--r-- | algorithms/dune | 2 | ||||
-rw-r--r-- | app/spectral_classification/src/classification.ml | 7 | ||||
-rw-r--r-- | app/spectral_classification/src/classification.mli | 5 | ||||
-rw-r--r-- | app/spectral_classification/src/classification_rules.ml | 12 | ||||
-rw-r--r-- | app/spectral_classification/src/classification_rules.mli | 10 | ||||
-rw-r--r-- | app/spectral_classification/src/dune | 6 | ||||
-rw-r--r-- | app/spectral_classification/src/main.ml | 1 | ||||
-rw-r--r-- | app/spectral_classification/src/main.mli | 3 | ||||
-rw-r--r-- | app/spectral_classification/src/parser.ml | 34 | ||||
-rw-r--r-- | app/spectral_classification/src/parser.mli | 4 | ||||
-rw-r--r-- | app/spectral_classification/src/spectral_classification.ml | 1 | ||||
-rw-r--r-- | app/spectral_classification/test/classification_rules.txt | 4 | ||||
-rw-r--r-- | app/spectral_classification/test/dune | 15 | ||||
-rw-r--r-- | app/spectral_classification/test/image_expert.txt | 1 | ||||
-rw-r--r-- | app/spectral_classification/test/test.ml | 16 | ||||
-rw-r--r-- | dune | 3 |
16 files changed, 122 insertions, 2 deletions
diff --git a/algorithms/dune b/algorithms/dune index 90f35b8..069a29e 100644 --- a/algorithms/dune +++ b/algorithms/dune @@ -1,6 +1,6 @@ (library (inline_tests) (name algorithms_lib) - (libraries core owl progress genetics_lib utils_lib structures_lib) + (libraries core owl genetics_lib utils_lib structures_lib) (preprocess (pps ppx_inline_test ppx_jane))) diff --git a/app/spectral_classification/src/classification.ml b/app/spectral_classification/src/classification.ml new file mode 100644 index 0000000..e401e57 --- /dev/null +++ b/app/spectral_classification/src/classification.ml @@ -0,0 +1,7 @@ +open! Core +open Owl + +type t = Mat.mat + +let create values = Mat.of_array values 1 (Array.length values) +let sexp_of_t t = Mat.to_array t |> Array.sexp_of_t Float.sexp_of_t diff --git a/app/spectral_classification/src/classification.mli b/app/spectral_classification/src/classification.mli new file mode 100644 index 0000000..02a60b2 --- /dev/null +++ b/app/spectral_classification/src/classification.mli @@ -0,0 +1,5 @@ +open! Core + +type t [@@deriving sexp_of] + +val create : float array -> t diff --git a/app/spectral_classification/src/classification_rules.ml b/app/spectral_classification/src/classification_rules.ml new file mode 100644 index 0000000..aebf5a8 --- /dev/null +++ b/app/spectral_classification/src/classification_rules.ml @@ -0,0 +1,12 @@ +open! Core +open Owl + +type t = Mat.mat + +let create values ~length ~number = Mat.of_array values number length + +let sexp_of_t t = + let n, _ = Mat.shape t in + let l = List.init n ~f:(fun i -> Mat.row t i |> Mat.to_array |> Array.to_list) in + List.sexp_of_t (List.sexp_of_t Float.sexp_of_t) l +;; diff --git a/app/spectral_classification/src/classification_rules.mli b/app/spectral_classification/src/classification_rules.mli new file mode 100644 index 0000000..9b11f0d --- /dev/null +++ b/app/spectral_classification/src/classification_rules.mli @@ -0,0 +1,10 @@ +open! Core +open Genetics_lib + +type t [@@deriving sexp_of] + +(** This might throw if this isn't a nonempty matrix (i.e. each array have the same size. )*) +val create : float array -> length:int -> number:int -> t + +(** Throws if chromosome length is different than the number of classifiers. *) +val classify : t -> Chromosome.t -> Classification.t diff --git a/app/spectral_classification/src/dune b/app/spectral_classification/src/dune new file mode 100644 index 0000000..3a2958e --- /dev/null +++ b/app/spectral_classification/src/dune @@ -0,0 +1,6 @@ +(library + (inline_tests) + (name spectral_classification_lib) + (libraries core owl owl-plplot curl utils_lib structures_lib algorithms_lib) + (preprocess + (pps ppx_inline_test ppx_jane))) diff --git a/app/spectral_classification/src/main.ml b/app/spectral_classification/src/main.ml new file mode 100644 index 0000000..3ad2609 --- /dev/null +++ b/app/spectral_classification/src/main.ml @@ -0,0 +1 @@ +open! Core diff --git a/app/spectral_classification/src/main.mli b/app/spectral_classification/src/main.mli new file mode 100644 index 0000000..ac8385a --- /dev/null +++ b/app/spectral_classification/src/main.mli @@ -0,0 +1,3 @@ +open! Core + +(* val command : Command.t *) diff --git a/app/spectral_classification/src/parser.ml b/app/spectral_classification/src/parser.ml new file mode 100644 index 0000000..c24c8df --- /dev/null +++ b/app/spectral_classification/src/parser.ml @@ -0,0 +1,34 @@ +open! Core + +let parse_line line = + let is_good_str str = + if String.is_empty str + then false + else if String.length str = 1 && Char.is_whitespace (String.get str 0) + then false + else true + in + String.split_lines line + |> String.concat + |> String.split ~on:' ' + |> List.filter ~f:is_good_str + |> List.map ~f:Float.of_string +;; + +let%expect_test "parse_line" = + print_s [%sexp (parse_line " 0. 124 0.231e32 \n 123. \n\n\t " : float list)]; + [%expect{| (0 124 2.31E+31 123) |}] +;; + +let load_classification_rules filename = + let lines = In_channel.read_lines filename in + let number = List.length lines + and values = List.concat_map lines ~f:parse_line in + let length = List.length values / number in + Classification_rules.create (Array.of_list values) ~length ~number +;; + +let load_classification filename = + let line = In_channel.read_all filename in + parse_line line |> Array.of_list |> Classification.create +;; diff --git a/app/spectral_classification/src/parser.mli b/app/spectral_classification/src/parser.mli new file mode 100644 index 0000000..18538f4 --- /dev/null +++ b/app/spectral_classification/src/parser.mli @@ -0,0 +1,4 @@ +open! Core + +val load_classification_rules : Filename.t -> Classification_rules.t +val load_classification : Filename.t -> Classification.t diff --git a/app/spectral_classification/src/spectral_classification.ml b/app/spectral_classification/src/spectral_classification.ml new file mode 100644 index 0000000..a5a59ad --- /dev/null +++ b/app/spectral_classification/src/spectral_classification.ml @@ -0,0 +1 @@ +module Main = Main diff --git a/app/spectral_classification/test/classification_rules.txt b/app/spectral_classification/test/classification_rules.txt new file mode 100644 index 0000000..f817a48 --- /dev/null +++ b/app/spectral_classification/test/classification_rules.txt @@ -0,0 +1,4 @@ + 1.0000000e+000 2.0000000e+000 3.0000000e+000 + 1.0000000e+000 1.0000000e+000 1.0000000e+0000 + 1.0124300e+000 1.0300000e+000 4.0000000e+000 + 1.0052310e+000 7.0000000e+000 5.0000000e+000 diff --git a/app/spectral_classification/test/dune b/app/spectral_classification/test/dune new file mode 100644 index 0000000..f3ad6ad --- /dev/null +++ b/app/spectral_classification/test/dune @@ -0,0 +1,15 @@ +(library + (inline_tests + (deps classification_rules.txt image_expert.txt)) + (name spectral_classification_test) + (libraries + core + owl + owl-plplot + curl + spectral_classification_lib + utils_lib + structures_lib + algorithms_lib) + (preprocess + (pps ppx_inline_test ppx_jane))) diff --git a/app/spectral_classification/test/image_expert.txt b/app/spectral_classification/test/image_expert.txt new file mode 100644 index 0000000..05b17b8 --- /dev/null +++ b/app/spectral_classification/test/image_expert.txt @@ -0,0 +1 @@ + 7.0000000e+000 4.0000000e+000 4.0000000e+000 4.0000000e+000 diff --git a/app/spectral_classification/test/test.ml b/app/spectral_classification/test/test.ml new file mode 100644 index 0000000..ef83c9d --- /dev/null +++ b/app/spectral_classification/test/test.ml @@ -0,0 +1,16 @@ +open! Core +open Spectral_classification_lib + +let%expect_test "load_classification_rules" = + let classification_rules = + Parser.load_classification_rules "classification_rules.txt" + in + print_s [%sexp (classification_rules : Classification_rules.t)]; + [%expect {| ((1 2 3) (1 1 1) (1.01243 1.03 4) (1.005231 7 5)) |}] +;; + +let%expect_test "load_classification" = + let classification = Parser.load_classification "image_expert.txt" in + print_s [%sexp (classification : Classification.t)]; + [%expect {| (7 4 4 4) |}] +;; @@ -1,5 +1,6 @@ (env - (dev) + (dev + (ocamlopt_flags (:standard))) (release (ocamlopt_flags (:standard -O3)))) |