raphtory 0.17.0

raphtory, a temporal graph library
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
//! Module containing functions for loading CSV files into a graph.
//!
//! # Example
//! ```no_run
//! use std::path::{Path, PathBuf};
//! use tracing::{error, info};
//! use regex::Regex;
//! use raphtory::io::csv_loader::CsvLoader;
//! use raphtory::graph_loader::lotr_graph::Lotr;
//! use raphtory::prelude::*;
//! use raphtory::logging::global_info_logger;
//!  global_info_logger();
//!  let g = Graph::new();
//!  let csv_path: PathBuf = [env!("CARGO_MANIFEST_DIR"), "../../resource/"]
//!         .iter()
//!         .collect();
//!
//!  info!("path = {}", csv_path.as_path().to_str().unwrap());
//!  let csv_loader = CsvLoader::new(Path::new(&csv_path));
//!  let has_header = true;
//!  let r = Regex::new(r".+(lotr.csv)").unwrap();
//!  let delimiter = ",";
//!
//!  csv_loader
//!      .set_header(has_header)
//!      .set_delimiter(delimiter)
//!      .with_filter(r)
//!      .load_into_graph(&g, |lotr: Lotr, g: &Graph| {
//!          let src_id = lotr.src_id.id();
//!          let dst_id = lotr.dst_id.id();
//!          let time = lotr.time;
//!
//!          g.add_node(
//!              time,
//!              src_id,
//!              [("name", Prop::str("Character"))],
//!              None,
//!          )
//!          .map_err(|err| error!("{:?}", err))
//!          .ok();
//!          g.add_node(
//!              time,
//!              dst_id,
//!              [("name", Prop::str("Character"))],
//!              None,
//!          )
//!          .map_err(|err| error!("{:?}", err))
//!          .ok();
//!          g.add_edge(
//!              time,
//!              src_id,
//!              dst_id,
//!              [(
//!                  "name",
//!                  Prop::str("Character Co-occurrence"),
//!              )],
//!              None,
//!          ).expect("Failed to add edge");
//!      })
//!      .expect("Csv did not parse.");
//! ```
//!

/// Module for loading CSV files into a graph.
use bzip2::read::BzDecoder;
use csv::StringRecord;
use flate2; // 1.0
use flate2::read::GzDecoder;
use rayon::prelude::*;
use regex::Regex;
use serde::de::DeserializeOwned;
use std::{
    collections::VecDeque,
    error::Error,
    fmt::{Debug, Display, Formatter},
    fs,
    fs::File,
    io,
    io::BufReader,
    path::{Path, PathBuf},
};
use tracing::info;

#[derive(Debug)]
pub enum CsvErr {
    /// An IO error that occurred during file read.
    IoError(io::Error),
    /// A CSV parsing error that occurred while parsing the CSV data.
    CsvError(csv::Error),
}

impl From<io::Error> for CsvErr {
    fn from(value: io::Error) -> Self {
        Self::IoError(value)
    }
}

impl From<csv::Error> for CsvErr {
    fn from(value: csv::Error) -> Self {
        Self::CsvError(value)
    }
}

impl Display for CsvErr {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self.source() {
            Some(error) => write!(f, "CSV loader failed with error: {}", error),
            None => write!(f, "CSV loader failed with unknown error"),
        }
    }
}

impl Error for CsvErr {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            CsvErr::IoError(error) => Some(error),
            CsvErr::CsvError(error) => Some(error),
        }
    }
}

/// A struct that defines the CSV loader with configurable options.
#[derive(Debug)]
pub struct CsvLoader {
    /// Path of the CSV file or directory containing CSV files.
    path: PathBuf,
    /// Optional regex filter to select specific CSV files by name.
    regex_filter: Option<Regex>,
    /// Specifies whether the CSV file has a header.
    header: bool,
    /// The delimiter character used in the CSV file.
    delimiter: u8,
    /// print the name of the file being loaded
    print_file_name: bool,
}

