crate_activity/
cli.rs

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
crate::ix!();

/// Crate Activity Analyzer
#[derive(Getters,StructOpt, Debug)]
#[structopt(name = "crate-activity")]
pub struct CrateActivityCli {

    /// Enable all analyses: correlations, PCA, hierarchical clustering, network analysis, etc.
    #[structopt(long, help = "Enable all analyses at once")]
    #[getset(get = "pub")]
    all: bool,

    /// Toggle to enable or disable correlation analysis
    #[structopt(long, short = "c", help = "Display correlation analysis")]
    #[getset(get = "pub")]
    show_correlations: bool,

    /// Toggle to enable or disable PCA analysis
    #[structopt(long, short = "p", help = "Perform PCA analysis")]
    #[getset(get = "pub")]
    perform_pca: bool,

    /// Toggle to enable hierarchical clustering
    #[structopt(long, short = "h", help = "Perform hierarchical clustering")]
    #[getset(get = "pub")]
    perform_hierarchical_clustering: bool,

    /// Toggle to enable correlation network analysis
    #[structopt(long, short = "n", help = "Perform correlation network analysis")]
    #[getset(get = "pub")]
    correlation_network: bool,

    /// Threshold for including edges in the correlation network graph
    #[structopt(long, default_value = "0.7", help = "Correlation threshold for network edges")]
    #[getset(get = "pub")]
    network_threshold: f64,

    /// Use Girvan–Newman algorithm to find a specified number of communities
    #[structopt(long, short = "g", help = "Target number of communities for Girvan–Newman")]
    #[getset(get = "pub")]
    girvan_newman: Option<usize>,

    /// Compute betweenness centrality for nodes (and edges) and display top nodes
    #[structopt(long, short = "b", help = "Compute betweenness centrality and display top nodes")]
    #[getset(get = "pub")]
    compute_betweenness: bool,

    /// Print a summary of the network graph
    #[structopt(long, short = "s", help = "Print a summary of the network graph")]
    #[getset(get = "pub")]
    print_summary: bool,

    /// Toggle to enable time-lagged correlation analysis
    #[structopt(long, short = "t", help = "Compute time-lagged correlations")]
    #[getset(get = "pub")]
    time_lag_correlations: bool,

    /// Maximum lag in days for time-lagged correlations
    #[structopt(long, default_value = "7", help = "Maximum lag (in days) to consider for time-lag correlations")]
    #[getset(get = "pub")]
    max_lag: i32,

    /// Z-score threshold for outlier detection (MAD-based)
    #[structopt(long, default_value = "4.0", help = "Z-score threshold for outlier detection")]
    #[getset(get = "pub")]
    outlier_z_threshold: f64,

    /// Downweight outliers instead of removing them
    #[structopt(long, help = "Downweight outliers instead of removing them")]
    #[getset(get = "pub")]
    downweight_outliers: bool,

    /// Factor by which to downweight outliers if --downweight-outliers is used
    #[structopt(long, default_value = "0.1", help = "Downweight factor for outliers")]
    #[getset(get = "pub")]
    outlier_weight: f64,

    /// Disable outlier handling altogether
    #[structopt(long, help = "Disable outlier detection and handling")]
    disable_outlier_handling: bool,
}

impl CrateActivityCli {

    pub fn read_command_line() -> Self {
        let mut cli = CrateActivityCli::from_args();
        cli.apply_all_flag();
        cli
    }

    pub fn disable_outlier_handling(&self) -> bool {

        #[cfg(test)]
        let disable_outliers_override = true; // Force no outliers in test

        #[cfg(not(test))]
        let disable_outliers_override = false;

        let disable_outliers = self.disable_outlier_handling || disable_outliers_override;

        disable_outliers
    }

    /// Apply the `--all` flag overrides if set.
    pub fn apply_all_flag(&mut self) {
        if self.all {
            self.show_correlations = true;
            self.perform_pca = true;
            self.perform_hierarchical_clustering = true;
            self.correlation_network = true;
            self.compute_betweenness = true;
            self.print_summary = true;
            self.time_lag_correlations = true;
            // You might leave outlier handling as is or also enable/disable it if desired.
        }
    }
}