Skip to content

Commit

Permalink
Merge pull request #1 from kmgill/ser_as_trait_impl
Browse files Browse the repository at this point in the history
Ser as trait impl
  • Loading branch information
kmgill authored Apr 15, 2024
2 parents f9e6406 + 6af84cd commit 666b8b1
Show file tree
Hide file tree
Showing 19 changed files with 509 additions and 309 deletions.
7 changes: 5 additions & 2 deletions bin/subs/preprocess.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use crate::subs::runnable::RunnableSubcommand;
use anyhow::Result;
use clap::Parser;

// use sciimg::prelude::*;
use solhat::calibrationframe::CalibrationImage;
use solhat::calibrationframe::ComputeMethod;
use solhat::context::*;
use solhat::drizzle::Scale;
use solhat::ser::SerFile;
use solhat::target::Target;

use crate::subs::runnable::RunnableSubcommand;

pb_create!();

#[derive(Parser)]
Expand Down Expand Up @@ -114,7 +117,7 @@ impl RunnableSubcommand for PreProcess {
CalibrationImage::new_empty()
};

let _context = ProcessContext::create_with_calibration_frames(
let _context: ProcessContext<SerFile> = ProcessContext::create_with_calibration_frames(
&ProcessParameters {
input_files: self.input_files.clone(),
obj_detection_threshold: self.threshold.unwrap_or(5000.0),
Expand Down
7 changes: 5 additions & 2 deletions bin/subs/process.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::subs::runnable::RunnableSubcommand;
use anyhow::Result;
use clap::Parser;

use solhat::anaysis::frame_sigma_analysis;
use solhat::calibrationframe::CalibrationImage;
use solhat::calibrationframe::ComputeMethod;
Expand All @@ -9,9 +9,12 @@ use solhat::drizzle::Scale;
use solhat::limiting::frame_limit_determinate;
use solhat::offsetting::frame_offset_analysis;
use solhat::rotation::frame_rotation_analysis;
use solhat::ser::SerFile;
use solhat::stacking::process_frame_stacking;
use solhat::target::Target;

use crate::subs::runnable::RunnableSubcommand;

pb_create!();

#[derive(Parser)]
Expand Down Expand Up @@ -122,7 +125,7 @@ impl RunnableSubcommand for Process {
};

info!("Creating process context...");
let mut context = ProcessContext::create_with_calibration_frames(
let mut context: ProcessContext<SerFile> = ProcessContext::create_with_calibration_frames(
&ProcessParameters {
input_files: self.input_files.clone(),
obj_detection_threshold: self.threshold.unwrap_or(5000.0),
Expand Down
15 changes: 11 additions & 4 deletions bin/subs/serinfo.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
use crate::subs::runnable::RunnableSubcommand;
use anyhow::Result;
use clap::Parser;
use sciimg::path;

use solhat::datasource::DataSource;
use solhat::ser;

use crate::subs::runnable::RunnableSubcommand;

#[derive(Parser)]
#[command(author, version, about = "Print information from a SER file", long_about = None)]
pub struct SerInfo {
#[clap(long, short, help = "Input ser file")]
input_file: String,
}

fn do_validation<F: DataSource>(ser_file: &F) -> Result<()> {
ser_file.validate()?;
ser_file.print_header_details();
Ok(())
}

#[async_trait::async_trait]
impl RunnableSubcommand for SerInfo {
async fn run(&self) -> Result<()> {
if path::file_exists(self.input_file.as_str()) {
let ser_file =
ser::SerFile::load_ser(&self.input_file).expect("Unable to load SER file");
ser_file.validate();

ser_file.print_header_details();
do_validation(&ser_file)?;
}
Ok(())
}
Expand Down
7 changes: 5 additions & 2 deletions bin/subs/threshtest.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use crate::subs::runnable::RunnableSubcommand;
use anyhow::Result;
use clap::Parser;

use solhat::calibrationframe::CalibrationImage;
use solhat::calibrationframe::ComputeMethod;
use solhat::context::*;
use solhat::drizzle::Scale;
use solhat::ser::SerFile;
use solhat::target::Target;
use solhat::threshtest::compute_threshtest_image;

use crate::subs::runnable::RunnableSubcommand;

pb_create_spinner!();

#[derive(Parser)]
Expand Down Expand Up @@ -64,7 +67,7 @@ impl RunnableSubcommand for ThreshTest {
CalibrationImage::new_empty()
};

let context = ProcessContext::create_with_calibration_frames(
let context: ProcessContext<SerFile> = ProcessContext::create_with_calibration_frames(
&ProcessParameters {
input_files: self.input_files.clone(),
obj_detection_threshold: self.threshold.unwrap_or(5000.0),
Expand Down
24 changes: 14 additions & 10 deletions src/anaysis.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
use crate::context::ProcessContext;
use crate::framerecord::FrameRecord;
use anyhow::Result;
use rayon::prelude::*;
use sciimg::quality;

pub fn frame_sigma_analysis<F>(
context: &ProcessContext,
on_frame_checked: F,
use crate::context::ProcessContext;
use crate::datasource::DataSource;
use crate::framerecord::FrameRecord;

pub fn frame_sigma_analysis<C, F>(
context: &ProcessContext<F>,
on_frame_checked: C,
) -> Result<Vec<FrameRecord>>
where
F: Fn(&FrameRecord) + Send + Sync + 'static,
C: Fn(&FrameRecord) + Send + Sync + 'static,
F: DataSource + Send + Sync + 'static,
{
frame_sigma_analysis_window_size(context, 128, on_frame_checked)
}

pub fn frame_sigma_analysis_window_size<F>(
context: &ProcessContext,
pub fn frame_sigma_analysis_window_size<C, F>(
context: &ProcessContext<F>,
window_size: usize,
on_frame_checked: F,
on_frame_checked: C,
) -> Result<Vec<FrameRecord>>
where
F: Fn(&FrameRecord) + Send + Sync + 'static,
C: Fn(&FrameRecord) + Send + Sync + 'static,
F: DataSource + Send + Sync + 'static,
{
let frame_records: Vec<FrameRecord> = context
.frame_records
Expand Down
24 changes: 14 additions & 10 deletions src/calibrationframe.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use crate::mean;
use crate::median;
use std::{ffi::OsStr, path::Path};

use anyhow::{Error, Result};
use sciimg::prelude::*;
use std::{ffi::OsStr, path::Path};

use crate::datasource::DataSource;
use crate::mean;
use crate::median;
use crate::ser::SerFile;

#[derive(Debug, PartialEq, Eq)]
pub enum ComputeMethod {
Expand All @@ -14,14 +18,13 @@ pub struct CalibrationImage {
pub image: Option<Image>,
}

fn create_mean_from_ser(ser_file_path: &str) -> Result<Image> {
let input_files: Vec<&str> = vec![ser_file_path];
let mean_stack = mean::compute_mean(&input_files, true)?;
fn create_mean_from_ser<F: DataSource + Send + Sync + 'static>(ser_file: &F) -> Result<Image> {
let mean_stack = mean::compute_mean(ser_file, true)?;
Ok(mean_stack)
}

fn create_median_from_ser(ser_file_path: &str) -> Result<Image> {
median::compute_mean(ser_file_path)
fn create_median_from_ser<F: DataSource + Send + Sync + 'static>(ser_file: &F) -> Result<Image> {
median::compute_mean(ser_file)
}

impl CalibrationImage {
Expand All @@ -41,9 +44,10 @@ impl CalibrationImage {
}

pub fn new_from_ser(ser_path: &str, method: ComputeMethod) -> Result<Self> {
let ser_file = SerFile::load_ser(ser_path)?;
let image = match method {
ComputeMethod::Mean => create_mean_from_ser(ser_path)?,
ComputeMethod::Median => create_median_from_ser(ser_path)?,
ComputeMethod::Mean => create_mean_from_ser(&ser_file)?,
ComputeMethod::Median => create_median_from_ser(&ser_file)?,
};
Ok(CalibrationImage { image: Some(image) })
}
Expand Down
38 changes: 22 additions & 16 deletions src/context.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use anyhow::Result;
use rayon::prelude::*;
use sciimg::imagebuffer::Offset;
use sciimg::prelude::ImageBuffer;

use crate::calibrationframe::CalibrationImage;
use crate::datasource::DataSource;
use crate::drizzle::Scale;
use crate::fpmap::FpMap;
use crate::framerecord::FrameRecord;
use crate::hotpixel;
use crate::ser::SerFile;
use crate::stats::ProcessStats;
use crate::target::Target;
use anyhow::Result;
use rayon::prelude::*;
use sciimg::imagebuffer::Offset;
use sciimg::prelude::ImageBuffer;

#[derive(Debug, Clone)]
pub struct ProcessParameters {
Expand All @@ -36,9 +37,9 @@ pub struct ProcessParameters {
pub horiz_offset: i32,
}

pub struct ProcessContext {
pub struct ProcessContext<F: DataSource> {
pub parameters: ProcessParameters,
pub fp_map: FpMap,
pub fp_map: FpMap<F>,
pub master_flat: CalibrationImage,
pub master_dark: CalibrationImage,
pub master_darkflat: CalibrationImage,
Expand All @@ -48,19 +49,22 @@ pub struct ProcessContext {
pub hotpixel_mask: Option<ImageBuffer>,
}

fn load_frame_records_for_ser(ser_file: &SerFile, number_of_frames: usize) -> Vec<FrameRecord> {
let frame_count = if ser_file.frame_count > number_of_frames {
fn load_frame_records_for_data_source<F: DataSource>(
ser_file: &F,
number_of_frames: usize,
) -> Vec<FrameRecord> {
let frame_count = if ser_file.frame_count() > number_of_frames {
number_of_frames
} else {
ser_file.frame_count
ser_file.frame_count()
};

(0..frame_count)
.map(|i| FrameRecord {
source_file_id: ser_file.source_file.to_string(),
source_file_id: ser_file.file_hash(),
frame_id: i,
frame_width: ser_file.image_width,
frame_height: ser_file.image_height,
frame_width: ser_file.image_width(),
frame_height: ser_file.image_height(),
sigma: 0.0,
computed_rotation: 0.0,
offset: Offset { h: 0.0, v: 0.0 },
Expand All @@ -78,7 +82,7 @@ fn load_hot_pixel_mask(file_path: &Option<String>) -> Result<Option<ImageBuffer>
}
}

impl ProcessContext {
impl<F: DataSource + Send + Sync + 'static> ProcessContext<F> {
pub fn create_with_calibration_frames(
params: &ProcessParameters,
master_flat: CalibrationImage,
Expand All @@ -101,15 +105,17 @@ impl ProcessContext {
params.input_files.iter().for_each(|input_file| {
info!("Loading input file: {}", input_file);
pc.fp_map
.open(input_file)
.open(&[input_file.to_string()])
.expect("Failed to open input file");
});

pc.frame_records = pc
.fp_map
.get_map()
.par_iter()
.map(|(_, ser)| load_frame_records_for_ser(ser, params.max_frames.unwrap_or(100000000)))
.map(|(_, ser)| {
load_frame_records_for_data_source(ser, params.max_frames.unwrap_or(100000000))
})
.collect::<Vec<Vec<FrameRecord>>>()
.iter()
.flatten()
Expand Down
Loading

0 comments on commit 666b8b1

Please sign in to comment.