impl CsvLoader {
    /// Creates a new `CsvLoader` instance with the specified file path.
    ///
    /// # Arguments
    ///
    /// * `p` - A path of the CSV file or directory containing CSV files.
    ///
    /// # Example
    ///
    /// ```no_run
    ///
    /// use raphtory::io::csv_loader::CsvLoader;
    /// let loader = CsvLoader::new("/path/to/csv_file.csv");
    /// ```
    pub fn new<P: Into<PathBuf>>(p: P) -> Self {
        Self {
            path: p.into(),
            regex_filter: None,
            header: false,
            delimiter: b',',
            print_file_name: false,
        }
    }

    /// Sets whether the CSV file has a header.
    ///
    /// # Arguments
    ///
    /// * `h` - A boolean value indicating whether the CSV file has a header.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use raphtory::io::csv_loader::CsvLoader;
    /// let loader = CsvLoader::new("/path/to/csv_file.csv").set_header(true);
    /// ```
    pub fn set_header(mut self, h: bool) -> Self {
        self.header = h;
        self
    }

    /// If set to true will print the file name as it reads it
    ///
    /// # Arguments
    ///
    /// * `p` - A boolean value indicating whether the CSV file has a header.
    ///
    /// # Example
    /// ```no_run
    /// use raphtory::io::csv_loader::CsvLoader;
    /// let loader = CsvLoader::new("/path/to/csv_file.csv").set_print_file_name(true);
    /// ```
    pub fn set_print_file_name(mut self, p: bool) -> Self {
        self.print_file_name = p;
        self
    }

    /// Sets the delimiter character used in the CSV file.
    ///
    /// # Arguments
    ///
    /// * `d` - A string containing the delimiter character.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use raphtory::io::csv_loader::CsvLoader;
    /// let loader = CsvLoader::new("/path/to/csv_file.csv").set_delimiter("|");
    /// ```
    pub fn set_delimiter(mut self, d: &str) -> Self {
        self.delimiter = d.as_bytes()[0];
        self
    }

    /// Sets the regex filter to select specific CSV files by name.
    ///
    /// # Arguments
    ///
    /// * `r` - A regex pattern to filter CSV files by name.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use regex::Regex;
    /// use raphtory::io::csv_loader::CsvLoader;
    ///
    /// let loader = CsvLoader::new("/path/to/csv_files")
    ///    .with_filter(Regex::new(r"file_name_pattern").unwrap());
    /// ```
    pub fn with_filter(mut self, r: Regex) -> Self {
        self.regex_filter = Some(r);
        self
    }

    /// Check if the provided path is a directory or not.
    ///
    /// # Arguments
    ///
    /// * `p` - A reference to the path to be checked.
    ///
    /// Returns:
    ///
    /// A Result containing a boolean value indicating whether the path is a directory or not.
    ///
    /// # Errors
    ///
    /// An error of type CsvErr is returned if an I/O error occurs while checking the path.
    ///
    fn is_dir<P: AsRef<Path>>(p: &P) -> Result<bool, CsvErr> {
        Ok(fs::metadata(p)?.is_dir())
    }

    /// Check if a file matches the specified regex pattern and add it to the provided vector of paths.
    ///
    /// # Arguments
    ///
    /// * `path` - The path to the file to be checked.
    /// * `paths` - A mutable reference to the vector of paths where the file should be added.
    ///
    /// Returns:
    ///
    /// Nothing is returned, the function only modifies the provided vector of paths.
    ///
    fn accept_file<P: Into<PathBuf>>(&self, path: P, paths: &mut Vec<PathBuf>) {
        let p: PathBuf = path.into();
        // this is an actual file so push it into the paths vec if it matches the pattern
        if let Some(pattern) = &self.regex_filter {
            let is_match = &p
                .to_str()
                .filter(|file_name| pattern.is_match(file_name))
                .is_some();
            if *is_match {
                paths.push(p);
            }
        } else {
            paths.push(p)
        }
    }

