Skip to main content

hypergraph_rs/stats/
edgestats.rs

1//! Edge statistics (XGI `stats/edgestats.py`).
2
3use indexmap::IndexMap;
4use serde_json::Value;
5
6use super::Stat;
7use crate::Hypergraph;
8
9/// Count members whose node-degree equals `degree` (helper for degree-filtered order/size).
10fn members_with_degree(h: &Hypergraph, edge_id: &str, degree: usize) -> usize {
11    h.members(edge_id)
12        .unwrap_or_default()
13        .iter()
14        .filter(|n| h.memberships(n).map(|m| m.len()) == Some(degree))
15        .count()
16}
17
18/// Edge order: `size - 1`, optionally counting only members of a given node-degree.
19/// XGI `edgestats.order`.
20pub fn edge_order(h: &Hypergraph, degree: Option<usize>) -> Stat<usize> {
21    let mut out: IndexMap<String, usize> = IndexMap::new();
22    for eid in h.edge_ids() {
23        let val = match degree {
24            None => h
25                .members(&eid)
26                .map(|m| m.len())
27                .unwrap_or(0)
28                .saturating_sub(1),
29            Some(d) => members_with_degree(h, &eid, d).saturating_sub(1),
30        };
31        out.insert(eid, val);
32    }
33    Stat::new(out)
34}
35
36/// Edge size: number of members. XGI `edgestats.size`.
37pub fn edge_size(h: &Hypergraph, degree: Option<usize>) -> Stat<usize> {
38    let mut out: IndexMap<String, usize> = IndexMap::new();
39    for eid in h.edge_ids() {
40        let val = match degree {
41            None => h.members(&eid).map(|m| m.len()).unwrap_or(0),
42            Some(d) => members_with_degree(h, &eid, d),
43        };
44        out.insert(eid, val);
45    }
46    Stat::new(out)
47}
48
49/// Edge attributes: one attr (Null-imputed) or the whole object. XGI `edgestats.attrs`.
50pub fn edge_attrs(h: &Hypergraph, attr: Option<&str>) -> Stat<Value> {
51    let mut out: IndexMap<String, Value> = IndexMap::new();
52    for eid in h.edge_ids() {
53        let all = h.edge_attrs(&eid).cloned().unwrap_or(Value::Null);
54        let v = match attr {
55            None => all,
56            Some(a) => all.get(a).cloned().unwrap_or(Value::Null),
57        };
58        out.insert(eid, v);
59    }
60    Stat::new(out)
61}