From 0b51ce48d993e44c50d0e4d89a16e520cc4cd427 Mon Sep 17 00:00:00 2001
From: STEVAN Antoine <antoine.stevan@isae-supaero.fr>
Date: Wed, 29 May 2024 15:23:31 +0000
Subject: [PATCH] add `inbreeding inspect` to look at the cache
 (dragoon/komodo!125)

* 7b44a88 refactor constants from `inbreeding load`
* 64e446f move `remove-cache-prefix` to new `path.nu`
* 822b03f add `inbreeding inspect`
---
 bins/inbreeding/consts.nu  |  4 ++++
 bins/inbreeding/inspect.nu | 32 ++++++++++++++++++++++++++++++++
 bins/inbreeding/load.nu    | 17 +++++------------
 bins/inbreeding/mod.nu     |  1 +
 bins/inbreeding/path.nu    |  5 +++++
 5 files changed, 47 insertions(+), 12 deletions(-)
 create mode 100644 bins/inbreeding/inspect.nu
 create mode 100644 bins/inbreeding/path.nu

diff --git a/bins/inbreeding/consts.nu b/bins/inbreeding/consts.nu
index 177d568c..708ac7ab 100644
--- a/bins/inbreeding/consts.nu
+++ b/bins/inbreeding/consts.nu
@@ -1,2 +1,6 @@
 export const BIN = "./target/release/inbreeding"
 export const CACHE = ($nu.home-path | path join .cache komodo inbreeding)
+
+export const ARG_EXPERIMENT_FORMAT = "{seed}-{env}-{k}-{n}-{nb_bytes}"
+export const EXPERIMENT_FORMAT = "{timestamp}-{env}-{strategy}-{k}-{n}-{nb_bytes}"
+export const FULL_EXPERIMENT_FORMAT = $"{seed}(char path_sep)($EXPERIMENT_FORMAT)"
diff --git a/bins/inbreeding/inspect.nu b/bins/inbreeding/inspect.nu
new file mode 100644
index 00000000..8fe9d548
--- /dev/null
+++ b/bins/inbreeding/inspect.nu
@@ -0,0 +1,32 @@
+use consts.nu
+use path.nu [ "remove-cache-prefix" ]
+
+def get-seeds [] [ nothing -> list<string> ] {
+    $consts.CACHE | path join '*' | into glob | ls $in | get name | each { path split | last }
+}
+
+export def main [seed: int@get-seeds]: [
+    nothing -> table<
+        seed: string,
+        timestamp: string,
+        env: string,
+        strategy: string,
+        k: string,
+        n: string,
+        nb_bytes: string,
+        m: int,
+    >
+] {
+    $consts.CACHE
+        | path join ($seed | into string) '*'
+        | into glob
+        | ls $in
+        | insert m { ls $in.name | length }
+        | select name m
+        | update name {
+            remove-cache-prefix
+                | parse $consts.FULL_EXPERIMENT_FORMAT
+        }
+        | flatten --all name
+}
+
diff --git a/bins/inbreeding/load.nu b/bins/inbreeding/load.nu
index 9537b96b..3c2c5cd7 100644
--- a/bins/inbreeding/load.nu
+++ b/bins/inbreeding/load.nu
@@ -1,14 +1,7 @@
 use consts.nu
+use path.nu [ "remove-cache-prefix" ]
 use ../../.nushell error "error throw"
 
-const ARG_EXPERIMENT_FORMAT = "{seed}-{env}-{k}-{n}-{nb_bytes}"
-const EXPERIMENT_FORMAT = "{timestamp}-{env}-{strategy}-{k}-{n}-{nb_bytes}"
-const FULL_EXPERIMENT_FORMAT = $"{seed}(char path_sep)($EXPERIMENT_FORMAT)"
-
-def remove-cache-prefix []: path -> string {
-    str replace $"($consts.CACHE)(char path_sep)" ''
-}
-
 # return experiment names following `$ARG_EXPERIMENT_FORMAT`
 def get-experiments []: nothing -> list<string> {
     $consts.CACHE
@@ -17,7 +10,7 @@ def get-experiments []: nothing -> list<string> {
         | ls $in
         | get name
         | each { remove-cache-prefix }
-        | parse $FULL_EXPERIMENT_FORMAT
+        | parse $consts.FULL_EXPERIMENT_FORMAT
         | reject timestamp strategy
         | each { values | str join '-' }
         | uniq
@@ -26,11 +19,11 @@ def get-experiments []: nothing -> list<string> {
 export def main [
     experiment: string@get-experiments,
 ]: nothing -> table<strategy: string, diversity: table<x: int, y: float, e: float>> {
-    let exp = $experiment | parse $ARG_EXPERIMENT_FORMAT | into record
+    let exp = $experiment | parse $consts.ARG_EXPERIMENT_FORMAT | into record
     if $exp == {} {
         error throw {
             err: "invalid experiment",
-            label: $"should have format '($ARG_EXPERIMENT_FORMAT)', found ($experiment)",
+            label: $"should have format '($consts.ARG_EXPERIMENT_FORMAT)', found ($experiment)",
             span: (metadata $experiment).span,
         }
     }
@@ -54,7 +47,7 @@ export def main [
 
     $experiment_files
         | select name
-        | insert . { get name | remove-cache-prefix | parse $EXPERIMENT_FORMAT }
+        | insert . { get name | remove-cache-prefix | parse $consts.EXPERIMENT_FORMAT }
         | flatten --all
         | insert diversity {
             ls $in.name
diff --git a/bins/inbreeding/mod.nu b/bins/inbreeding/mod.nu
index 6917cad3..f3bdbd9d 100644
--- a/bins/inbreeding/mod.nu
+++ b/bins/inbreeding/mod.nu
@@ -1,4 +1,5 @@
 export use build.nu
 export use run.nu
+export use inspect.nu
 export use load.nu
 export use plot.nu
diff --git a/bins/inbreeding/path.nu b/bins/inbreeding/path.nu
new file mode 100644
index 00000000..4614db12
--- /dev/null
+++ b/bins/inbreeding/path.nu
@@ -0,0 +1,5 @@
+use consts.nu
+
+export def remove-cache-prefix []: path -> string {
+    str replace $"($consts.CACHE)(char path_sep)" ''
+}
-- 
GitLab