    /// Traverse the directory recursively and return a vector of paths to all files in the directory.
    ///
    /// # Arguments
    ///
    /// * No arguments are required.
    ///
    /// Returns:
    ///
    /// A Result containing a vector of PathBuf objects representing the paths to all files in the directory.
    ///
    /// # Errors
    ///
    /// An error of type CsvErr is returned if an I/O error occurs while reading the directory.
    ///
    fn files_vec(&self) -> Result<Vec<PathBuf>, CsvErr> {
        let mut paths = vec![];
        let mut queue = VecDeque::from([self.path.to_path_buf()]);

        while let Some(ref path) = queue.pop_back() {
            match fs::read_dir(path) {
                Ok(entries) => {
                    for f_path in entries.flatten() {
                        let p = f_path.path();
                        if Self::is_dir(&p)? {
                            queue.push_back(p.clone())
                        } else {
                            self.accept_file(f_path.path(), &mut paths);
                        }
                    }
                }
                Err(err) => {
                    if !Self::is_dir(path)? {
                        self.accept_file(path.to_path_buf(), &mut paths);
                    } else {
                        return Err(err.into());
                    }
                }
            }
        }

        Ok(paths)
    }

    /// Load data from all CSV files in the directory into a graph.
    ///
    /// # Arguments
    ///
    /// * `g` - A reference to the graph object where the data should be loaded.
    /// * `loader` - A closure that takes a deserialized record and the graph object as arguments and adds the record to the graph.
    ///
    /// Returns:
    ///
    /// A Result containing an empty Ok value if the data is loaded successfully.
    ///
    /// # Errors
    ///
    /// An error of type CsvErr is returned if an I/O error occurs while reading the files or parsing the CSV data.
    ///
    pub fn load_into_graph<F, REC, G>(&self, g: &G, loader: F) -> Result<(), CsvErr>
    where
        REC: DeserializeOwned + Debug,
        F: Fn(REC, &G) + Send + Sync,
        G: Sync,
    {
        //FIXME: loader function should return a result for reporting parsing errors
        let paths = self.files_vec()?;
        paths
            .par_iter()
            .try_for_each(move |path| self.load_file_into_graph(path, g, &loader))?;
        Ok(())
    }

    /// Load data from all CSV files in the directory into a graph.
    ///
    /// # Arguments
    ///
    /// * `g` - A reference to the graph object where the data should be loaded.
    /// * `loader` - A closure that takes a deserialized record and the graph object as arguments and adds the record to the graph.
    ///
    /// Returns:
    ///
    /// A Result containing an empty Ok value if the data is loaded successfully.
    ///
    /// # Errors
    ///
    /// An error of type CsvErr is returned if an I/O error occurs while reading the files or parsing the CSV data.
    ///
    pub fn load_rec_into_graph<F, G>(&self, g: &G, loader: F) -> Result<(), CsvErr>
    where
        F: Fn(StringRecord, &G) + Send + Sync,
        G: Sync,
    {
        //FIXME: loader function should return a result for reporting parsing errors
        let paths = self.files_vec()?;
        paths
            .par_iter()
            .try_for_each(move |path| self.load_file_into_graph_record(path, g, &loader))?;
        Ok(())
    }

    /// Loads a CSV file into a graph using the specified loader function.
    ///
    /// # Arguments
    ///
    /// * `path` - The path to the CSV file to load.
    /// * `g` - A reference to the graph to load the data into.
    /// * `loader` - The function to use for loading the CSV records into the graph.
    ///
    /// Returns:
    ///
    /// Returns `Ok(())` if the operation was successful, or a `CsvErr` if there was an error.
    ///
    fn load_file_into_graph<F, REC, P: Into<PathBuf> + Debug, G>(
        &self,
        path: P,
        g: &G,
        loader: &F,
    ) -> Result<(), CsvErr>
    where
        REC: DeserializeOwned + Debug,
        F: Fn(REC, &G),
    {
        let file_path: PathBuf = path.into();
        if self.print_file_name {
            info!("Loading file: {:?}", file_path);
        }
        let mut csv_reader = self.csv_reader(file_path)?;
        let records_iter = csv_reader.deserialize::<REC>();

        //TODO this needs better error handling for files without perfect data
        for rec in records_iter {
            let record = rec.expect("Failed to deserialize");
            loader(record, g)
        }

        Ok(())
    }

