Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add analyzer to order qi #31

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions examples/order.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{"x":4, "y":1}
{"x":4, "y":2}
{"x":4, "y":3}
{"x":3, "y":4}
{"x":3, "y":5}
{"x":3, "y":6}
{"x":3, "y":7}
{"x":4, "y":8}
{"x":4, "y":9}
{"x":4, "y":10}
{"x":3, "y":11}
{"x":4, "y":12}
{"x":4, "y":13}
{"x":3, "y":14}
{"x":3, "y":15}
70 changes: 70 additions & 0 deletions pkg/sigo/analyzer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package sigo

import (
"sort"
)

type Source struct {
qi map[string]int
values map[string][]float64
}

func NewAnalyzer(qi []string) Analyzer {
dict := make(map[string]int)
for i, key := range qi {
dict[key] = i
}

return Source{qi: dict, values: make(map[string][]float64)}
}

func (s Source) Add(r Record) {
for key, i := range s.qi {
s.values[key] = append(s.values[key], r.QuasiIdentifer()[i])
}
}

func (s Source) QI(i int) string {
return s.Order()[i]
}

func (s Source) CountUniqueValues() map[string]int {
uniques := make(map[string]int)

for key := range s.qi {
uniques[key] = Unique(s.values[key])
}

return uniques
}

func (s Source) Order() map[int]string {
order := make(map[int]string)
switched := make(map[int][]string)
slice := []int{}

for key, count := range s.CountUniqueValues() {
switched[count] = append(switched[count], key)

slice = append(slice, count)
}

sort.Sort(sort.Reverse(sort.IntSlice(slice)))

i := 0

for _, count := range slice {
for _, qi := range switched[count] {
order[i] = qi
i++
}

delete(switched, count)
}

return order
}

func (s Source) Dimension(rot int) int {
return s.qi[s.Order()[rot]]
}
71 changes: 71 additions & 0 deletions pkg/sigo/analyzer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright (C) 2022 CGI France
//
// This file is part of SIGO.
//
// SIGO is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// SIGO is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with SIGO. If not, see <http://www.gnu.org/licenses/>.
package sigo_test

import (
"testing"

"github.com/cgi-fr/sigo/pkg/sigo"
"github.com/stretchr/testify/assert"
)

func TestCountUniqueValues(t *testing.T) {
t.Parallel()

qi := []string{"x", "y"}
source := sigo.NewAnalyzer(qi)

source.Add(createRow(4, 1, qi))
source.Add(createRow(3, 2, qi))
source.Add(createRow(4, 3, qi))

res := source.CountUniqueValues()

assert.Equal(t, 2, res["x"])
assert.Equal(t, 3, res["y"])
}

func TestOrderMap(t *testing.T) {
t.Parallel()

qi := []string{"x", "y"}
source := sigo.NewAnalyzer(qi)

source.Add(createRow(4, 1, qi))
source.Add(createRow(3, 2, qi))
source.Add(createRow(4, 3, qi))

res := source.Order()

assert.Equal(t, "y", res[0])
assert.Equal(t, "x", res[1])
}

func TestDimension(t *testing.T) {
t.Parallel()

qi := []string{"x", "y"}
source := sigo.NewAnalyzer(qi)

source.Add(createRow(4, 1, qi))
source.Add(createRow(3, 2, qi))
source.Add(createRow(4, 3, qi))

res := source.Dimension(0)

assert.Equal(t, 1, res)
}
12 changes: 7 additions & 5 deletions pkg/sigo/kdtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type KDTreeFactory struct{}

