single_rust 0.5.8

Single-cell analysis in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
//! # Memory Utilities for Single-Cell Data Processing
//!
//! This module provides essential utility functions for handling data type conversions,
//! matrix transformations, and data structure manipulations in single-cell analysis.
//! These utilities are crucial for ensuring compatibility between different numeric types
//! and data formats used throughout the SingleRust ecosystem.
//!
//! ## Key Functionality
//!
//! ### Type Conversions
//! - **Float conversions**: Convert integer/other numeric types to f32/f64 for analysis
//! - **Precision control**: Handle single vs double precision requirements
//! - **Matrix format preservation**: Maintain sparse matrix structure during conversion
//!
//! ### Data Structure Utilities
//! - **DataFrame creation**: Convert analysis results to Polars DataFrames
//! - **Array conversions**: Transform between different ndarray types
//! - **Result formatting**: Prepare data for storage in AnnData objects
//!
//! ## Why Type Conversion Matters
//!
//! Single-cell data often comes in various numeric formats:
//! - **Raw counts**: Usually integers (u32, i32)
//! - **Normalized data**: Requires floating point (f32, f64)
//! - **Statistical results**: Often need double precision (f64)
//! - **Memory efficiency**: Sometimes f32 is preferred for large datasets
//!
//! This module handles these conversions safely and efficiently while preserving
//! data structure characteristics like sparsity.
//!
//! ## Usage Patterns
//!
//! ```rust,ignore
//! use single_rust::memory::utils::{
//!     convert_to_float_if_non_float_type,
//!     create_dataframe_from_map,
//!     arr1_conversion
//! };
//! use single_rust::shared::Precision;
//!
//! // Convert matrix to double precision for analysis
//! convert_to_float_if_non_float_type(&adata.x(), Some(Precision::Double))?;
//!
//! // Create DataFrame from analysis results
//! let mut results = HashMap::new();
//! results.insert("group_A".to_string(), vec![1.0, 2.0, 3.0]);
//! results.insert("group_B".to_string(), vec![4.0, 5.0, 6.0]);
//! let df = create_dataframe_from_map(&results)?;
//! ```
//!
//! ## Supported Conversions
//!
//! ### Matrix Types
//! - **Dense arrays**: ndarray Array types
//! - **CSR sparse matrices**: Compressed Sparse Row format
//! - **CSC sparse matrices**: Compressed Sparse Column format
//!
//! ### Numeric Types
//! - **Integers**: i8, i16, i32, i64, u8, u16, u32, u64
//! - **Floats**: f32, f64
//! - **Target precision**: Single (f32) or Double (f64)
//!
//! ## Performance Considerations
//!
//! - **In-place conversion**: Minimizes memory usage by modifying existing data
//! - **Sparse preservation**: Maintains sparsity structure during conversion
//! - **Batch operations**: Efficient vectorized conversions
//! - **Zero-copy when possible**: Avoids unnecessary data copying

use polars::prelude::{IntoColumn, NamedFrom, NamedFromOwned};
use polars::series::Series;
use std::collections::HashMap;
use std::ops::DerefMut;

use crate::shared::{need_conversion_target_float_type, Precision};
use anndata::{backend::DataType, data::DynArray, ArrayData};
use anndata_memory::IMArrayElement;
use anyhow::{anyhow, bail};
use nalgebra_sparse::{CscMatrix, CsrMatrix};
use ndarray::{Array1, Array2, ArrayBase, Dim, IxDynImpl, OwnedRepr};
use num_traits::{Float, Num, NumCast};
use polars::prelude::DataFrame;
use single_utilities::traits::NumericOps;

