2021-08-19 14:24:30 +02:00
|
|
|
package clusters
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2021-10-06 07:10:50 +02:00
|
|
|
"os"
|
2021-08-19 14:24:30 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type jsonImporter struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func JsonImporter() Importer {
|
|
|
|
return &jsonImporter{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *jsonImporter) Import(file string, start, end int) ([][]float64, error) {
|
|
|
|
if start < 0 || end < 0 || start > end {
|
|
|
|
return [][]float64{}, errInvalidRange
|
|
|
|
}
|
|
|
|
|
2021-10-06 07:10:50 +02:00
|
|
|
f, err := os.ReadFile(file)
|
2021-08-19 14:24:30 +02:00
|
|
|
if err != nil {
|
|
|
|
return [][]float64{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
d = make([][]float64, 0)
|
|
|
|
s = end - start + 1
|
|
|
|
g = make([]float64, 0, s)
|
|
|
|
c int
|
|
|
|
)
|
|
|
|
|
|
|
|
err = json.Unmarshal(f, &d)
|
|
|
|
if err != nil {
|
|
|
|
return [][]float64{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, _ := range d {
|
|
|
|
c = 0
|
|
|
|
|
|
|
|
for j := start; j <= end; j++ {
|
|
|
|
g[c] = d[i][j]
|
|
|
|
c++
|
|
|
|
}
|
|
|
|
|
|
|
|
d[i] = make([]float64, 0, s)
|
|
|
|
for j := 0; j < s; j++ {
|
|
|
|
d[i][j] = g[j]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return d, nil
|
|
|
|
}
|