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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use std::{fmt, fmt::Write};

use necsim_core::{
    event::{DispersalEvent, SpeciationEvent},
    impl_finalise, impl_report,
    lineage::LineageInteraction,
    reporter::Reporter,
};
use necsim_core_bond::NonNegativeF64;

#[allow(clippy::module_name_repetitions)]
#[derive(Default)]
pub struct EventCounterReporter {
    last_parent_prior_time: Option<NonNegativeF64>,
    last_speciation_event: Option<SpeciationEvent>,
    last_dispersal_event: Option<DispersalEvent>,

    raw_total: usize,
    speciation: usize,
    out_dispersal: usize,
    self_dispersal: usize,
    out_coalescence: usize,
    self_coalescence: usize,
    late_dispersal_coalescence: usize,
    late_coalescence: usize,
}

impl fmt::Debug for EventCounterReporter {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt.debug_struct(stringify!(EventCounterReporter))
            .field("speciation", &self.speciation)
            .field("out_dispersal", &self.out_dispersal)
            .field("self_dispersal", &self.self_dispersal)
            .field("out_coalescence", &self.out_coalescence)
            .field("self_coalescence", &self.self_coalescence)
            .field(
                "late_dispersal",
                &(self.late_dispersal_coalescence - self.late_coalescence),
            )
            .field("late_coalescence", &self.late_coalescence)
            .finish_non_exhaustive()
    }
}

impl serde::Serialize for EventCounterReporter {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.serialize_unit()
    }
}

impl<'de> serde::Deserialize<'de> for EventCounterReporter {
    fn deserialize<D: serde::Deserializer<'de>>(_deserializer: D) -> Result<Self, D::Error> {
        Ok(Self::default())
    }
}

impl Reporter for EventCounterReporter {
    impl_report!(speciation(&mut self, speciation: Used) {
        self.raw_total += 1;

        if Some(speciation) == self.last_speciation_event.as_ref() {
            if Some(speciation.prior_time) != self.last_parent_prior_time {
                self.late_coalescence += 1;
            }
            self.last_parent_prior_time = Some(speciation.prior_time);

            return;
        }
        self.last_speciation_event = Some(speciation.clone());
        self.last_parent_prior_time = Some(speciation.prior_time);

        self.speciation += 1;
    });

    impl_report!(dispersal(&mut self, dispersal: Used) {
        self.raw_total += 1;

        if Some(dispersal) == self.last_dispersal_event.as_ref() {
            if Some(dispersal.prior_time) != self.last_parent_prior_time {
                self.late_coalescence += 1;
            }
            self.last_parent_prior_time = Some(dispersal.prior_time);

            return;
        }
        self.last_dispersal_event = Some(dispersal.clone());
        self.last_parent_prior_time = Some(dispersal.prior_time);

        let self_dispersal = dispersal.origin == dispersal.target;
        let coalescence = if dispersal.interaction == LineageInteraction::Maybe {
            self.late_dispersal_coalescence += 1;
            return
        } else { dispersal.interaction.is_coalescence() };

        match (self_dispersal, coalescence) {
            (true, true) => {
                self.self_coalescence += 1;
            },
            (true, false) => {
                self.self_dispersal += 1;
            },
            (false, true) => {
                self.out_coalescence += 1;
            },
            (false, false) => {
                self.out_dispersal += 1;
            },
        }
    });

    impl_report!(progress(&mut self, _progress: Ignored) {});

    impl_finalise!((self) {
        if self.last_speciation_event.is_none() && self.last_dispersal_event.is_none() {
            return;
        }

        let mut event_summary = String::new();

        let _ = writeln!(&mut event_summary, "Event Summary:");

        let _ = writeln!(
            &mut event_summary,
            " - Total #individuals:\n   {}",
            self.speciation + self.self_coalescence + self.out_coalescence + self.late_coalescence
        );
        let _ = writeln!(
            &mut event_summary,
            " - Total #events:\
            \n   - raw:\n     {}\
            \n   - deduplicated:\n     {}",
            self.raw_total,
            self.speciation
                + self.self_coalescence
                + self.out_coalescence
                + self.self_dispersal
                + self.out_dispersal
                + self.late_dispersal_coalescence
        );
        let _ = writeln!(
            &mut event_summary,
            " - Speciation:\
            \n    {}",
            self.speciation
        );
        let _ = writeln!(
            &mut event_summary,
            " - Dispersal:\
            \n   - same location, no coalescence:\n     {}\
            \n   - same location, with coalescence:\n     {}\
            \n   - new location, no coalescence:\n     {}\
            \n   - new location, with coalescence:\n     {}\
            \n   - detected late, no coalescence:\n     {}\
            \n   - detected late, with coalescence:\n     {}",
            self.self_dispersal,
            self.self_coalescence,
            self.out_dispersal,
            self.out_coalescence,
            self.late_dispersal_coalescence - self.late_coalescence,
            self.late_coalescence
        );

        log::info!("{}", event_summary);
    });
}