pub fn write_data_nearest(
        &self,
        som: &Som,
        data: &DataFrame,
        path: &str,
    ) -> Result<(), Box<dyn Error>> {
        let mut classes: Vec<Option<Vec<String>>> = vec![None; self.layers.len()];
        let mut denorm: Vec<Option<DataFrame>> = (0..self.layers.len()).map(|_| None).collect();

        let nearest = self.nearest_unit(&som, data);

        let mut names: Vec<String> = self.preserve_columns.clone();
        let offset_preserved = names.len();

        for (idx, layer) in som.params().layers().iter().enumerate() {
            if layer.categorical() {
                let (name, cl) = self.to_class(&som, data, idx).unwrap();
                classes[idx] = Some(cl);
                names.push(name);
            } else {
                let result = self.to_denormalized(&som, data, idx).unwrap();
                names.extend_from_slice(&result.columns());
                denorm[idx] = Some(result);
            }
        }

        let offset = names.len();
        names.extend_from_slice(&[
            "som_index".to_string(),
            "som_row".to_string(),
            "som_col".to_string(),
        ]);

        let no_data = &self.csv_options.no_data;

        let mut writer = WriterBuilder::new()
            .delimiter(self.csv_options.delimiter)
            .from_path(path)?;

        let mut row = vec!["".to_string(); names.len()];
        writer.write_record(&names)?;
        for index in 0..data.nrows() {
            for (idx, vec) in self.preserved.iter().enumerate() {
                row[idx] = vec[index].clone();
            }
            for (idx, (layer, start_col)) in som
                .params()
                .layers()
                .iter()
                .zip(som.params().start_columns())
                .enumerate()
            {
                if layer.categorical() {
                    let cls = classes[idx].as_ref().unwrap();
                    let v = &cls[index];
                    row[*start_col + offset_preserved] = v.clone();
                } else {
                    let df = denorm[idx].as_ref().unwrap();
                    let df_row = df.get_row(index);
                    for i in 0..df_row.len() {
                        let v = df_row[i];
                        row[*start_col + offset_preserved + i] = if v.is_nan() {
                            no_data.clone()
                        } else {
                            v.to_string()
                        };
                    }
                }
            }
            let (near, _dist) = nearest[index];
            let (r, c) = som.to_row_col(near);
            row[offset] = near.to_string();
            row[offset + 1] = r.to_string();
            row[offset + 2] = c.to_string();

            writer.write_record(&row)?;
        }

        Ok(())
    }
/// Writes `data`, amended by the nearest SOM unit index, row and column, to a CSV file.
Writes `data`, amended by the nearest SOM unit index, row and column, to a CSV file.
Writes ` data ` amended by the nearest SOM unit index row and column to a CSV file .
pub fn write_data_nearest(
        &self,
        som: &Som,
        data: &DataFrame,
        path: &str,
    ) -> Result<(), Box<dyn Error>> {
        let mut classes: Vec<Option<Vec<String>>> = vec![None; self.layers.len()];
        let mut denorm: Vec<Option<DataFrame>> = (0..self.layers.len()).map(|_| None).collect();
        let nearest = self.nearest_unit(&som, data);
        let mut names: Vec<String> = self.preserve_columns.clone();
        let offset_preserved = names.len();
        for (idx, layer) in som.params().layers().iter().enumerate() {
            if layer.categorical() {
                let (name, cl) = self.to_class(&som, data, idx).unwrap();
                classes[idx] = Some(cl);
                names.push(name);
            } else {
                let result = self.to_denormalized(&som, data, idx).unwrap();
                names.extend_from_slice(&result.columns());
                denorm[idx] = Some(result);
            }
        }
        let offset = names.len();
        names.extend_from_slice(&[
            "som_index".to_string(),
            "som_row".to_string(),
            "som_col".to_string(),
        ]);
        let no_data = &self.csv_options.no_data;
        let mut writer = WriterBuilder::new()
            .delimiter(self.csv_options.delimiter)
            .from_path(path)?;
        let mut row = vec!["".to_string(); names.len()];
        writer.write_record(&names)?;
        for index in 0..data.nrows() {
            for (idx, vec) in self.preserved.iter().enumerate() {
                row[idx] = vec[index].clone();
            }
            for (idx, (layer, start_col)) in som
                .params()
                .layers()
                .iter()
                .zip(som.params().start_columns())
                .enumerate()
            {
                if layer.categorical() {
                    let cls = classes[idx].as_ref().unwrap();
                    let v = &cls[index];
                    row[*start_col + offset_preserved] = v.clone();
                } else {
                    let df = denorm[idx].as_ref().unwrap();
                    let df_row = df.get_row(index);
                    for i in 0..df_row.len() {
                        let v = df_row[i];
                        row[*start_col + offset_preserved + i] = if v.is_nan() {
                            no_data.clone()
                        } else {
                            v.to_string()
                        };
                    }
                }
            }
            let (near, _dist) = nearest[index];
            let (r, c) = som.to_row_col(near);
            row[offset] = near.to_string();
            row[offset + 1] = r.to_string();
            row[offset + 2] = c.to_string();
            writer.write_record(&row)?;
        }
        Ok(())
    }
