smlm_locs/io/
parse_method.rs1use super::{CsvSettings};
2use crate::constants::{THUNDER_STORM};
3
4#[derive(Debug, Default, Clone, PartialEq)]
5pub enum ParseMethod
6{
7 #[default]
8 ThunderStorm,
9 Csv(CsvSettings),
10}
11
12impl TryFrom<&str> for ParseMethod
13{
14 type Error = String;
15 fn try_from(description: &str) -> Result<Self, Self::Error>
16 {
17 parse_method(description)
18 }
19}
20
21fn parse_method(description: &str) -> Result<ParseMethod, String>
22{
23 match description
24 {
25 THUNDER_STORM => Ok(ParseMethod::ThunderStorm),
26 desc @ _ =>
27 {
28 let splits : Vec<&str> = desc.split("=").collect();
29 if splits.len() < 2
30 {
31 return Err(format!("Cannot create parse method from {description}"))
32 }
33 if splits[0] != "csv"
34 {
35 Err(format!("{} not recognised as parse method identifier", splits[0]))
36 }
37 else
38 {
39 let settings = parse_settings(splits[1])?;
40 Ok(ParseMethod::Csv(settings))
41 }
42 },
43 }
44}
45
46fn parse_optional_param(param: &str) -> Result<Option<u8>, String>
47{
48 if param.is_empty()
49 {
50 return Ok(None)
51 }
52 match param
53 {
54 "-1" => Ok(None),
55 desc @ _ => desc.parse::<u8>().map(Some).map_err(|e| e.to_string())
56 }
57}
58
59fn get_optional_param(index: usize, splits: &[&str]) -> Result<Option<u8>, String>
60{
61 match splits.get(index)
62 {
63 None => Ok(None),
64 Some(desc) => parse_optional_param(desc)
65 }
66}
67
68fn parse_settings(description: &str) -> Result<CsvSettings, String>
69{
70 let splits : Vec<&str> = description.split(";").collect();
71 let n_required = 4;
73 if splits.len() < n_required || splits.iter().take(n_required).any(|s| s.is_empty())
74 {
75 return Err(format!("Cannot parse {description} into csv settings"))
76 }
77 let n_headers = splits[0].parse::<usize>().map_err(|e| e.to_string())?;
78 let delim = splits[1].chars().next().unwrap(); let x_pos = splits[2].parse::<u8>().map_err(|e| e.to_string())?;
80 let y_pos = splits[3].parse::<u8>().map_err(|e| e.to_string())?;
81
82 let psf_sigma_pos = get_optional_param(4, &splits)?;
83 let uncertainty_pos = get_optional_param(5, &splits)?;
84 let frame_number_pos = get_optional_param(6, &splits)?;
85
86 Ok(CsvSettings::default().with_n_headers(n_headers)
87 .with_delimeter(delim)
88 .with_x_pos(x_pos)
89 .with_y_pos(y_pos)
90 .with_sigma_pos(psf_sigma_pos)
91 .with_uncertainty_pos(uncertainty_pos)
92 .with_frame_number_pos(frame_number_pos))
93}
94
95#[cfg(test)]
96mod tests
97{
98 use super::*;
99
100 #[test]
101 fn can_parse_thunderstorm()
102 {
103 assert_eq!(ParseMethod::try_from("ts").unwrap(), ParseMethod::ThunderStorm)
104 }
105
106 #[test]
107 fn error_on_unknown()
108 {
109 assert_eq!(ParseMethod::try_from("blah").unwrap_err(), "Cannot create parse method from blah")
110 }
111
112 #[test]
113 fn error_on_unknown_descriptions()
114 {
115 assert_eq!(ParseMethod::try_from("blah=").unwrap_err(), "blah not recognised as parse method identifier")
116 }
117
118 #[test]
119 fn bad_csv_description()
120 {
121 assert_eq!(ParseMethod::try_from("csv=stuff;and;things").unwrap_err(), "Cannot parse stuff;and;things into csv settings")
122 }
123
124 #[test]
125 fn good_minimal_csv_description()
126 {
127 let settings = CsvSettings::default().with_n_headers(1)
128 .with_delimeter(',')
129 .with_x_pos(1)
130 .with_y_pos(2);
131 assert_eq!(ParseMethod::try_from("csv=1;,;1;2").unwrap(), ParseMethod::Csv(settings))
132 }
133
134 #[test]
135 fn good_minimal_csv_for_sigma()
136 {
137 let settings = CsvSettings::default().with_n_headers(1)
138 .with_delimeter(',')
139 .with_x_pos(1)
140 .with_y_pos(2)
141 .with_sigma_pos(Some(3));
142 assert_eq!(ParseMethod::try_from("csv=1;,;1;2;3").unwrap(), ParseMethod::Csv(settings))
143 }
144
145 #[test]
146 fn good_minimal_csv_for_uncertainty()
147 {
148 let settings = CsvSettings::default().with_n_headers(1)
149 .with_delimeter(',')
150 .with_x_pos(1)
151 .with_y_pos(2)
152 .with_uncertainty_pos(Some(3));
153 assert_eq!(ParseMethod::try_from("csv=1;,;1;2;;3").unwrap(), ParseMethod::Csv(settings))
154 }
155
156 #[test]
157 fn comma_delimeted_csv_description()
158 {
159 assert_eq!(ParseMethod::try_from("csv=1;;;1;2;3").unwrap_err(), "Cannot parse 1;;;1;2;3 into csv settings")
160 }
161
162 #[test]
163 fn good_csv_description_with_specified_no_sigma_value_and_implied_no_uncertainty()
164 {
165 let settings = CsvSettings::default().with_n_headers(1)
166 .with_delimeter(',')
167 .with_x_pos(1)
168 .with_y_pos(2)
169 .with_sigma_pos(None)
170 .with_uncertainty_pos(None);
171 assert_eq!(ParseMethod::try_from("csv=1;,;1;2;-1").unwrap(), ParseMethod::Csv(settings))
172 }
173
174 #[test]
175 fn good_csv_description_with_implied_no_sigma_value_and_specified_no_uncertainty()
176 {
177 let settings = CsvSettings::default().with_n_headers(1)
178 .with_delimeter(',')
179 .with_x_pos(1)
180 .with_y_pos(2)
181 .with_sigma_pos(None)
182 .with_uncertainty_pos(None);
183 assert_eq!(ParseMethod::try_from("csv=1;,;1;2;;-1").unwrap(), ParseMethod::Csv(settings))
184 }
185
186 #[test]
187 fn good_csv_description_with_specified_no_sigma_and_implied_no_uncertainty_value()
188 {
189 let settings = CsvSettings::default().with_n_headers(1)
190 .with_delimeter(',')
191 .with_x_pos(1)
192 .with_y_pos(2)
193 .with_sigma_pos(None)
194 .with_uncertainty_pos(None);
195 assert_eq!(ParseMethod::try_from("csv=1;,;1;2;-1;").unwrap(), ParseMethod::Csv(settings))
196 }
197
198 #[test]
199 fn good_csv_description_with_specified_no_sigma_or_uncertainty_value()
200 {
201 let settings = CsvSettings::default().with_n_headers(1)
202 .with_delimeter(',')
203 .with_x_pos(1)
204 .with_y_pos(2)
205 .with_sigma_pos(None)
206 .with_uncertainty_pos(None);
207 assert_eq!(ParseMethod::try_from("csv=1;,;1;2;-1;-1").unwrap(), ParseMethod::Csv(settings))
208 }
209
210 #[test]
211 fn good_csv_description_with_sigma_but_no_uncertainty()
212 {
213 let settings = CsvSettings::default().with_n_headers(1)
214 .with_delimeter(',')
215 .with_x_pos(1)
216 .with_y_pos(2)
217 .with_sigma_pos(Some(4))
218 .with_uncertainty_pos(None);
219 assert_eq!(ParseMethod::try_from("csv=1;,;1;2;4;-1").unwrap(), ParseMethod::Csv(settings))
220 }
221
222 #[test]
223 fn good_csv_description_with_no_sigma_and_specified_uncertainty()
224 {
225 let settings = CsvSettings::default().with_n_headers(1)
226 .with_delimeter(',')
227 .with_x_pos(1)
228 .with_y_pos(2)
229 .with_sigma_pos(None)
230 .with_uncertainty_pos(Some(4));
231 assert_eq!(ParseMethod::try_from("csv=1;,;1;2;;4").unwrap(), ParseMethod::Csv(settings))
232 }
233
234 #[test]
235 fn good_csv_description_with_frame_number()
236 {
237 let settings = CsvSettings::default().with_n_headers(1)
238 .with_delimeter(',')
239 .with_x_pos(1)
240 .with_y_pos(2)
241 .with_sigma_pos(None)
242 .with_uncertainty_pos(None)
243 .with_frame_number_pos(Some(5));
244 assert_eq!(ParseMethod::try_from("csv=1;,;1;2;;;5").unwrap(), ParseMethod::Csv(settings))
245 }
246}