/// Check if a matrix data type requires conversion to float for processing.
///
/// Determines whether the given matrix data type needs to be converted to a floating-point
/// type for mathematical operations. Most single-cell analysis requires floating-point
/// arithmetic for normalization, statistical tests, and dimensionality reduction.
///
/// ## Parameters
/// * `matrix_datatype` - The AnnData backend data type to check
///
/// ## Returns
/// * `Ok(true)` - Conversion to float is needed
/// * `Ok(false)` - Data is already in appropriate float format
/// * `Err` - Unsupported data type for conversion
///
/// ## Supported Types
/// - Array types (dense matrices)
/// - CSR/CSC sparse matrices  
/// - Scalar values
///
/// ## Unsupported Types
/// - DataFrames (use specialized handling)
/// - Mappings (not suitable for numeric operations)
/// - Categorical data (convert manually first)
/// - NullableArrays (handle nulls before conversion)
pub fn _target_type_float_need_conversion_in_memory(
    matrix_datatype: &DataType,
) -> anyhow::Result<bool> {
    match matrix_datatype {
        DataType::Array(scalar_type) => need_conversion_target_float_type(scalar_type),
        DataType::CsrMatrix(scalar_type) => need_conversion_target_float_type(scalar_type),
        DataType::CscMatrix(scalar_type) => need_conversion_target_float_type(scalar_type),
        DataType::DataFrame => {
            bail!("Cannot use a matrix of type <DataFrame> in the normalization procedure.")
        }
        DataType::Mapping => {
            bail!("Cannot use a matrix of type <Mapping> in the normalization procedure.")
        }
        DataType::Scalar(scalar_type) => need_conversion_target_float_type(scalar_type),
        DataType::Categorical => {
            bail!("Cannot use a matrix of type <Categorical> in the normalization procedure.")
        }
        DataType::NullableArray => {
            bail!("Cannot use a matrix of type <NullableArray> in the normalization procedure.")
        }
    }
}

