09e17022216f28f6e3724f5c966689218fedafb0
[lightning.git] / exportnumpy_test.go
1 package main
2
3 import (
4         "bytes"
5         "os"
6
7         "github.com/kshedden/gonpy"
8         "gopkg.in/check.v1"
9 )
10
11 type exportSuite struct{}
12
13 var _ = check.Suite(&exportSuite{})
14
15 func (s *exportSuite) TestFastaToNumpy(c *check.C) {
16         var buffer bytes.Buffer
17         exited := (&importer{}).RunCommand("import", []string{"-local=true", "-tag-library", "testdata/tags", "-ref", "testdata/ref", "-output-tiles", "testdata/a.1.fasta"}, &bytes.Buffer{}, &buffer, os.Stderr)
18         c.Assert(exited, check.Equals, 0)
19         var output bytes.Buffer
20         exited = (&exportNumpy{}).RunCommand("export-numpy", []string{"-local=true"}, &buffer, &output, os.Stderr)
21         c.Check(exited, check.Equals, 0)
22         npy, err := gonpy.NewReader(&output)
23         c.Assert(err, check.IsNil)
24         variants, err := npy.GetInt16()
25         c.Assert(err, check.IsNil)
26         for i := 0; i < 4; i += 2 {
27                 if variants[i] == 1 {
28                         c.Check(variants[i+1], check.Equals, int16(2), check.Commentf("i=%d, v=%v", i, variants))
29                 } else {
30                         c.Check(variants[i], check.Equals, int16(2), check.Commentf("i=%d, v=%v", i, variants))
31                 }
32         }
33         for i := 4; i < 9; i += 2 {
34                 c.Check(variants[i], check.Equals, int16(1), check.Commentf("i=%d, v=%v", i, variants))
35         }
36 }
37
38 func sortUints(variants []int16) {
39         for i := 0; i < len(variants); i += 2 {
40                 if variants[i] > variants[i+1] {
41                         for j := 0; j < len(variants); j++ {
42                                 variants[j], variants[j+1] = variants[j+1], variants[j]
43                         }
44                         return
45                 }
46         }
47 }
48
49 func (s *exportSuite) TestOnehot(c *check.C) {
50         for _, trial := range []struct {
51                 incols  int
52                 in      []int16
53                 outcols int
54                 out     []int16
55         }{
56                 {2, []int16{1, 1, 1, 1}, 2, []int16{1, 1, 1, 1}},
57                 {2, []int16{1, 1, 1, 2}, 3, []int16{1, 1, 0, 1, 0, 1}},
58                 {
59                         // 2nd column => 3 one-hot columns
60                         // 4th column => 0 one-hot columns
61                         4, []int16{
62                                 1, 1, 0, 0,
63                                 1, 2, 1, 0,
64                                 1, 3, 0, 0,
65                         }, 5, []int16{
66                                 1, 1, 0, 0, 0,
67                                 1, 0, 1, 0, 1,
68                                 1, 0, 0, 1, 0,
69                         },
70                 },
71         } {
72                 out, _, outcols := recodeOnehot(trial.in, trial.incols)
73                 c.Check(out, check.DeepEquals, trial.out)
74                 c.Check(outcols, check.Equals, trial.outcols)
75         }
76 }