Skip to content

Commit 1f08fff

Browse files
committed
Refactor multicolor.rs into multiple modules
1 parent bee2ada commit 1f08fff

12 files changed

+729
-634
lines changed

src/multicolor.rs

Lines changed: 0 additions & 634 deletions
This file was deleted.
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
use crate::data::MultiColorTimeSeries;
2+
use crate::error::MultiColorEvaluatorError;
3+
use crate::evaluator::{
4+
EvaluatorInfo, EvaluatorInfoTrait, FeatureEvaluator, FeatureNamesDescriptionsTrait,
5+
};
6+
use crate::features::Median;
7+
use crate::float_trait::Float;
8+
use crate::multicolor::multicolor_evaluator::*;
9+
use crate::multicolor::{PassbandSet, PassbandTrait};
10+
11+
pub use lazy_static::lazy_static;
12+
pub use schemars::JsonSchema;
13+
pub use serde::{Deserialize, Serialize};
14+
use std::collections::BTreeSet;
15+
use std::fmt::Debug;
16+
17+
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
18+
#[serde(bound(deserialize = "P: PassbandTrait + Deserialize<'de>"))]
19+
pub struct ColorOfMedian<P>
20+
where
21+
P: Ord,
22+
{
23+
passband_set: PassbandSet<P>,
24+
passbands: [P; 2],
25+
median: Median,
26+
name: String,
27+
description: String,
28+
}
29+
30+
impl<P> ColorOfMedian<P>
31+
where
32+
P: PassbandTrait,
33+
{
34+
pub fn new(passbands: [P; 2]) -> Self {
35+
let set: BTreeSet<_> = passbands.clone().into();
36+
Self {
37+
passband_set: set.into(),
38+
name: format!(
39+
"color_median_{}_{}",
40+
passbands[0].name(),
41+
passbands[1].name()
42+
),
43+
description: format!(
44+
"difference of median magnitudes {}-{}",
45+
passbands[0].name(),
46+
passbands[1].name()
47+
),
48+
passbands,
49+
median: Median {},
50+
}
51+
}
52+
}
53+
54+
lazy_info!(
55+
COLOR_MEDIAN_INFO,
56+
size: 1,
57+
min_ts_length: 1,
58+
t_required: false,
59+
m_required: true,
60+
w_required: false,
61+
sorting_required: false,
62+
variability_required: false,
63+
);
64+
65+
impl<P> EvaluatorInfoTrait for ColorOfMedian<P>
66+
where
67+
P: Ord,
68+
{
69+
fn get_info(&self) -> &EvaluatorInfo {
70+
&COLOR_MEDIAN_INFO
71+
}
72+
}
73+
74+
impl<P> FeatureNamesDescriptionsTrait for ColorOfMedian<P>
75+
where
76+
P: Ord,
77+
{
78+
fn get_names(&self) -> Vec<&str> {
79+
vec![self.name.as_str()]
80+
}
81+
82+
fn get_descriptions(&self) -> Vec<&str> {
83+
vec![self.description.as_str()]
84+
}
85+
}
86+
87+
impl<P> MultiColorPassbandSetTrait<P> for ColorOfMedian<P>
88+
where
89+
P: PassbandTrait,
90+
{
91+
fn get_passband_set(&self) -> &PassbandSet<P> {
92+
&self.passband_set
93+
}
94+
}
95+
96+
impl<P, T> MultiColorEvaluator<P, T> for ColorOfMedian<P>
97+
where
98+
P: PassbandTrait,
99+
T: Float,
100+
{
101+
fn eval_multicolor(
102+
&self,
103+
mcts: &mut MultiColorTimeSeries<P, T>,
104+
) -> Result<Vec<T>, MultiColorEvaluatorError> {
105+
self.check_mcts_passabands(mcts)?;
106+
let mut medians = [T::zero(); 2];
107+
for (median, passband) in medians.iter_mut().zip(self.passbands.iter()) {
108+
*median = self
109+
.median
110+
.eval(mcts.get_mut(passband).expect(
111+
"we checked all needed passbands are in mcts, but we still cannot find one",
112+
))
113+
.map_err(|error| MultiColorEvaluatorError::MonochromeEvaluatorError {
114+
passband: passband.name().into(),
115+
error,
116+
})?[0]
117+
}
118+
Ok(vec![medians[0] - medians[1]])
119+
}
120+
}

src/multicolor/features/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mod color_of_median;
2+
pub use color_of_median::ColorOfMedian;