/// Convert matrix data to floating-point format if needed for analysis.
///
/// This is the main conversion function that handles in-place transformation of matrix data
/// from integer or other numeric types to floating-point types (f32 or f64). Essential for
/// most single-cell analysis operations that require floating-point arithmetic.
///
/// ## Key Features
/// - **In-place conversion**: Modifies existing data to minimize memory usage
/// - **Precision control**: Choose between single (f32) or double (f64) precision
/// - **Sparse preservation**: Maintains sparse matrix structure during conversion
/// - **Type safety**: Comprehensive error handling for unsupported conversions
///
/// ## Parameters
/// * `matrix` - The matrix element to convert (modified in-place)
/// * `precision` - Target precision (Single=f32, Double=f64, None=Double)
///
/// ## Supported Source Types
/// - **Integers**: i8, i16, i32, i64, u8, u16, u32, u64
/// - **Floats**: f32 ↔ f64 conversion
/// - **Matrix formats**: Dense arrays, CSR matrices, CSC matrices
///
/// ## When to Use
/// - Before normalization operations
/// - Prior to statistical analysis
/// - When switching between f32/f64 for memory vs precision trade-offs
/// - Before applying mathematical transformations
///
/// ## Examples
/// ```rust,ignore
/// // Convert to double precision for high-accuracy analysis
/// convert_to_float_if_non_float_type(&adata.x(), Some(Precision::Double))?;
///
/// // Convert to single precision to save memory
/// convert_to_float_if_non_float_type(&adata.x(), Some(Precision::Single))?;
///
/// // Use default (double precision)
/// convert_to_float_if_non_float_type(&adata.x(), None)?;
/// ```
///
/// ## Performance Notes
/// - In-place operation minimizes memory overhead
/// - Vectorized conversions for efficiency
/// - Preserves sparse matrix structure (no densification)
/// - Zero-copy when source and target types match
pub fn convert_to_float_if_non_float_type(
    matrix: &IMArrayElement,
    precision: Option<Precision>,
) -> anyhow::Result<()> {
    let precision = precision.unwrap_or_default();

    // For now discarded, as we want to convert f32 -> f64 and f64 -> f32 in case this becomes necessary
    //let need_to_convert_type = target_type_float_need_conversion_in_memory(&matrix_data_type)?;

    //if !need_to_convert_type {
    //    return Ok(());
    //}

    let mut write_guard = matrix.0.write_inner();
    let data = write_guard.deref_mut();

    let dummy_data: Array2<f64> = Array2::zeros((0, 0));
    let dummy_array_data = ArrayData::Array(DynArray::from(dummy_data));
    let original_matrix_data = std::mem::replace(data, dummy_array_data);

    let new_matrix: anyhow::Result<ArrayData> = match original_matrix_data {
        ArrayData::Array(dyn_array) => {
            match (dyn_array, precision) {
                (DynArray::I8(array_base), Precision::Single) => {
                    let converted = convert_array::<i8, f32>(array_base)?;
                    Ok(ArrayData::from(converted))
                },
                (DynArray::I8(array_base), Precision::Double) => {
                    let converted = convert_array::<i8, f64>(array_base)?;
                    Ok(ArrayData::from(converted))
                },
                (DynArray::I16(array_base), Precision::Single) => {
                    let converted = convert_array::<i16, f32>(array_base)?;
                    Ok(ArrayData::from(converted))
                },
                (DynArray::I16(array_base), Precision::Double) => {
                    let converted = convert_array::<i16, f64>(array_base)?;
                    Ok(ArrayData::from(converted))
                },
                (DynArray::I32(array_base), Precision::Single) => {
                    let converted = convert_array::<i32, f32>(array_base)?;
                    Ok(ArrayData::from(converted))
                },
                (DynArray::I32(array_base), Precision::Double) => {
                    let converted = convert_array::<i32, f64>(array_base)?;
                    Ok(ArrayData::from(converted))
                },
                (DynArray::I64(array_base), Precision::Single) => {
                    let converted = convert_array::<i64, f32>(array_base)?;
                    Ok(ArrayData::from(converted))
                },
                (DynArray::I64(array_base), Precision::Double) => {
                    let converted = convert_array::<i64, f64>(array_base)?;
                    Ok(ArrayData::from(converted))
                },
                (DynArray::U8(array_base), Precision::Single) => {
                    let converted = convert_array::<u8, f32>(array_base)?;
                    Ok(ArrayData::from(converted))
                },
                (DynArray::U8(array_base), Precision::Double) => {
                    let converted = convert_array::<u8, f64>(array_base)?;
                    Ok(ArrayData::from(converted))
                },
                (DynArray::U16(array_base), Precision::Single) => {
                    let converted = convert_array::<u16, f32>(array_base)?;
                    Ok(ArrayData::from(converted))
                },
                (DynArray::U16(array_base), Precision::Double) => {
                    let converted = convert_array::<u16, f64>(array_base)?;
                    Ok(ArrayData::from(converted))
                },
                (DynArray::U32(array_base), Precision::Single) => {
                    let converted = convert_array::<u32, f32>(array_base)?;
                    Ok(ArrayData::from(converted))
                },
                (DynArray::U32(array_base), Precision::Double) => {
                    let converted = convert_array::<u32, f64>(array_base)?;
                    Ok(ArrayData::from(converted))
                },
                (DynArray::U64(array_base), Precision::Single) => {
                    let converted = convert_array::<u64, f32>(array_base)?;
                    Ok(ArrayData::from(converted))
                },
                (DynArray::U64(array_base), Precision::Double) => {
                    let converted = convert_array::<u64, f64>(array_base)?;
                    Ok(ArrayData::from(converted))
                },
                (DynArray::F32(array_base), Precision::Single) => Ok(ArrayData::from(array_base)),
                (DynArray::F32(array_base), Precision::Double) => {
                    let converted = convert_array::<f32, f64>(array_base)?;
                    Ok(ArrayData::from(converted))
                },
                (DynArray::F64(array_base), Precision::Single) => {
                    let converted = convert_array::<f64, f32>(array_base)?;
                    Ok(ArrayData::from(converted))
                },
                (DynArray::F64(array_base), Precision::Double) => Ok(ArrayData::from(array_base)),
                (DynArray::Bool(_), Precision::Single) => bail!("ArrayBase with type: <bool> cannot be converted into float<f32>. Please convert it manually before."),
                (DynArray::Bool(_), Precision::Double) => bail!("ArrayBase with type: <bool> cannot be converted into float<f64>. Please convert it manually before."),
                (DynArray::String(_), Precision::Single) => bail!("ArrayBase with type: <string> cannot be converted into float<f32>. Please convert it manually before."),
                (DynArray::String(_), Precision::Double) => bail!("ArrayBase with type: <string> cannot be converted into float<f64>. Please convert it manually before."),
            }
        },
        ArrayData::CsrMatrix(dyn_csr_matrix) => match (dyn_csr_matrix, precision) {
            (anndata::data::DynCsrMatrix::I8(csr_matrix), Precision::Single) => {
                let converted = convert_csr_sparse_matrix::<i8, f32>(csr_matrix)?;
                Ok(ArrayData::from(converted))
            },
            (anndata::data::DynCsrMatrix::I8(csr_matrix), Precision::Double) => {
                let converted = convert_csr_sparse_matrix::<i8, f64>(csr_matrix)?;
                Ok(ArrayData::from(converted))
            },
            (anndata::data::DynCsrMatrix::I16(csr_matrix), Precision::Single) => {
                let converted = convert_csr_sparse_matrix::<i16, f32>(csr_matrix)?;
                Ok(ArrayData::from(converted))
            },
            (anndata::data::DynCsrMatrix::I16(csr_matrix), Precision::Double) => {
                let converted = convert_csr_sparse_matrix::<i16, f64>(csr_matrix)?;
                Ok(ArrayData::from(converted))
            },
            (anndata::data::DynCsrMatrix::I32(csr_matrix), Precision::Single) => {
                let converted = convert_csr_sparse_matrix::<i32, f32>(csr_matrix)?;
                Ok(ArrayData::from(converted))
            },
            (anndata::data::DynCsrMatrix::I32(csr_matrix), Precision::Double) => {
                let converted = convert_csr_sparse_matrix::<i32, f32>(csr_matrix)?;
                Ok(ArrayData::from(converted))
            },
            (anndata::data::DynCsrMatrix::I64(csr_matrix), Precision::Single) => {
                let converted = convert_csr_sparse_matrix::<i64, f32>(csr_matrix)?;
                Ok(ArrayData::from(converted))
            },
            (anndata::data::DynCsrMatrix::I64(csr_matrix), Precision::Double) => {
                let converted = convert_csr_sparse_matrix::<i64, f64>(csr_matrix)?;
                Ok(ArrayData::from(converted))
            },
            (anndata::data::DynCsrMatrix::U8(csr_matrix), Precision::Single) => {
                let converted = convert_csr_sparse_matrix::<u8, f32>(csr_matrix)?;
                Ok(ArrayData::from(converted))
            },
            (anndata::data::DynCsrMatrix::U8(csr_matrix), Precision::Double) => {
                let converted = convert_csr_sparse_matrix::<u8, f64>(csr_matrix)?;
                Ok(ArrayData::from(converted))
            },
            (anndata::data::DynCsrMatrix::U16(csr_matrix), Precision::Single) => {
                let converted = convert_csr_sparse_matrix::<u16, f32>(csr_matrix)?;
                Ok(ArrayData::from(converted))
            },
            (anndata::data::DynCsrMatrix::U16(csr_matrix), Precision::Double) => {
                let converted = convert_csr_sparse_matrix::<u16, f64>(csr_matrix)?;
                Ok(ArrayData::from(converted))
            },
            (anndata::data::DynCsrMatrix::U32(csr_matrix), Precision::Single) => {
                let converted = convert_csr_sparse_matrix::<u32, f32>(csr_matrix)?;
                Ok(ArrayData::from(converted))
            },
            (anndata::data::DynCsrMatrix::U32(csr_matrix), Precision::Double) => {
                let converted = convert_csr_sparse_matrix::<u32, f64>(csr_matrix)?;
                Ok(ArrayData::from(converted))
            },
            (anndata::data::DynCsrMatrix::U64(csr_matrix), Precision::Single) => {
                let converted = convert_csr_sparse_matrix::<u64, f32>(csr_matrix)?;
                Ok(ArrayData::from(converted))
            },
            (anndata::data::DynCsrMatrix::U64(csr_matrix), Precision::Double) => {
                let converted = convert_csr_sparse_matrix::<u64, f64>(csr_matrix)?;
                Ok(ArrayData::from(converted))
            },
            (anndata::data::DynCsrMatrix::F32(csr_matrix), Precision::Single) => Ok(ArrayData::from(csr_matrix)),
            (anndata::data::DynCsrMatrix::F32(csr_matrix), Precision::Double) => {
                let converted = convert_csr_sparse_matrix::<f32, f64>(csr_matrix)?;
                Ok(ArrayData::from(converted))
            },
            (anndata::data::DynCsrMatrix::F64(csr_matrix), Precision::Single) => {
                let converted = convert_csr_sparse_matrix::<f64, f32>(csr_matrix)?;
                Ok(ArrayData::from(converted))
            },
            (anndata::data::DynCsrMatrix::F64(csr_matrix), Precision::Double) => Ok(ArrayData::from(csr_matrix)),
            (anndata::data::DynCsrMatrix::Bool(_), Precision::Single) => bail!("CsrMatrix with type: <bool> cannot be converted into float<f32>. Please convert it manually before."),
            (anndata::data::DynCsrMatrix::Bool(_), Precision::Double) => bail!("CsrMatrix with type: <bool> cannot be converted into float<f64>. Please convert it manually before."),
            (anndata::data::DynCsrMatrix::String(_), Precision::Single) => bail!("CsrMatrix with type: <string> cannot be converted into float<f32>. Please convert it manually before."),
            (anndata::data::DynCsrMatrix::String(_), Precision::Double) => bail!("CsrMatrix with type: <string> cannot be converted into float<f64>. Please convert it manually before."),
        },
        ArrayData::CsrNonCanonical(_) => todo!("This is not implemented yet!"),
        ArrayData::CscMatrix(dyn_csc_matrix) => match (dyn_csc_matrix, precision) {
            (anndata::data::DynCscMatrix::I8(csc_matrix), Precision::Single) => {
                let converted = convert_csc_sparse_matrix::<i8, f32>(csc_matrix)?;
                Ok(ArrayData::from(converted))
            },
            (anndata::data::DynCscMatrix::I8(csc_matrix), Precision::Double) => {
                let converted = convert_csc_sparse_matrix::<i8, f64>(csc_matrix)?;
                Ok(ArrayData::from(converted))
            },
            (anndata::data::DynCscMatrix::I16(csc_matrix), Precision::Single) => {
                let converted = convert_csc_sparse_matrix::<i16, f32>(csc_matrix)?;
                Ok(ArrayData::from(converted))
            },
            (anndata::data::DynCscMatrix::I16(csc_matrix), Precision::Double) => {
                let converted = convert_csc_sparse_matrix::<i16, f64>(csc_matrix)?;
                Ok(ArrayData::from(converted))
            },
            (anndata::data::DynCscMatrix::I32(csc_matrix), Precision::Single) => {
                let converted = convert_csc_sparse_matrix::<i32, f32>(csc_matrix)?;
                Ok(ArrayData::from(converted))
            },
            (anndata::data::DynCscMatrix::I32(csc_matrix), Precision::Double) => {
                let converted = convert_csc_sparse_matrix::<i32, f64>(csc_matrix)?;
                Ok(ArrayData::from(converted))
            },
            (anndata::data::DynCscMatrix::I64(csc_matrix), Precision::Single) => {
                let converted = convert_csc_sparse_matrix::<i64, f32>(csc_matrix)?;
                Ok(ArrayData::from(converted))
            },
            (anndata::data::DynCscMatrix::I64(csc_matrix), Precision::Double) => {
                let converted = convert_csc_sparse_matrix::<i64, f64>(csc_matrix)?;
                Ok(ArrayData::from(converted))
            },
            (anndata::data::DynCscMatrix::U8(csc_matrix), Precision::Single) => {
                let converted = convert_csc_sparse_matrix::<u8, f64>(csc_matrix)?;
                Ok(ArrayData::from(converted))
            },
            (anndata::data::DynCscMatrix::U8(csc_matrix), Precision::Double) => {
                let converted = convert_csc_sparse_matrix::<u8, f64>(csc_matrix)?;
                Ok(ArrayData::from(converted))
            },
            (anndata::data::DynCscMatrix::U16(csc_matrix), Precision::Single) => {
                let converted = convert_csc_sparse_matrix::<u16, f32>(csc_matrix)?;
                Ok(ArrayData::from(converted))
            },
            (anndata::data::DynCscMatrix::U16(csc_matrix), Precision::Double) => {
                let converted = convert_csc_sparse_matrix::<u16, f64>(csc_matrix)?;
                Ok(ArrayData::from(converted))
            },
            (anndata::data::DynCscMatrix::U32(csc_matrix), Precision::Single) => {
                let converted = convert_csc_sparse_matrix::<u32, f32>(csc_matrix)?;
                Ok(ArrayData::from(converted))
            },
            (anndata::data::DynCscMatrix::U32(csc_matrix), Precision::Double) => {
                let converted = convert_csc_sparse_matrix::<u32, f64>(csc_matrix)?;
                Ok(ArrayData::from(converted))
            },
            (anndata::data::DynCscMatrix::U64(csc_matrix), Precision::Single) => {
                let converted = convert_csc_sparse_matrix::<u64, f32>(csc_matrix)?;
                Ok(ArrayData::from(converted))
            },
            (anndata::data::DynCscMatrix::U64(csc_matrix), Precision::Double) => {
                let converted = convert_csc_sparse_matrix::<u64, f64>(csc_matrix)?;
                Ok(ArrayData::from(converted))
            },
            (anndata::data::DynCscMatrix::F32(csc_matrix), Precision::Single) => Ok(ArrayData::from(csc_matrix)),
            (anndata::data::DynCscMatrix::F32(csc_matrix), Precision::Double) => {
                let converted = convert_csc_sparse_matrix::<f32, f64>(csc_matrix)?;
                Ok(ArrayData::from(converted))
            },
            (anndata::data::DynCscMatrix::F64(csc_matrix), Precision::Single) => {
                let converted = convert_csc_sparse_matrix::<f64, f32>(csc_matrix)?;
                Ok(ArrayData::from(converted))
            },
            (anndata::data::DynCscMatrix::F64(csc_matrix), Precision::Double) => Ok(ArrayData::from(csc_matrix)),
            (anndata::data::DynCscMatrix::Bool(_), Precision::Single) => bail!("CscMatrix with type: <bool> cannot be converted into float<f32>. Please convert it manually before."),
            (anndata::data::DynCscMatrix::Bool(_), Precision::Double) => bail!("CscMatrix with type: <bool> cannot be converted into float<f64>. Please convert it manually before."),
            (anndata::data::DynCscMatrix::String(_), Precision::Single) => bail!("CscMatrix with type: <string> cannot be converted into float<f32>. Please convert it manually before."),
            (anndata::data::DynCscMatrix::String(_), Precision::Double) => bail!("CscMatrix with type: <string> cannot be converted into float<f32>. Please convert it manually before."),
        },
        ArrayData::DataFrame(_) => todo!("Conversion with dataframes has not been implemented yet!"),
    };

    *data = new_matrix?;

    Ok(())
}