func (f KDTreeFactory) New(k int, l int, dim int, qi []string) Generalizer {
// nolint: exhaustivestruct
tree := KDTree{k: k, l: l, dim: dim, clusterID: make(map[string]int), qi: qi}
tree := KDTree{k: k, l: l, dim: dim, clusterID: make(map[string]int), analyzer: NewAnalyzer(qi)}
root := NewNode(&tree, "root", 0)
root.validate()
tree.root = &root
Expand All @@ -49,7 +49,7 @@ type KDTree struct {
root *node
dim int
clusterID map[string]int
qi []string
analyzer Analyzer
}

func NewKDTree(k, l, dim int, clusterID map[string]int) KDTree {
Expand All @@ -59,6 +59,7 @@ func NewKDTree(k, l, dim int, clusterID map[string]int) KDTree {

func (t KDTree) Add(r Record) {
t.root.Add(r)
t.analyzer.Add(r)
}

func (t KDTree) Build() {
Expand Down Expand Up @@ -112,7 +113,7 @@ func (n *node) incRot() {

func (n *node) build() {
log.Debug().
Str("Dimension", n.tree.qi[n.rot]).
Str("Dimension", n.tree.analyzer.QI(n.rot)).
Str("Path", n.clusterPath).
Int("Size", len(n.cluster)).
Msg("Cluster:")
Expand Down Expand Up @@ -174,8 +175,9 @@ func (n *node) Bounds() []bounds {
}

func (n *node) split() (node, node, bool) {
dim := n.tree.analyzer.Dimension(n.rot)
sort.SliceStable(n.cluster, func(i int, j int) bool {
return n.cluster[i].QuasiIdentifer()[n.rot] < n.cluster[j].QuasiIdentifer()[n.rot]
return n.cluster[i].QuasiIdentifer()[dim] < n.cluster[j].QuasiIdentifer()[dim]
})

n.pivot = nil
Expand All @@ -189,7 +191,7 @@ func (n *node) split() (node, node, bool) {
previous := n.cluster[0]

for _, row := range n.cluster {
if lowerSize < len(n.cluster)/2 || row.QuasiIdentifer()[n.rot] == previous.QuasiIdentifer()[n.rot] {
if lowerSize < len(n.cluster)/2 { // || row.QuasiIdentifer()[n.rot] == previous.QuasiIdentifer()[n.rot] {
lower.Add(row)
previous = row
lowerSize++
Expand Down
8 changes: 8 additions & 0 deletions pkg/sigo/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,11 @@ type Anonymizer interface {
type Debugger interface {
Information(Record, Cluster) Record
}

type Analyzer interface {
Add(Record)
QI(i int) string
CountUniqueValues() map[string]int
Order() map[int]string
Dimension(int) int
}
10 changes: 10 additions & 0 deletions pkg/sigo/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,13 @@ func BoxMuller() (float64, float64) {

return z1, z2
}

func Unique(values []float64) int {
tmp := make(map[float64]int)

for _, val := range values {
tmp[val]++
}

return len(tmp)
}
13 changes: 13 additions & 0 deletions pkg/sigo/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,16 @@ func TestQuartiles(t *testing.T) {
assert.Equal(t, q.Q2, sigo.Median(values))
assert.Equal(t, 5.00, sigo.IQR(values))
}

func TestUnique(t *testing.T) {
t.Parallel()

values1 := []float64{12, 10, 5, 6, 9, 10, 4, 5, 10, 12, 9, 6, 4, 3, 9, 10}
values2 := []float64{1, 9, 8, 5, 2, 6, 7, 10, 3, 12, 4, 11}

res1 := sigo.Unique(values1)
res2 := sigo.Unique(values2)

assert.Equal(t, 7, res1)
assert.Equal(t, 12, res2)
}
30 changes: 30 additions & 0 deletions test/suites/02-order-QI.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Venom Test Suite definition
# Check Venom documentation for more information : https://github.com/ovh/venom

name: sigo odrer qi
testcases:
- name: sort qi
steps:
- script: |-
sigo -q x,y -i id <<EOF
{"x":4, "y":1}
{"x":4, "y":2}
{"x":4, "y":3}
{"x":3, "y":4}
{"x":3, "y":5}
{"x":3, "y":6}
{"x":3, "y":7}
{"x":4, "y":8}
{"x":4, "y":9}
{"x":4, "y":10}
{"x":3, "y":11}
{"x":4, "y":12}
{"x":4, "y":13}
{"x":3, "y":14}
{"x":3, "y":15}
EOF
assertions:
- result.systemoutjson.id ShouldEqual 1
- result.systemoutjson.x ShouldEqual 3
- result.systemoutjson.y ShouldEqual 4
- result.code ShouldEqual 0