/
functions.go
65 lines (53 loc) · 1.35 KB
/
functions.go
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
package main
import (
"encoding/csv"
"fmt"
"os"
)
// Via https://flaviocopes.com/go-list-files/
func scanFolder(dirname string) ([]os.FileInfo, error) {
f, err := os.Open(dirname)
if err != nil {
return nil, err
}
files, err := f.Readdir(-1)
f.Close()
if err != nil {
return nil, err
}
return files, nil
}
func getDicomSlice(manifest string) ([]string, error) {
f, err := os.Open(manifest)
if err != nil {
return nil, err
}
defer f.Close()
csvReader := csv.NewReader(f)
csvReader.Comma = '\t'
entries, err := csvReader.ReadAll()
if err != nil {
return nil, err
}
dicomFileCol := -1
// First, identify whether we are extracting multiple images from any zips.
// If so, it will be more efficient to open the zip one time and extract the
// desired images, rather than opening/closing the zip for each image
// (especially if over gcsfuse)
dicomSlice := make([]string, 0, len(entries)) // []dicom_filename
for i, row := range entries {
if i == 0 {
for j, col := range row {
if col == "dicom_file" {
dicomFileCol = j
}
}
continue
} else if dicomFileCol < 0 {
return nil, fmt.Errorf("Did not identify dicom_file in the header line of %s", manifest)
}
// Append to this zip file's list of individual dicom images to process
dicomSlice = append(dicomSlice, row[dicomFileCol])
}
return dicomSlice, nil
}