/// Convert CSR sparse matrix between numeric types.
///
/// Transforms a Compressed Sparse Row matrix from one numeric type to another while
/// preserving the sparse structure. Particularly useful for converting integer count
/// data to floating-point for analysis.
///
/// ## Parameters
/// * `matrix` - Source CSR matrix to convert
///
/// ## Type Parameters
/// * `T` - Source numeric type (e.g., i32, u32)
/// * `U` - Target floating-point type (f32 or f64)
///
/// ## Returns
/// New CSR matrix with converted values in target type
///
/// ## Preservation
/// - **Sparsity pattern**: Row offsets and column indices unchanged
/// - **Matrix dimensions**: Rows and columns preserved
/// - **Memory efficiency**: No unnecessary densification
fn convert_csr_sparse_matrix<T, U>(matrix: CsrMatrix<T>) -> anyhow::Result<CsrMatrix<U>>
where
    T: NumericOps + NumCast + Copy, // Base numeric traits for source
    U: NumericOps + NumCast + Copy + Float, // Ensure target is float (f32/f64)
{
    let nrows = matrix.nrows();
    let ncols = matrix.ncols();
    let (row_offsets, col_indices, values) = matrix.disassemble();

    let new_values: Vec<U> = values
        .into_iter()
        .map(|x| NumCast::from(x).unwrap())
        .collect();

    CsrMatrix::try_from_csr_data(nrows, ncols, row_offsets, col_indices, new_values)
        .map_err(|e| anyhow::anyhow!("Failed to create CSR matrix: {}", e))
}

