-
Notifications
You must be signed in to change notification settings - Fork 0
/
exportCSV.js
140 lines (128 loc) · 3.72 KB
/
exportCSV.js
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
const fs = require('fs');
const Promise = require('bluebird');
const path = require('path');
const argv = require('minimist')(process.argv.slice(2));
const _ = require('underscore');
const os = require('os');
const xnat = require("./index.js");
const json2csv = require('json2csv');
const help = function(){
console.error("Export all scans and annotations from a project to a CSV file");
console.error("Usage: node", process.argv[1],"-p <project id>");
console.error("Options:");
console.error("-p <project id>, project id in XNAT to upload the files.");
console.error("--out <filename to write data in csv format, if not provided will print to console>");
console.error("--nrrd <Export only images that are located in the NRRD resource>");
}
if(!argv["p"] || argv["h"] || argv["help"]){
help();
process.exit(1);
}
var projectid = argv["p"];
var outfilename = argv["out"];
var nrrd_resource = argv["nrrd"];
xnat.start()
.then(function(res){
return xnat.getSubjects(projectid);
})
.then(function(res){
return Promise.map(res, function(subject){
return xnat.getSubjectExperiments(projectid, subject.ID)
.then(function(experiments){
return Promise.map(experiments.ResultSet.Result, function(exp){
return xnat.getScans(projectid, subject.ID, exp.ID)
.then(function(subjectExperiments){
if(!nrrd_resource){
return _.map(subjectExperiments, function(scans){
return {
subjectid: subject.ID,
subject_label: subject.label,
expid: exp.ID,
exp_label: exp.label,
scanid: scans.ID,
series_description: scans.series_description,
quality: scans.quality,
note: scans.note
}
});
}else{
return Promise.map(subjectExperiments, function(scans){
return xnat.getScanFiles(projectid, subject.ID, exp.ID, scans.ID)
.then(function(scanfiles){
var scanfilesfound = _.map(scanfiles, function(sfile){
return {
subjectid: subject.ID,
subject_label: subject.label,
expid: exp.ID,
exp_label: exp.label,
scanid: scans.ID,
series_description: scans.series_description,
quality: scans.quality,
note: scans.note,
name: sfile.Name,
uri: sfile.URI,
collection: sfile.collection,
path: path.join(exp.label, 'SCANS', scans.ID, 'NRRD', sfile.Name)
}
});
return _.filter(scanfilesfound, function(sfile){
return (sfile.collection.indexOf("NRRD") != -1 && (sfile.name.indexOf(".nrrd") != -1 || sfile.name.indexOf(".nii.gz") != -1 || sfile.name.indexOf(".nii") != -1))
});
})
.catch(function(e){
console.error("getScanFiles", projectid, subject.ID, exp.ID, scans.ID);
console.error(e);
return null;
});
}, {
concurrency: 1
});
}
})
.catch(function(e){
console.error("getScans", projectid, subject.ID, exp.ID);
console.error(e);
return null;
});
}, {
concurrency: 1
});
})
.catch(function(e){
console.error("getSubjectExperiments", projectid, subject.ID);
console.error(e);
return null;
});
}, {
concurrency: 1
})
.then(function(allscans){
allscans = _.compact(_.flatten(allscans));
csvout = '';
if(allscans.length > 0){
var fields = _.keys(allscans[0]);
csvout = json2csv({
data: allscans,
fields: fields
});
}
if(outfilename){
console.log("Writing:", outfilename);
fs.writeFileSync(outfilename, csvout);
}else{
console.log(csvout);
}
})
.catch(function(e){
console.error("map ids");
console.error(e);
return null;
});
})
.then(function(){
return xnat.logout();
})
.catch(function(e){
console.error(e);
return xnat.logout();
})