med64 0.0.2

Headless MED64 electrophysiology toolkit: .modat I/O, spike/burst analysis, assays, spike-rate maps, and CSV/HTML/SVG/HDF5 export
Documentation
//! Analysis report CSV/HDF5/chart export helpers.

mod batch;
mod bundles;
mod create_image;
mod csv;
mod draw;
mod hdf5;
mod tables;
mod util;

pub use batch::{write_burst_batch_package, write_burst_batch_package_with_chart, AsdrChartKind};
pub use bundles::{
    write_analysis_bundle, write_burst_detector_bundle, write_burst_detector_bundle_with_options,
    BurstDetectorBundleOptions,
};
pub use create_image::{
    write_chart_snapshots, write_create_image_package, write_create_image_package_with_options,
    write_png_snapshot, write_svg_snapshot, CreateImagePackageOptions,
};
#[cfg(feature = "histograms")]
#[allow(unused_imports)] // public export surface; used via crate::export::
pub use csv::write_histogram_csv;
pub use csv::{
    write_asdr_csv, write_autocorr_csv, write_average_waveforms_csv, write_burst_profile_csv,
    write_filtered_raw_csv, write_frequency_csv, write_intra_burst_csv, write_multi_burst_csv,
    write_raster_csv, write_single_burst_csv, write_spike_timestamps_csv,
    write_spike_timestamps_csv_from_train, write_spike_timestamps_plus_waveforms_csv,
};
#[cfg(feature = "correlation")]
#[allow(unused_imports)] // public export surface; used via crate::export::
pub use csv::{write_correlogram_csv, write_synchrony_csv};
pub(crate) use draw::{
    line_chart_pixels_public, raster_chart_pixels, write_line_bmp, write_line_png,
    write_raster_bmp, write_raster_png,
};
pub use hdf5::{write_spike_hdf5, write_spike_timestamps_hdf5, SpikeHdf5Options};
pub use tables::{
    write_amplitude_rank_csv, write_result_table_compare_csv, write_result_table_csv,
    write_result_table_csv_with_options, write_stat_results_csv,
};

#[cfg(all(test, feature = "analysis"))]
mod bundle_tests {
    use super::*;
    use crate::analysis::{run_analysis, AnalysisOptions};

    #[test]
    fn burst_bundle_and_create_image_smoke() {
        let report = run_analysis(
            crate::fixture_paths::sample_data1(),
            &AnalysisOptions {
                max_blocks: Some(2),
                retain_filtered: true,
                ..AnalysisOptions::reference()
            },
        )
        .unwrap();
        let dir = std::env::temp_dir().join("med64_burst_bundle_test");
        let _ = std::fs::remove_dir_all(&dir);
        let files = write_burst_detector_bundle_with_options(
            &report,
            &dir,
            &BurstDetectorBundleOptions::default(),
        )
        .unwrap();
        assert!(files.iter().any(|p| p.ends_with("batch_prefs.json")));
        assert!(dir.join("IntraBurstSpikes.csv").is_file());
        #[cfg(feature = "histograms")]
        {
            assert!(dir.join("ISI_Histogram.csv").is_file());
            assert!(dir.join("Amplitude_Histogram.csv").is_file());
        }
        let prefs: BurstDetectorBundleOptions =
            serde_json::from_str(&std::fs::read_to_string(dir.join("batch_prefs.json")).unwrap())
                .unwrap();
        assert!(prefs.include_intra_burst);
        assert!(prefs.include_amplitude_hist);

        let img = dir.join("create_image");
        let snaps = write_create_image_package(&report, &img, "sample1").unwrap();
        assert!(!snaps.is_empty());
        assert!(img.join("sample1_asdr.svg").is_file());
        #[cfg(feature = "histograms")]
        assert!(
            img.join("sample1_isi_hist.svg").is_file() || img.join("ISIHistogram.svg").is_file()
        );
    }

    #[test]
    fn burst_batch_package_writes_expected_filenames() {
        use crate::toolkit::batch_export_prefs::{
            load_batch_export_prefs, write_batch_export_prefs, BatchExportPrefs, ChartImageType,
            ExportTraceType,
        };
        let report = run_analysis(
            crate::fixture_paths::sample_data1(),
            &AnalysisOptions { max_blocks: Some(2), ..AnalysisOptions::reference() },
        )
        .unwrap();
        let dir = std::env::temp_dir().join(format!("med64_burst_batch_{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&dir);
        let prefs = BatchExportPrefs {
            export_trace_type: ExportTraceType::AllInOne,
            chart_image_type: ChartImageType::Png,
            export_chart: true,
            export_burst: true,
            export_summary_values: true,
            export_individual_values: true,
            export_details: true,
            export_spike_events: true,
            ..BatchExportPrefs::default()
        };
        let files = write_burst_batch_package(&report, &dir, &prefs, "stem").unwrap();
        assert!(dir.join("stemChart.csv").is_file());
        assert!(dir.join("stemBurst.csv").is_file());
        assert!(dir.join("stemSummary values.csv").is_file());
        assert!(dir.join("stemIndividual values.csv").is_file());
        assert!(dir.join("stemDetails.csv").is_file());
        assert!(dir.join("stemSpikes events.csv").is_file());
        assert!(dir.join("stemSpikes events.png").is_file());
        let png_bytes = std::fs::metadata(dir.join("stemSpikes events.png")).unwrap().len();
        assert!(png_bytes > 100, "raster png should be non-trivial, got {png_bytes}");
        assert!(dir.join("image_export_manifest.json").is_file());
        assert!(dir.join("batch_export_prefs.properties").is_file());
        assert!(files.len() >= 7);
        let loaded = load_batch_export_prefs(dir.join("batch_export_prefs.properties")).unwrap();
        assert!(loaded.export_chart);
        write_batch_export_prefs(&loaded, dir.join("roundtrip.properties")).unwrap();
        let again = load_batch_export_prefs(dir.join("roundtrip.properties")).unwrap();
        assert_eq!(again.export_trace_type, ExportTraceType::AllInOne);
        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn create_image_options_roundtrip_formats() {
        let opts = CreateImagePackageOptions {
            write_svg: true,
            write_png: true,
            write_bmp: false,
            write_eps: false,
            ..CreateImagePackageOptions::default()
        };
        assert_eq!(opts.enabled_formats(), vec!["svg", "png"]);
        let json = serde_json::to_string(&opts).unwrap();
        let back: CreateImagePackageOptions = serde_json::from_str(&json).unwrap();
        assert_eq!(back.width, 800);
        assert_eq!(back.height, 280);
        assert!(!back.write_bmp);
        assert!(back.copy_manifest);
    }

    #[test]
    fn create_image_package_with_options_smoke() {
        let report = run_analysis(
            crate::fixture_paths::sample_data1(),
            &AnalysisOptions {
                max_blocks: Some(2),
                retain_filtered: true,
                ..AnalysisOptions::reference()
            },
        )
        .unwrap();
        let dir = std::env::temp_dir().join(format!("med64_cimg_opts_{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&dir);
        let opts = CreateImagePackageOptions {
            write_svg: true,
            write_png: true,
            write_bmp: false,
            write_eps: false,
            include_botcc: false,
            ..CreateImagePackageOptions::default()
        };
        let files = write_create_image_package_with_options(&report, &dir, "t", &opts).unwrap();
        assert!(!files.is_empty());
        assert!(dir.join("copy_image_manifest.json").is_file());
        assert!(dir.join("t_asdr_raster.svg").is_file() || dir.join("ASDR_Raster.svg").is_file());
        let _ = std::fs::remove_dir_all(&dir);
    }
}