/// Convert CSC sparse matrix between numeric types.
///
/// Transforms a Compressed Sparse Column matrix from one numeric type to another while
/// preserving the sparse structure. Similar to CSR conversion but for column-major format.
///
/// ## Parameters
/// * `matrix` - Source CSC matrix to convert
///
/// ## Type Parameters  
/// * `T` - Source numeric type (e.g., i32, u32)
/// * `U` - Target floating-point type (f32 or f64)
///
/// ## Returns
/// New CSC matrix with converted values in target type
fn convert_csc_sparse_matrix<T, U>(matrix: CscMatrix<T>) -> anyhow::Result<CscMatrix<U>>
where
    T: NumericOps + NumCast + Copy, // Base numeric traits for source
    U: NumericOps + NumCast + Copy + Float, // Ensure target is float (f32/f64)
{
    let nrows = matrix.nrows();
    let ncols = matrix.ncols();
    let (col_offsets, row_indices, values) = matrix.disassemble();

    let new_values: Vec<U> = values
        .into_iter()
        .map(|x| NumCast::from(x).unwrap())
        .collect();

    CscMatrix::try_from_csc_data(nrows, ncols, col_offsets, row_indices, new_values)
        .map_err(|e| anyhow::anyhow!("Failed to create CSC matrix: {}", e))
}