pub fn write_data_nearest ( & self , som : & Som , data : & DataFrame , path : & str , ) -> Result < ( ) , Box < dyn Error > > { let mut classes : Vec < Option < Vec < String > > > = vec ! [ None self layers len ( ) ] ; let mut denorm : Vec < Option < DataFrame > > = ( 0 .. self . layers . len ( ) ) . map ( | _ | None ) . collect ( ) ; let nearest = self . nearest_unit ( & som , data ) ; let mut names : Vec < String > = self . preserve_columns . clone ( ) ; let offset_preserved = names . len ( ) ; for ( idx , layer ) in som . params ( ) . layers ( ) . iter ( ) . enumerate ( ) { if layer . categorical ( ) { let ( name , cl ) = self . to_class ( & som , data , idx ) . unwrap ( ) ; classes [ idx ] = Some ( cl ) ; names . push ( name ) ; } else { let result = self . to_denormalized ( & som , data , idx ) . unwrap ( ) ; names . extend_from_slice ( & result . columns ( ) ) ; denorm [ idx ] = Some ( result ) ; } } let offset = names . len ( ) ; names . extend_from_slice ( & [ " " . to_string ( ) , " " . to_string ( ) , " " . to_string ( ) , ] ) ; let no_data = & self . csv_options . no_data ; let mut writer = WriterBuilder :: new ( ) . delimiter ( self . csv_options . delimiter ) . from_path ( path ) ? ; let mut row = vec ! [ " " to_string ( ) names len ( ) ] ; writer . write_record ( & names ) ? ; for index in 0 .. data . nrows ( ) { for ( idx , vec ) in self . preserved . iter ( ) . enumerate ( ) { row [ idx ] = vec [ index ] . clone ( ) ; } for ( idx , ( layer , start_col ) ) in som . params ( ) . layers ( ) . iter ( ) . zip ( som . params ( ) . start_columns ( ) ) . enumerate ( ) { if layer . categorical ( ) { let cls = classes [ idx ] . as_ref ( ) . unwrap ( ) ; let v = & cls [ index ] ; row [ * start_col + offset_preserved ] = v . clone ( ) ; } else { let df = denorm [ idx ] . as_ref ( ) . unwrap ( ) ; let df_row = df . get_row ( index ) ; for i in 0 .. df_row . len ( ) { let v = df_row [ i ] ; row [ * start_col + offset_preserved + i ] = if v . is_nan ( ) { no_data . clone ( ) } else { v . to_string ( ) } ; } } } let ( near , _dist ) = nearest [ index ] ; let ( r , c ) = som . to_row_col ( near ) ; row [ offset ] = near . to_string ( ) ; row [ offset + 1 ] = r . to_string ( ) ; row [ offset + 2 ] = c . to_string ( ) ; writer . write_record ( & row ) ? ; } Ok ( ( ) ) }
Writes `data`, amended by the nearest SOM unit index, row and column, to a CSV file.
Writes ` data ` amended by the nearest SOM unit index row and column to a CSV file .
&self
som
&Som
data
&DataFrame
path
&str
&self
som
&Som
data
&DataFrame
path
&str