Skip to content

Commit fa142c7

Browse files
committed
complete revise the architecture
1 parent 546abf3 commit fa142c7

File tree

10 files changed

+82
-87
lines changed

10 files changed

+82
-87
lines changed

src/assumptions/assumption.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ fn _get_assumption_df(
1414
new_col_names: Option<&[&str]>,
1515
) -> PolarsResult<DataFrame> {
1616
// If new_col_names is provided, ensure it matches the length of col_names
17-
if let Some(ref new_names) = new_col_names {
17+
if let Some(new_names) = new_col_names {
1818
if col_names.len() != new_names.len() {
1919
return Err(PolarsError::ComputeError(
2020
"Length of col_names and new_col_names must match".into(),
@@ -51,8 +51,8 @@ fn _get_assumption_df(
5151
//---------------------------------------------------------------------------------------------------------
5252
// Mortality assumption: The schema is slightly different from other since it is based on gender
5353
pub fn get_mort_rate_df(mort_name: &str) -> PolarsResult<DataFrame> {
54-
let col1 = format!("{}_m", mort_name);
55-
let col2 = format!("{}_f", mort_name);
54+
let col1 = format!("{mort_name}_m");
55+
let col2 = format!("{mort_name}_f");
5656
let col_names = [col1.as_str(), col2.as_str()];
5757
let df = _get_assumption_df("mort_rate", &col_names, Some(&["mort_m", "mort_f"]))?;
5858
Ok(df)
@@ -104,7 +104,7 @@ mod tests {
104104
// Test reading data from the lapse_rate sheet
105105
let df = get_mort_rate_df("cso80");
106106

107-
println!("{:?}", df);
107+
println!("{df:?}");
108108

109109
assert!(true, "Assumption DataFrame should be created successfully");
110110
}

src/assumptions/assumption_scenario.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ mod tests {
131131
// Test reading data from the mort_rate sheet
132132
let df = _get_assumption_scenario_df("pricing");
133133

134-
println!("{:?}", df);
134+
println!("{df:?}");
135135

136136
assert!(true, "Mortality DataFrame should be created successfully");
137137
}
@@ -142,17 +142,17 @@ mod tests {
142142
let df = _get_assumption_scenario_df("pricing").unwrap();
143143
let vec = _dataframe_to_vec_of_tuples(&df).unwrap();
144144

145-
println!("{:?}", vec);
145+
println!("{vec:?}");
146146

147147
assert!(!vec.is_empty(), "Vec should not be empty");
148148
}
149149

150150
#[test]
151-
fn test_method_AssumptionScenario_new_by_name() {
151+
fn test_method_assumption_scenario_new_by_name() {
152152
// Test reading data from the lapse_rate sheet
153153
let df = AssumptionScenario::new_by_name("pricing");
154154

155-
println!("{:?}", df);
155+
println!("{df:?}");
156156

157157
assert!(true, "AssumptionScenario should be created successfully");
158158
}

src/assumptions/helpers.rs

Lines changed: 45 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,12 @@ fn _parse_cell_text(cell_str: &str) -> String {
2828
fn _get_header_rows(sheet: &spreadsheet_ods::Sheet) -> Vec<String> {
2929
let mut header: Vec<String> = Vec::new();
3030
let mut col_idx = 0;
31-
loop {
32-
if let Some(cell) = sheet.cell(0, col_idx) {
33-
let cell_str = format!("{:?}", cell);
34-
let name = _parse_cell_text(&cell_str);
35-
header.push(name);
36-
col_idx += 1;
37-
} else {
38-
break;
39-
}
31+
32+
while let Some(cell) = sheet.cell(0, col_idx) {
33+
let cell_str = format!("{cell:?}");
34+
let name = _parse_cell_text(&cell_str);
35+
header.push(name);
36+
col_idx += 1;
4037
}
4138

4239
header
@@ -57,7 +54,7 @@ pub fn get_indices_names_hashmap(
5754
let header = _get_header_rows(sheet);
5855

5956
// Always include the first column (index 0)
60-
if let Some(first_col_name) = header.get(0) {
57+
if let Some(first_col_name) = header.first() {
6158
indices.insert(0, (first_col_name.clone(), first_col_name.clone()));
6259
}
6360

@@ -81,7 +78,7 @@ pub fn get_indices_names_hashmap(
8178
}
8279
if !found {
8380
return Err(PolarsError::ComputeError(
84-
format!("Column '{}' not found in sheet header", col_name).into(),
81+
format!("Column '{col_name}' not found in sheet header").into(),
8582
));
8683
}
8784
}
@@ -91,7 +88,7 @@ pub fn get_indices_names_hashmap(
9188

9289
pub fn get_sheet_by_name(sheet_name: &str) -> PolarsResult<Sheet> {
9390
let doc = read_ods("src/assumptions/assumptions.ods")
94-
.map_err(|e| PolarsError::ComputeError(format!("Failed to read ODS file: {}", e).into()))?;
91+
.map_err(|e| PolarsError::ComputeError(format!("Failed to read ODS file: {e}").into()))?;
9592

9693
for idx in 0..doc.num_sheets() {
9794
let wsh = doc.sheet(idx);
@@ -101,7 +98,7 @@ pub fn get_sheet_by_name(sheet_name: &str) -> PolarsResult<Sheet> {
10198
}
10299

103100
Err(PolarsError::ComputeError(
104-
format!("Sheet '{}' not found", sheet_name).into(),
101+
format!("Sheet '{sheet_name}' not found").into(),
105102
))
106103
}
107104

@@ -115,23 +112,21 @@ pub fn parse_col_by_index_to_f64(
115112
let mut row_idx = 1; // Skip header
116113

117114
let mut col_data = Vec::new();
118-
loop {
119-
if let Some(cell_content) = sheet.cell(row_idx, col_idx as u32) {
120-
let cell_str = format!("{:?}", cell_content);
121-
122-
let val = if cell_str.contains("Number(") {
123-
let start = cell_str.find("Number(").unwrap_or(0) + 7;
124-
let end = cell_str.rfind(")").unwrap_or(cell_str.len());
125-
cell_str[start..end].parse::<f64>().unwrap_or(0.0)
126-
} else {
127-
0.0
128-
};
129-
col_data.push(val);
130-
row_idx += 1;
115+
116+
while let Some(cell_content) = sheet.cell(row_idx, col_idx as u32) {
117+
let cell_str = format!("{cell_content:?}");
118+
119+
let val = if cell_str.contains("Number(") {
120+
let start = cell_str.find("Number(").unwrap_or(0) + 7;
121+
let end = cell_str.rfind(")").unwrap_or(cell_str.len());
122+
cell_str[start..end].parse::<f64>().unwrap_or(0.0)
131123
} else {
132-
break;
133-
}
124+
0.0
125+
};
126+
col_data.push(val);
127+
row_idx += 1;
134128
}
129+
135130
Ok(col_data)
136131
}
137132

@@ -146,36 +141,34 @@ pub fn parse_col_by_index_to_string(sheet: &Sheet, col_idx: usize) -> PolarsResu
146141
let mut row_idx = 1; // Skip header
147142

148143
let mut col_data = Vec::new();
149-
loop {
150-
if let Some(cell_content) = sheet.cell(row_idx, col_idx as u32) {
151-
let cell_str = format!("{:?}", cell_content);
152144

153-
// Extract the text value from the cell string
154-
let val = if cell_str.contains("Text(") {
155-
let start = cell_str.find("Text(").unwrap_or(0) + 5;
145+
while let Some(cell_content) = sheet.cell(row_idx, col_idx as u32) {
146+
let cell_str = format!("{cell_content:?}");
156147

157-
let end = if let Some(style_pos) = cell_str.find("), style:") {
158-
style_pos
159-
} else {
160-
cell_str.rfind(")").unwrap_or(cell_str.len())
161-
};
162-
163-
let extracted = &cell_str[start..end];
148+
// Extract the text value from the cell string
149+
let val = if cell_str.contains("Text(") {
150+
let start = cell_str.find("Text(").unwrap_or(0) + 5;
164151

165-
// Remove quotes if present
166-
if extracted.starts_with('"') && extracted.ends_with('"') {
167-
extracted[1..extracted.len() - 1].to_string()
168-
} else {
169-
extracted.to_string()
170-
}
152+
let end = if let Some(style_pos) = cell_str.find("), style:") {
153+
style_pos
171154
} else {
172-
cell_str
155+
cell_str.rfind(")").unwrap_or(cell_str.len())
173156
};
174-
col_data.push(val);
175-
row_idx += 1;
157+
158+
let extracted = &cell_str[start..end];
159+
160+
// Remove quotes if present
161+
if extracted.starts_with('"') && extracted.ends_with('"') {
162+
extracted[1..extracted.len() - 1].to_string()
163+
} else {
164+
extracted.to_string()
165+
}
176166
} else {
177-
break;
178-
}
167+
cell_str
168+
};
169+
col_data.push(val);
170+
row_idx += 1;
179171
}
172+
180173
Ok(col_data)
181174
}

src/main.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ mod projections;
55

66
use crate::assumptions::assumption_scenario::AssumptionScenario;
77
use crate::model_points::generate_model_points;
8-
use crate::prem_rate::get_avg_prem_rate_by_age_and_term;
9-
use crate::projections::projection_multi_runs::{RunsResult, RunsSetup};
8+
use crate::projections::projection_multi_runs::RunsSetup;
109
use crate::projections::projection_single_run::SingleRunSetup;
1110

1211
use polars::prelude::*;

src/model_points.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ pub fn generate_model_points(mp_size: usize, seed: usize) -> PolarsResult<DataFr
4343
// Sum insured (Float): Random values between 100,000 and 1,000,000 (multiple of 1000)
4444
let sum_insured =
4545
Array1::random_using(mp_size, Uniform::new(0.0f64, 1.0f64), &mut rng) // Random floats between 0 and 1
46-
.mapv(|x| (((900_000.0 * x + 100_000.0) / 1000.0).round() * 1000.0) as f64);
46+
.mapv(|x| (((900_000.0 * x + 100_000.0) / 1000.0).round() * 1000.0));
4747

4848
// Create a DataFrame with the generated data
4949
let model_points_df = df![
5050
"id" => (1..(mp_size+1) as i32).collect::<Vec<i32>>(),
5151
"entry_age" => entry_age.to_vec(),
5252
"gender" => gender,
5353
"term" => term.to_vec(),
54-
"policy_count" => policy_count.to_vec().into_iter().map(|x| x as f64).collect::<Vec<f64>>(),
54+
"policy_count" => policy_count.to_vec().into_iter().collect::<Vec<f64>>(),
5555
"sum_insured" => sum_insured.to_vec(),
5656
]?;
5757

src/prem_rate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use polars::prelude::*;
44
//---------------------------------------------------------------------------------------------------------
55
// PUBLIC
66
//---------------------------------------------------------------------------------------------------------
7+
#[allow(dead_code)]
78
pub fn get_avg_prem_rate_by_age_and_term(run_result: &SingleRunResult) -> PolarsResult<DataFrame> {
89
let proj_df = run_result.projected_df.clone();
910

src/projections/helpers.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@ pub fn create_folder(path: &Path) {
1313
}
1414
}
1515
}
16-
} else {
17-
if !path.exists() {
18-
if let Err(e) = create_dir_all(path) {
19-
panic!("Failed to create folder {}: {}", path.display(), e);
20-
}
16+
} else if !path.exists() {
17+
if let Err(e) = create_dir_all(path) {
18+
panic!("Failed to create folder {}: {}", path.display(), e);
2119
}
2220
}
2321
}

src/projections/projection_mp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ fn __map_mort_assumption(
4848
.find(|&col| col.ends_with(&suffix))
4949
.ok_or_else(|| {
5050
PolarsError::ComputeError(
51-
format!("Mortality column with suffix '{}' not found", suffix).into(),
51+
format!("Mortality column with suffix '{suffix}' not found").into(),
5252
)
5353
})?
5454
.as_str();

src/projections/projection_multi_runs.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub struct RunsSetup {
1919
pub setups: Vec<SingleRunSetup>, // Vector of run setups
2020
}
2121

22+
#[allow(dead_code)]
2223
impl RunsSetup {
2324
pub fn get_run_setup_count(&self) -> usize {
2425
self.setups.len()
@@ -42,6 +43,7 @@ pub struct RunsResult {
4243
pub results: Vec<SingleRunResult>, // Vector of run results
4344
}
4445

46+
#[allow(dead_code)]
4547
impl RunsResult {
4648
pub fn get_run_result_count(&self) -> usize {
4749
self.results.len()
@@ -78,7 +80,7 @@ impl RunsResult {
7880
let path = Path::new(&folder_path_str);
7981

8082
// Create the folder if it does not exist
81-
create_folder(&path);
83+
create_folder(path);
8284

8385
// Export description
8486
let description_content = serde_json::json!({
@@ -93,7 +95,7 @@ impl RunsResult {
9395

9496
// Loop over each run result and export it
9597
for (i, result) in self.results.iter().enumerate() {
96-
let run_path = path.join(format!("run_{}", i)); // Export each run result
98+
let run_path = path.join(format!("run_{i}")); // Export each run result
9799
create_folder(&run_path); // Create the folder for the run
98100
result.export(run_path.to_str().unwrap())?; // Export run setup
99101
}
@@ -107,7 +109,7 @@ impl RunsResult {
107109
// Check if the folder exists
108110
if !path.exists() || !path.is_dir() {
109111
return Err(PolarsError::ComputeError(
110-
format!("Folder does not exist: {}", folder_path_str).into(),
112+
format!("Folder does not exist: {folder_path_str}").into(),
111113
));
112114
}
113115

@@ -117,7 +119,7 @@ impl RunsResult {
117119
let info_content = read_to_string(info)?;
118120

119121
let info_json: serde_json::Value = serde_json::from_str(&info_content)
120-
.map_err(|e| PolarsError::ComputeError(format!("serde_json error: {}", e).into()))?;
122+
.map_err(|e| PolarsError::ComputeError(format!("serde_json error: {e}").into()))?;
121123

122124
let description = info_json["description"]
123125
.as_str()
@@ -130,14 +132,14 @@ impl RunsResult {
130132
let mut results: Vec<SingleRunResult> = Vec::with_capacity(vec_len); // Preallocate capacity for results
131133

132134
for i in 0..vec_len {
133-
let run_path = path.join(format!("run_{}", i));
135+
let run_path = path.join(format!("run_{i}"));
134136
let result = SingleRunResult::import(run_path.to_str().unwrap())?;
135137
results.push(result);
136138
}
137139

138140
let result = RunsResult {
139-
description: description,
140-
results: results,
141+
description,
142+
results,
141143
};
142144

143145
Ok(result)
@@ -163,7 +165,7 @@ fn _project_runs(
163165

164166
let runs = RunsResult {
165167
description: description.unwrap_or_default().to_string(),
166-
results: results,
168+
results,
167169
};
168170

169171
Ok(runs)

0 commit comments

Comments
 (0)