/// Convert dense ndarray between numeric types.
///
/// Transforms a dense array from one numeric type to another, preserving the
/// multidimensional structure. Handles dynamic dimensionality efficiently.
///
/// ## Parameters
/// * `array` - Source array with dynamic dimensions
///
/// ## Type Parameters
/// * `T` - Source numeric type
/// * `U` - Target floating-point type
///
/// ## Returns
/// New array with same shape but converted element type
fn convert_array<T, U>(
    array: ArrayBase<OwnedRepr<T>, Dim<IxDynImpl>>,
) -> anyhow::Result<ArrayBase<OwnedRepr<U>, Dim<IxDynImpl>>>
where
    T: NumericOps + NumCast + Copy,
    U: NumericOps + NumCast + Copy + Float,
{
    let shape = array.raw_dim(); // Keep the dynamic dimension

    let (vec, _) = array.into_raw_vec_and_offset();

    let new_values: Vec<U> = vec.into_iter().map(|x| NumCast::from(x).unwrap()).collect();

    Ok(ArrayBase::from_shape_vec(shape, new_values)?)
}

/// Create a Polars DataFrame from a HashMap of numeric vectors.
///
/// Converts analysis results stored as HashMap<String, Vec<T>> into a structured
/// DataFrame format suitable for storage in AnnData objects or further analysis.
/// Commonly used for differential expression results, QC metrics, and other
/// group-wise analysis outputs.
///
/// ## Parameters
/// * `map` - HashMap where keys are column names and values are data vectors
///
/// ## Type Parameters
/// * `T` - Numeric type that can be stored in Polars Series
///
/// ## Returns
/// Polars DataFrame with columns corresponding to HashMap keys
///
/// ## Usage Examples
/// ```rust,ignore
/// let mut results = HashMap::new();
/// results.insert("group_A_scores".to_string(), vec![1.5, 2.3, 0.8]);
/// results.insert("group_B_scores".to_string(), vec![0.2, 1.9, 2.1]);
///
/// let df = create_dataframe_from_map(&results)?;
/// // Results in DataFrame with columns "group_A_scores" and "group_B_scores"
/// ```
///
/// ## Common Use Cases
/// - Storing differential expression results by group
/// - Organizing QC metrics by sample/condition
/// - Converting analysis outputs for AnnData storage
/// - Preparing data for visualization or export
pub fn create_dataframe_from_map<T>(map: &HashMap<String, Vec<T>>) -> anyhow::Result<DataFrame>
where
    T: Clone,
    Series: NamedFromOwned<Vec<T>>,
{
    let mut df = DataFrame::default();

    for (group, values) in map {
        let ser = polars::prelude::Series::from_vec(group.into(), values.clone()).into_column();
        df.with_column(ser)?;
    }
    Ok(df)
}

