-
Notifications
You must be signed in to change notification settings - Fork 0
/
io_utils.go
306 lines (287 loc) · 6.65 KB
/
io_utils.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
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
package src
import (
"bufio"
"fmt"
"github.com/gonum/matrix/mat64"
"log"
"os"
"runtime"
"runtime/pprof"
"strconv"
"strings"
)
func ReadNetwork(inNetworkFile string) (network *mat64.Dense, idIdx map[string]int, idxToId map[int]string) {
idIdx, idxToId, nID := idIdxGen(inNetworkFile)
network = mat64.NewDense(nID, nID, nil)
//file
file, err := os.Open(inNetworkFile)
if err != nil {
return
}
//load
br := bufio.NewReaderSize(file, 32768000)
for {
line, isPrefix, err := br.ReadLine()
if err != nil {
break
}
if isPrefix {
return
}
str := string(line)
elements := strings.Split(str, "\t")
a := idIdx[elements[0]]
b := idIdx[elements[1]]
v, _ := strconv.ParseFloat(elements[2], 64)
network.Set(a, b, v)
network.Set(b, a, v)
}
return network, idIdx, idxToId
}
func idIdxGen(inNetworkFile string) (idIdx map[string]int, idxToId map[int]string, count int) {
idIdx = make(map[string]int)
idxToId = make(map[int]string)
//file
file, err := os.Open(inNetworkFile)
if err != nil {
return
}
//load
br := bufio.NewReaderSize(file, 32768000)
count = 0
for {
line, isPrefix, err := br.ReadLine()
if err != nil {
break
}
if isPrefix {
return
}
str := string(line)
elements := strings.Split(str, "\t")
for c, id := range elements {
if c < 2 {
_, exist := idIdx[id]
if !exist {
idIdx[id] = count
idxToId[count] = id
count += 1
}
}
}
}
//count +1, so that it is number of unique IDs
//count += 1
return idIdx, idxToId, count
}
func ReadIDfile(inFile string) (rName []string) {
rName = make([]string, 0)
//file
file, err := os.Open(inFile)
if err != nil {
return
}
defer file.Close()
//load
br := bufio.NewReaderSize(file, 32768000)
for {
line, isPrefix, err1 := br.ReadLine()
if err1 != nil {
break
}
if isPrefix {
return
}
str := string(line)
elements := strings.Split(str, "\t")
value := Shift(&elements)
rName = append(rName, value)
}
return rName
}
func ReadFile(inFile string, rowName bool, colName bool) (dataR *mat64.Dense, rName []string, cName []string, err error) {
//init
lc, cc, _ := lcCount(inFile)
if rowName {
cc -= 1
}
if colName {
lc -= 1
}
data := mat64.NewDense(lc, cc, nil)
rName = make([]string, 0)
cName = make([]string, 0)
//file
file, err := os.Open(inFile)
if err != nil {
return
}
defer file.Close()
//load
br := bufio.NewReaderSize(file, 32768000)
r := 0
touchCol := false
for {
line, isPrefix, err1 := br.ReadLine()
if err1 != nil {
break
}
if isPrefix {
return
}
str := string(line)
elements := strings.Split(str, "\t")
if rowName {
value := Shift(&elements)
rName = append(rName, value)
}
//first element already shifted if rowName is true
if colName && !touchCol {
cName = elements
touchCol = true
} else {
for c, i := range elements {
j, _ := strconv.ParseFloat(i, 64)
data.Set(r, c, j)
}
r++
}
}
//shfit first rowName away if colName exist
if colName && rowName {
Shift(&rName)
}
return data, rName, cName, nil
}
//line count(nRow) and column count(nCol) for a tab separeted txt
func lcCount(filename string) (lc int, cc int, err error) {
lc = 0
cc = 0
touch := true
file, err := os.Open(filename)
if err != nil {
return
}
defer file.Close()
//load
br := bufio.NewReaderSize(file, 32768000)
for {
line, isPrefix, err1 := br.ReadLine()
if err1 != nil {
break
}
if isPrefix {
return
}
if touch {
cc = strings.Count(string(line), "\t")
cc += 1
touch = false
}
lc++
}
return lc, cc, nil
}
func WriteFile(outFile string, data *mat64.Dense, name []string, isRowID bool) (err error) {
file, err := os.OpenFile(outFile, os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
fmt.Println(err)
return
}
defer file.Close()
file.Truncate(0)
file.Seek(0, 0)
wr := bufio.NewWriterSize(file, 192000)
nRow, nCol := data.Caps()
var ele string
for i := 0; i < nRow; i++ {
if isRowID {
wr.WriteString(name[i])
wr.WriteString("\t")
}
ele = strconv.FormatFloat(data.At(i, 0), 'f', 6, 64)
wr.WriteString(ele)
for j := 1; j < nCol; j++ {
ele = strconv.FormatFloat(data.At(i, j), 'f', 6, 64)
wr.WriteString("\t")
wr.WriteString(ele)
}
wr.WriteString("\n")
}
wr.Flush()
return err
}
func WriteNetwork(outFile string, data *mat64.Dense, idxToId map[int]string) (err error) {
file, err := os.OpenFile(outFile, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
fmt.Println(err)
return
}
defer file.Close()
file.Truncate(0)
file.Seek(0, 0)
wr := bufio.NewWriterSize(file, 192000)
nRow, nCol := data.Caps()
var ele string
for i := 0; i < nRow; i++ {
for j := i + 1; j < nCol; j++ {
if data.At(i, j) > 0 {
wr.WriteString(idxToId[i])
wr.WriteString("\t")
wr.WriteString(idxToId[j])
wr.WriteString("\t")
ele = strconv.FormatFloat(data.At(i, j), 'f', 6, 64)
wr.WriteString(ele)
wr.WriteString("\n")
}
}
}
wr.Flush()
return err
}
func WriteOutputFiles(isVerbose bool, resFolder string, trainMeasure *mat64.Dense, testMeasure *mat64.Dense, posLabelRls *mat64.Dense, negLabelRls *mat64.Dense, tsYhat *mat64.Dense, thres *mat64.Dense, Yhat *mat64.Dense, YhatCalibrated *mat64.Dense, Ylabel *mat64.Dense) {
oFile := "./" + resFolder + "/test.probMatrix.txt"
WriteFile(oFile, tsYhat, nil, false)
if isVerbose {
oFile = "./" + resFolder + "/cvTraining.measure.txt"
WriteFile(oFile, trainMeasure, nil, false)
oFile = "./" + resFolder + "/cvTesting.measure.txt"
WriteFile(oFile, testMeasure, nil, false)
oFile = "./" + resFolder + "/posLabelRls.txt"
WriteFile(oFile, posLabelRls, nil, false)
oFile = "./" + resFolder + "/negLabelRls.txt"
WriteFile(oFile, negLabelRls, nil, false)
oFile = "./" + resFolder + "/thres.txt"
WriteFile(oFile, thres, nil, false)
oFile = "./" + resFolder + "/train.probMatrix.txt"
WriteFile(oFile, Yhat, nil, false)
oFile = "./" + resFolder + "/trainCalibrated.probMatrix.txt"
WriteFile(oFile, YhatCalibrated, nil, false)
oFile = "./" + resFolder + "/reorder.trMatrix.txt"
WriteFile(oFile, Ylabel, nil, false)
//mem profile
memprofile := resFolder + "/mem.prof"
f, err2 := os.Create(memprofile)
if err2 != nil {
log.Fatal("could not create memory profile: ", err2)
}
defer f.Close()
runtime.GC() // get up-to-date statistics
if err2 := pprof.WriteHeapProfile(f); err2 != nil {
log.Fatal("could not write memory profile: ", err2)
}
defer f.Close()
}
}
func Init(resFolder string) (logFIle *os.File) {
err := os.MkdirAll("./"+resFolder, 0755)
if err != nil {
fmt.Println(err)
return
}
logFile, err := os.OpenFile("./"+resFolder+"/log.txt", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
return logFile
}