    fn load_file_into_graph_record<F, P: Into<PathBuf> + Debug, G>(
        &self,
        path: P,
        g: &G,
        loader: &F,
    ) -> Result<(), CsvErr>
    where
        F: Fn(StringRecord, &G),
    {
        let file_path: PathBuf = path.into();

        let mut csv_reader = self.csv_reader(file_path)?;
        for rec in csv_reader.records() {
            let record = rec?;
            loader(record, g)
        }

        Ok(())
    }

    /// Returns a `csv::Reader` for the specified file path, automatically detecting and handling gzip and bzip compression.
    ///
    /// # Arguments
    ///
    /// * `file_path` - The path to the CSV file to read.
    ///
    /// Returns:
    ///
    /// Returns a `csv::Reader` that can be used to read the CSV file, or a `CsvErr` if there was an error.
    ///
    fn csv_reader(&self, file_path: PathBuf) -> Result<csv::Reader<Box<dyn io::Read>>, CsvErr> {
        let is_gziped = file_path
            .file_name()
            .and_then(|name| name.to_str())
            .filter(|name| name.ends_with(".gz"))
            .is_some();

        let is_bziped = file_path
            .file_name()
            .and_then(|name| name.to_str())
            .filter(|name| name.ends_with(".bz2"))
            .is_some();

        let f = File::open(&file_path)?;
        if is_gziped {
            Ok(csv::ReaderBuilder::new()
                .has_headers(self.header)
                .delimiter(self.delimiter)
                .from_reader(Box::new(BufReader::new(GzDecoder::new(f)))))
        } else if is_bziped {
            Ok(csv::ReaderBuilder::new()
                .has_headers(self.header)
                .delimiter(self.delimiter)
                .from_reader(Box::new(BufReader::new(BzDecoder::new(f)))))
        } else {
            Ok(csv::ReaderBuilder::new()
                .has_headers(self.header)
                .delimiter(self.delimiter)
                .from_reader(Box::new(f)))
        }
    }
}

#[cfg(test)]
mod csv_loader_test {
    use crate::{io::csv_loader::CsvLoader, prelude::*};
    use csv::StringRecord;
    use raphtory_api::core::utils::logging::global_info_logger;
    use regex::Regex;
    use serde::Deserialize;
    use std::path::{Path, PathBuf};
    use tracing::error;

    #[test]
    fn regex_match() {
        let r = Regex::new(r".+address").unwrap();
        // todo: move file path to data module
        let text = "bitcoin/address_000000000001.csv.gz";
        assert!(r.is_match(text));
        let text = "bitcoin/received_000000000001.csv.gz";
        assert!(!r.is_match(text));
    }

    #[test]
    fn regex_match_2() {
        let r = Regex::new(r".+(sent|received)").unwrap();
        // todo: move file path to data module
        let text = "bitcoin/sent_000000000001.csv.gz";
        assert!(r.is_match(text));
        let text = "bitcoin/received_000000000001.csv.gz";
        assert!(r.is_match(text));
        let text = "bitcoin/address_000000000001.csv.gz";
        assert!(!r.is_match(text));
    }

    #[derive(Deserialize, std::fmt::Debug)]
    pub struct Lotr {
        src_id: String,
        dst_id: String,
        time: i64,
    }

    fn lotr_test(g: Graph, csv_loader: CsvLoader, has_header: bool, delimiter: &str, r: Regex) {
        csv_loader
            .set_header(has_header)
            .set_delimiter(delimiter)
            .with_filter(r)
            .load_into_graph(&g, |lotr: Lotr, g: &Graph| {
                let src_id = lotr.src_id.id();
                let dst_id = lotr.dst_id.id();
                let time = lotr.time;

                g.add_node(time, src_id, [("name", Prop::str("Character"))], None)
                    .map_err(|err| error!("{:?}", err))
                    .ok();
                g.add_node(time, dst_id, [("name", Prop::str("Character"))], None)
                    .map_err(|err| error!("{:?}", err))
                    .ok();
                g.add_edge(
                    time,
                    src_id,
                    dst_id,
                    [("name", Prop::str("Character Co-occurrence"))],
                    None,
                )
                .unwrap();
            })
            .expect("Csv did not parse.");
    }