/// Create a Polars DataFrame from a HashMap of String vectors.
///
/// Specialized version of DataFrame creation for string data, commonly used for
/// gene names, cell identifiers, and categorical analysis results.
///
/// ## Parameters
/// * `map` - HashMap where keys are column names and values are String vectors
///
/// ## Returns
/// Polars DataFrame with string columns
///
/// ## Usage Examples
/// ```rust,ignore
/// let mut gene_results = HashMap::new();
/// gene_results.insert("group_A_genes".to_string(),
///                    vec!["ACTB".to_string(), "GAPDH".to_string()]);
/// gene_results.insert("group_B_genes".to_string(),
///                    vec!["TP53".to_string(), "MYC".to_string()]);
///
/// let df = create_string_dataframe_from_map(&gene_results)?;
/// ```
///
/// ## Common Use Cases
/// - Storing gene names from differential expression analysis
/// - Organizing cell type annotations by group
/// - Creating lookup tables for identifiers
/// - Preparing categorical results for storage
pub fn create_string_dataframe_from_map(
    map: &HashMap<String, Vec<String>>,
) -> anyhow::Result<DataFrame> {
    let mut df = DataFrame::default();

    for (group, values) in map {
        let string_slice: Vec<&str> = values.iter().map(|s| s.as_str()).collect();
        let series = Series::new(group.into(), &string_slice);
        df.with_column(series)?;
    }

    Ok(df)
}

