-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv.go
More file actions
216 lines (194 loc) · 5.84 KB
/
csv.go
File metadata and controls
216 lines (194 loc) · 5.84 KB
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
// FILE: csv.go
// This file contains functions for reading and writing CSV data.
package main
import (
"bufio"
"encoding/csv"
"fmt"
"io"
"log"
"math"
"os"
"strconv"
"strings"
)
// calculateRessens calculates resolution and sensitivity from the generated pathlengths file.
// This is a port of the `summarise_data` function from the Python script.
func (m *Model) calculateRessens() {
p := m.Params
fmt.Println("INFO: Calculating resolution and sensitivity...")
// Open output files
resFile, err := os.Create(fmt.Sprintf("%s_summary_res.csv", p.SpeciesName))
if err != nil {
log.Fatalf("Error creating resolution file: %v", err)
}
defer resFile.Close()
resWriter := bufio.NewWriter(resFile)
defer resWriter.Flush()
sensFile, err := os.Create(fmt.Sprintf("%s_summary_sen.csv", p.SpeciesName))
if err != nil {
log.Fatalf("Error creating sensitivity file: %v", err)
}
defer sensFile.Close()
sensWriter := bufio.NewWriter(sensFile)
defer sensWriter.Flush()
// Open the pathlengths file for reading
pathlengthsFile, err := os.Open(fmt.Sprintf("%s_pathlengths.csv", p.SpeciesName))
if err != nil {
log.Fatalf("Error opening pathlengths file for reading: %v", err)
}
defer pathlengthsFile.Close()
// --- State variables for summary calculation ---
rhabdoms := make([]float64, 21)
matrixSens := []string{}
matrixRes := []string{}
facet := 0.0
arem := 0.0
cc, dd := 0, 0
scanner := bufio.NewScanner(pathlengthsFile)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" {
continue
}
if strings.Contains(line, "998") {
parts := strings.Split(line, ",")
rhabdom := 0
tot := 0.0
area := math.Pi * math.Pow(facet+0.5, 2)
inci := math.Pi * math.Pow(facet-0.5, 2)
if facet == 0 {
inci = 0
}
torus := area - inci
if area > arem {
arem = area
}
for _, part := range parts {
if part == "998" {
break
}
pathlength, _ := strconv.ParseFloat(part, 64)
var absorbance, bx float64
if pathlength > 0 {
absorbance = 1 - math.Exp(-0.01*pathlength)
} else {
absorbance = 0
}
if rhabdom == 0 && absorbance > 0 {
bx = 100 * absorbance
} else if rhabdom > 0 && absorbance > 0 {
bx = 100 * ((1 - tot) * absorbance)
}
if absorbance == 0 {
bx = 0
}
tot += bx / 100.0
bx *= torus
if rhabdom < len(rhabdoms) {
rhabdoms[rhabdom] += bx
}
rhabdom++
}
facet++
} else if line == "999" {
// End of block, summarize
sens := 0.0
for _, r := range rhabdoms {
sens += r
}
halfwayPoint := rhabdoms[0] / 2.0
opticAxis := 0.0
xz, yy := rhabdoms[0], rhabdoms[1]
for i := 1; i < 12; i++ {
if halfwayPoint < rhabdoms[i] {
xz = rhabdoms[i]
if i+1 < len(rhabdoms) {
yy = rhabdoms[i+1]
}
opticAxis = m.OmmatidialAngle * float64(i)
break
}
}
diff := xz - yy
hwp := xz - halfwayPoint
frac := hwp / (diff + 0.1) // prevent div by zero
oab := frac * m.OmmatidialAngle
res := oab + opticAxis
if cc == 0 && dd > 0 {
fmt.Fprintln(sensWriter, strings.Join(matrixSens, ","))
fmt.Fprintln(resWriter, strings.Join(matrixRes, ","))
matrixSens = []string{}
matrixRes = []string{}
}
if arem > 0 {
matrixSens = append(matrixSens, fmt.Sprintf("%d", int(sens/arem)))
} else {
matrixSens = append(matrixSens, "0")
}
matrixRes = append(matrixRes, fmt.Sprintf("%d", int(res*200)))
cc++
if cc == 11 {
dd++
cc = 0
}
// Reset for next block
for i := range rhabdoms {
rhabdoms[i] = 0
}
facet = 0
}
}
// Write the final line of data
if len(matrixSens) > 0 {
fmt.Fprintln(sensWriter, strings.Join(matrixSens, ","))
fmt.Fprintln(resWriter, strings.Join(matrixRes, ","))
}
}
// parseInputParameters reads a parameter file using Go's standard CSV reader.
// It returns a slice of parsed parameters and an error if parsing fails.
func parseInputParameters(filename string) ([]Parameters, error) {
var paramsList []Parameters // A slice to hold multiple parameter sets
file, err := os.Open(filename)
if err != nil {
return nil, fmt.Errorf("could not open parameter file %s: %w", filename, err)
}
defer file.Close()
reader := csv.NewReader(file)
for {
record, err := reader.Read()
if err == io.EOF {
break
}
// Check for a specific parsing error (like wrong number of fields)
if parseErr, ok := err.(*csv.ParseError); ok && parseErr.Err == csv.ErrFieldCount {
log.Printf("Skipping malformed record on line %d: %v", parseErr.Line, err)
continue // Skip to the next record
}
if err != nil {
return nil, fmt.Errorf("error reading CSV record: %w", err)
}
if len(record) != 10 {
log.Printf("Skipping malformed record (expected 10 fields, got %d): %v", len(record), record)
continue
}
var params Parameters
// (sn, rl, rw, ed, fw, ad, cri, rri, bce, pra)
params.SpeciesName = strings.TrimSpace(record[0])
params.RhabdomLength, _ = strconv.ParseFloat(strings.TrimSpace(record[1]), 64)
params.RhabdomWidth, _ = strconv.ParseFloat(strings.TrimSpace(record[2]), 64)
params.EyeDiameter, _ = strconv.ParseFloat(strings.TrimSpace(record[3]), 64)
params.FacetWidth, _ = strconv.ParseFloat(strings.TrimSpace(record[4]), 64)
params.ApertureDiameter, _ = strconv.ParseFloat(strings.TrimSpace(record[5]), 64)
params.CytoplasmRefractiveIndex, _ = strconv.ParseFloat(strings.TrimSpace(record[6]), 64)
params.RhabdomRefractiveIndex, _ = strconv.ParseFloat(strings.TrimSpace(record[7]), 64)
bce, _ := strconv.ParseFloat(strings.TrimSpace(record[8]), 64)
params.BlurCircleExtent = math.Max(1.0, bce) // Ensure blur circle is at least 1
params.ProximalRhabdomAngle, _ = strconv.ParseFloat(strings.TrimSpace(record[9]), 64)
paramsList = append(paramsList, params)
}
if len(paramsList) == 0 {
return nil, fmt.Errorf("no valid parameter data found in %s", filename)
}
return paramsList, nil
}