    fn lotr_test_rec(g: Graph, csv_loader: CsvLoader, has_header: bool, delimiter: &str, r: Regex) {
        csv_loader
            .set_header(has_header)
            .set_delimiter(delimiter)
            .with_filter(r)
            .load_rec_into_graph(&g, |lotr: StringRecord, g: &Graph| {
                let src_id = lotr.get(0).map(|s| s.id()).unwrap();
                let dst_id = lotr.get(1).map(|s| s.id()).unwrap();
                let time = lotr.get(2).map(|s| s.parse::<i64>().unwrap()).unwrap();

                g.add_node(time, src_id, [("name", Prop::str("Character"))], None)
                    .map_err(|err| error!("{:?}", err))
                    .ok();
                g.add_node(time, dst_id, [("name", Prop::str("Character"))], None)
                    .map_err(|err| error!("{:?}", err))
                    .ok();
                g.add_edge(
                    time,
                    src_id,
                    dst_id,
                    [("name", Prop::str("Character Co-occurrence"))],
                    None,
                )
                .unwrap();
            })
            .expect("Csv did not parse.");
    }

    #[test]
    fn test_headers_flag_and_delimiter() {
        global_info_logger();
        let g = Graph::new();
        // todo: move file path to data module
        let csv_path: PathBuf = [env!("CARGO_MANIFEST_DIR"), "../resource/"]
            .iter()
            .collect();

        let csv_loader = CsvLoader::new(Path::new(&csv_path));
        let has_header = true;
        let r = Regex::new(r".+(lotr.csv)").unwrap();
        let delimiter = ",";
        lotr_test(g, csv_loader, has_header, delimiter, r);
        let g = Graph::new();
        let csv_loader = CsvLoader::new(Path::new(&csv_path));
        let r = Regex::new(r".+(lotr.csv)").unwrap();
        lotr_test_rec(g, csv_loader, has_header, delimiter, r);
    }

    #[test]
    #[should_panic]
    fn test_wrong_header_flag_file_with_header() {
        global_info_logger();
        let g = Graph::new();
        // todo: move file path to data module
        let csv_path: PathBuf = [env!("CARGO_MANIFEST_DIR"), "../../resource/"]
            .iter()
            .collect();
        let csv_loader = CsvLoader::new(Path::new(&csv_path));
        let has_header = false;
        let r = Regex::new(r".+(lotr.csv)").unwrap();
        let delimiter = ",";
        lotr_test(g, csv_loader, has_header, delimiter, r);
    }

    #[test]
    #[should_panic]
    fn test_flag_has_header_but_file_has_no_header() {
        global_info_logger();
        let g = Graph::new();
        // todo: move file path to data module
        let csv_path: PathBuf = [env!("CARGO_MANIFEST_DIR"), "../../resource/"]
            .iter()
            .collect();
        let csv_loader = CsvLoader::new(Path::new(&csv_path));
        let has_header = true;
        let r = Regex::new(r".+(lotr-without-header.csv)").unwrap();
        let delimiter = ",";
        lotr_test(g, csv_loader, has_header, delimiter, r);
    }

    #[test]
    #[should_panic]
    fn test_wrong_header_names() {
        global_info_logger();
        let g = Graph::new();
        // todo: move file path to data module
        let csv_path: PathBuf = [env!("CARGO_MANIFEST_DIR"), "../../resource/"]
            .iter()
            .collect();
        let csv_loader = CsvLoader::new(Path::new(&csv_path));
        let r = Regex::new(r".+(lotr-wrong.csv)").unwrap();
        let has_header = true;
        let delimiter = ",";
        lotr_test(g, csv_loader, has_header, delimiter, r);
    }

    #[test]
    #[should_panic]
    fn test_wrong_delimiter() {
        global_info_logger();
        let g = Graph::new();
        // todo: move file path to data module
        let csv_path: PathBuf = [env!("CARGO_MANIFEST_DIR"), "../../resource/"]
            .iter()
            .collect();
        let csv_loader = CsvLoader::new(Path::new(&csv_path));
        let r = Regex::new(r".+(lotr.csv)").unwrap();
        let has_header = true;
        let delimiter = ".";
        lotr_test(g, csv_loader, has_header, delimiter, r);
    }
}