/// Convert a 2D array between numeric types.
///
/// Type-safe conversion between different numeric types for 2D arrays, commonly
/// used when interfacing between different libraries or precision requirements.
///
/// ## Parameters
/// * `array2` - Source 2D array to convert
///
/// ## Type Parameters
/// * `M` - Source numeric type
/// * `T` - Target numeric type
///
/// ## Returns
/// New 2D array with same dimensions but converted element type
///
/// ## Usage
/// ```rust,ignore
/// let f32_array: Array2<f32> = Array2::zeros((10, 5));
/// let f64_array: Array2<f64> = arr2_conversion(f32_array)?;
/// ```
///
/// ## Common Scenarios
/// - Converting PCA results between precision levels
/// - Interfacing with external libraries requiring specific types
/// - Preparing data for storage or analysis pipelines
pub fn arr2_conversion<M, T>(array2: Array2<M>) -> anyhow::Result<Array2<T>>
where
    M: Num + Copy + num_traits::ToPrimitive,
    T: Num + NumCast + Clone,
{
    let mut result = Array2::zeros(array2.dim());

    for (target, &source) in result.iter_mut().zip(array2.iter()) {
        *target = T::from(source).ok_or_else(|| anyhow!("Failed to convert value"))?;
    }

    Ok(result)
}

/// Convert a 1D array between numeric types.
///
/// Type-safe conversion for 1D arrays, useful for converting vectors of statistics,
/// scores, or other single-dimensional data between numeric types.
///
/// ## Parameters
/// * `array1` - Source 1D array to convert
///
/// ## Type Parameters
/// * `M` - Source numeric type
/// * `T` - Target numeric type
///
/// ## Returns
/// New 1D array with same length but converted element type
///
/// ## Usage
/// ```rust,ignore
/// let scores_f32: Array1<f32> = Array1::from_vec(vec![1.0, 2.0, 3.0]);
/// let scores_f64: Array1<f64> = arr1_conversion(scores_f32)?;
/// ```
///
/// ## Common Use Cases
/// - Converting statistical test results between precision levels
/// - Preparing vectors for different analysis functions
/// - Type compatibility between library interfaces
/// - Converting scores or weights for storage
pub fn arr1_conversion<M, T>(array1: Array1<M>) -> anyhow::Result<Array1<T>>
where
    M: Num + Copy + num_traits::ToPrimitive,
    T: Num + NumCast + Clone,
{
    let mut result = Array1::zeros(array1.dim());

    for (target, &source) in result.iter_mut().zip(array1.iter()) {
        *target = T::from(source).ok_or_else(|| anyhow!("Failed to convert value"))?;
    }

    Ok(result)
}