src/multicolor/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
mod features;
2+
3+
mod monochrome_feature;
4+
pub use monochrome_feature::MonochromeFeature;
5+
6+
mod multicolor_evaluator;
7+
pub use multicolor_evaluator::{MultiColorEvaluator, MultiColorPassbandSetTrait, PassbandSet};
8+
9+
mod multicolor_extractor;
10+
pub use multicolor_extractor::MultiColorExtractor;
11+
12+
mod multicolor_feature;
13+
pub use multicolor_feature::MultiColorFeature;
14+
15+
mod passband;
16+
pub use passband::*;
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
use crate::data::MultiColorTimeSeries;
2+
use crate::error::MultiColorEvaluatorError;
3+
use crate::evaluator::{
4+
EvaluatorInfo, EvaluatorInfoTrait, EvaluatorProperties, FeatureEvaluator,
5+
FeatureNamesDescriptionsTrait,
6+
};
7+
use crate::float_trait::Float;
8+
use crate::multicolor::multicolor_evaluator::*;
9+
10+
use itertools::Itertools;
11+
pub use lazy_static::lazy_static;
12+
pub use schemars::JsonSchema;
13+
pub use serde::{Deserialize, Serialize};
14+
use std::collections::BTreeSet;
15+
use std::fmt::Debug;
16+
use std::marker::PhantomData;
17+
18+
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
19+
#[serde(bound(
20+
deserialize = "P: PassbandTrait + Deserialize<'de>, T: Float, F: FeatureEvaluator<T>"
21+
))]
22+
pub struct MonochromeFeature<P, T, F>
23+
where
24+
P: Ord,
25+
{
26+
feature: F,
27+
passband_set: PassbandSet<P>,
28+
properties: Box<EvaluatorProperties>,
29+
phantom: PhantomData<T>,
30+
}
31+
32+
impl<P, T, F> MonochromeFeature<P, T, F>
33+
where
34+
P: PassbandTrait,
35+
T: Float,
36+
F: FeatureEvaluator<T>,
37+
{
38+
pub fn new(feature: F, passband_set: BTreeSet<P>) -> Self {
39+
let names = passband_set
40+
.iter()
41+
.cartesian_product(feature.get_names())
42+
.map(|(passband, name)| format!("{}_{}", name, passband.name()))
43+
.collect();
44+
let descriptions = passband_set
45+
.iter()
46+
.cartesian_product(feature.get_descriptions())
47+
.map(|(passband, description)| format!("{}, passband {}", description, passband.name()))
48+
.collect();
49+
let info = {
50+
let mut info = feature.get_info().clone();
51+
info.size *= passband_set.len();
52+
info
53+
};
54+
Self {
55+
properties: EvaluatorProperties {
56+
info,
57+
names,
58+
descriptions,
59+
}
60+
.into(),
61+
feature,
62+
passband_set: passband_set.into(),
63+
phantom: PhantomData,
64+
}
65+
}
66+
}
67+
68+
impl<P, T, F> FeatureNamesDescriptionsTrait for MonochromeFeature<P, T, F>
69+
where
70+
P: Ord,
71+
{
72+
fn get_names(&self) -> Vec<&str> {
73+
self.properties.names.iter().map(String::as_str).collect()
74+
}
75+
76+
fn get_descriptions(&self) -> Vec<&str> {
77+
self.properties
78+
.descriptions
79+
.iter()
80+
.map(String::as_str)
81+
.collect()
82+
}
83+
}
84+
85+
impl<P, T, F> EvaluatorInfoTrait for MonochromeFeature<P, T, F>
86+
where
87+
P: Ord,
88+
{
89+
fn get_info(&self) -> &EvaluatorInfo {
90+
&self.properties.info
91+
}
92+
}
93+
94+
impl<P, T, F> MultiColorPassbandSetTrait<P> for MonochromeFeature<P, T, F>
95+
where
96+
P: PassbandTrait,
97+
{
98+
fn get_passband_set(&self) -> &PassbandSet<P> {
99+
&self.passband_set
100+
}
101+
}
102+
103+
impl<P, T, F> MultiColorEvaluator<P, T> for MonochromeFeature<P, T, F>
104+
where
105+
P: PassbandTrait,
106+
T: Float,
107+
F: FeatureEvaluator<T>,
108+
{
109+
fn eval_multicolor(
110+
&self,
111+
mcts: &mut MultiColorTimeSeries<P, T>,
112+
) -> Result<Vec<T>, MultiColorEvaluatorError> {
113+
self.check_mcts_passabands(mcts)?;
114+
match &self.passband_set {
115+
PassbandSet::FixedSet(set) => set
116+
.iter()
117+
.map(|passband| {
118+
self.feature.eval(mcts.get_mut(passband).expect(
119+
"we checked all needed passbands are in mcts, but we still cannot find one",
120+
)).map_err(|error| MultiColorEvaluatorError::MonochromeEvaluatorError {
121+
passband: passband.name().into(),
122+
error,
123+
})
124+
})
125+
.flatten_ok()
126+
.collect(),
127+
PassbandSet::AllAvailable => panic!("passband_set must be FixedSet variant here"),
128+
}
129+
}
130+
}

0 commit comments

Comments
 (0)