github.com/janelia-flyem/dvid@v1.0.0/datatype/common/labels/index_test.go (about)

     1  package labels
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/janelia-flyem/dvid/datatype/common/proto"
     8  	"github.com/janelia-flyem/dvid/dvid"
     9  )
    10  
    11  func TestIndexOps(t *testing.T) {
    12  	var idx Index
    13  	idx.Blocks = make(map[uint64]*proto.SVCount)
    14  
    15  	block1 := EncodeBlockIndex(1, 1, 1)
    16  	x, y, z := DecodeBlockIndex(block1)
    17  	if x != 1 || y != 1 || z != 1 {
    18  		t.Errorf("bad block index encoding\n")
    19  	}
    20  	svc := new(proto.SVCount)
    21  	svc.Counts = map[uint64]uint32{
    22  		23:      100,
    23  		1001:    899,
    24  		11890:   357,
    25  		8473291: 20000,
    26  	}
    27  	idx.Blocks[block1] = svc
    28  	if idx.NumVoxels() != 21356 {
    29  		t.Errorf("bad NumVoxels(), get %d, expected 21356\n", idx.NumVoxels())
    30  	}
    31  	expected := Set{
    32  		23:      struct{}{},
    33  		1001:    struct{}{},
    34  		11890:   struct{}{},
    35  		8473291: struct{}{},
    36  	}
    37  	supervoxels := idx.GetSupervoxels()
    38  	if len(supervoxels) != 4 || !reflect.DeepEqual(supervoxels, expected) {
    39  		t.Errorf("expected %v, got %v\n", expected, supervoxels)
    40  	}
    41  
    42  	block2 := EncodeBlockIndex(10, 24837, 890)
    43  	x, y, z = DecodeBlockIndex(block2)
    44  	if x != 10 || y != 24837 || z != 890 {
    45  		t.Errorf("bad block index encoding\n")
    46  	}
    47  	izyx := BlockIndexToIZYXString(block2)
    48  	chunkPt, err := izyx.ToChunkPoint3d()
    49  	if err != nil {
    50  		t.Error(err)
    51  	}
    52  	if chunkPt[0] != 10 || chunkPt[1] != 24837 || chunkPt[2] != 890 {
    53  		t.Error("bad block index to IZYXString")
    54  	}
    55  	svc = new(proto.SVCount)
    56  	svc.Counts = map[uint64]uint32{
    57  		23:    11,
    58  		87:    289,
    59  		382:   400,
    60  		1001:  1000,
    61  		3829:  10000,
    62  		9584:  15000,
    63  		29284: 3819,
    64  	}
    65  	idx.Blocks[block2] = svc
    66  
    67  	block3 := EncodeBlockIndex(87, 283, 3855)
    68  	x, y, z = DecodeBlockIndex(block3)
    69  	if x != 87 || y != 283 || z != 3855 {
    70  		t.Errorf("bad block index encoding\n")
    71  	}
    72  	svc = new(proto.SVCount)
    73  	svc.Counts = map[uint64]uint32{
    74  		23:     14,
    75  		673:    2389,
    76  		1001:   5000,
    77  		8763:   25463,
    78  		26029:  63560,
    79  		356983: 486927,
    80  	}
    81  	idx.Blocks[block3] = svc
    82  	idx.Label = 199
    83  
    84  	sidx, err := idx.LimitToSupervoxel(673)
    85  	if err != nil {
    86  		t.Error(err)
    87  	}
    88  	limitCounts := map[uint64]uint64{
    89  		673: 2389,
    90  	}
    91  	if !reflect.DeepEqual(limitCounts, sidx.GetSupervoxelCounts()) {
    92  		t.Errorf("after cleave, remain index has incorrect counts:\nExpected %v\nGot %v\n", limitCounts, sidx.GetSupervoxelCounts())
    93  	}
    94  	if len(sidx.Blocks) != 1 {
    95  		t.Errorf("expected supervoxel limits count to be 1, got %d\n", len(sidx.Blocks))
    96  	}
    97  	_, found := sidx.Blocks[block3]
    98  	if !found {
    99  		t.Errorf("didnt find expected block after LimitToSupervoxel()\n")
   100  	}
   101  
   102  	origCounts := idx.GetSupervoxelCounts()
   103  	var totalVoxels uint64
   104  	for _, count := range origCounts {
   105  		totalVoxels += count
   106  	}
   107  	if totalVoxels != 635228 {
   108  		t.Errorf("expected 635228 total voxels, got %d voxels in index supervoxel counts\n", totalVoxels)
   109  	}
   110  
   111  	cleavedSize, remainSize, cleaveIdx := idx.Cleave(200, []uint64{1001, 26029, 3829})
   112  	expectedCleavedSize := uint64(6899 + 63560 + 10000)
   113  	if cleavedSize != expectedCleavedSize {
   114  		t.Errorf("expected cleaved size to be %d, got %d\n", expectedCleavedSize, cleavedSize)
   115  	}
   116  	if remainSize != totalVoxels-cleavedSize {
   117  		t.Errorf("expected remaining size to be %d, got %d\n", totalVoxels-cleavedSize, remainSize)
   118  	}
   119  	supervoxels = idx.GetSupervoxels()
   120  	mainBodySupervoxels := Set{
   121  		23:      struct{}{},
   122  		11890:   struct{}{},
   123  		8473291: struct{}{},
   124  		87:      struct{}{},
   125  		382:     struct{}{},
   126  		9584:    struct{}{},
   127  		29284:   struct{}{},
   128  		673:     struct{}{},
   129  		8763:    struct{}{},
   130  		356983:  struct{}{},
   131  	}
   132  	if !reflect.DeepEqual(supervoxels, mainBodySupervoxels) {
   133  		t.Errorf("after cleave, remain index is weird.  Expected %v, got %v\n", mainBodySupervoxels, supervoxels)
   134  	}
   135  	cleaveSupervoxels := Set{
   136  		26029: struct{}{},
   137  		3829:  struct{}{},
   138  		1001:  struct{}{},
   139  	}
   140  	supervoxels = cleaveIdx.GetSupervoxels()
   141  	if !reflect.DeepEqual(supervoxels, cleaveSupervoxels) {
   142  		t.Errorf("after cleave, the cleaved index is weird.  Expected %v, got %v\n", cleaveSupervoxels, supervoxels)
   143  	}
   144  
   145  	mainCounts := map[uint64]uint64{
   146  		23:      125,
   147  		11890:   357,
   148  		8473291: 20000,
   149  		87:      289,
   150  		382:     400,
   151  		9584:    15000,
   152  		29284:   3819,
   153  		673:     2389,
   154  		8763:    25463,
   155  		356983:  486927,
   156  	}
   157  	cleaveCounts := map[uint64]uint64{
   158  		1001:  6899,
   159  		3829:  10000,
   160  		26029: 63560,
   161  	}
   162  	if idx.GetSupervoxelCount(23) != 125 {
   163  		t.Error("bad count")
   164  	}
   165  	if idx.GetSupervoxelCount(9584) != 15000 {
   166  		t.Error("bad count")
   167  	}
   168  	if cleaveIdx.GetSupervoxelCount(1001) != 6899 {
   169  		t.Error("bad count")
   170  	}
   171  	if !reflect.DeepEqual(mainCounts, idx.GetSupervoxelCounts()) {
   172  		t.Errorf("after cleave, remain index has incorrect counts:\nExpected %v\nGot %v\n", mainCounts, idx.GetSupervoxelCounts())
   173  	}
   174  	if !reflect.DeepEqual(cleaveCounts, cleaveIdx.GetSupervoxelCounts()) {
   175  		t.Errorf("after cleave, remain index has incorrect counts:\nExpected %v\nGot %v\n", cleaveCounts, cleaveIdx.GetSupervoxelCounts())
   176  	}
   177  	if err := idx.Add(cleaveIdx); err != nil {
   178  		t.Error(err)
   179  	}
   180  	if !reflect.DeepEqual(origCounts, idx.GetSupervoxelCounts()) {
   181  		t.Errorf("after add, recombined index has incorrect counts:\nExpected %v\nGot %v\n", origCounts, idx.GetSupervoxelCounts())
   182  	}
   183  
   184  	// test GetProcessedBlockIndices()
   185  
   186  	blockIndices := idx.GetBlockIndices()
   187  	expectedBlocks := map[dvid.IZYXString]struct{}{
   188  		BlockIndexToIZYXString(block1): struct{}{},
   189  		BlockIndexToIZYXString(block2): struct{}{},
   190  		BlockIndexToIZYXString(block3): struct{}{},
   191  	}
   192  	gotBlocks := make(map[dvid.IZYXString]struct{}, 3)
   193  	for _, zyx := range blockIndices {
   194  		gotBlocks[zyx] = struct{}{}
   195  	}
   196  	if !reflect.DeepEqual(expectedBlocks, gotBlocks) {
   197  		t.Errorf("after add, recombined index has incorrect blocks:\nExpected %v\nGot %v\n", expectedBlocks, gotBlocks)
   198  	}
   199  	var totVoxels uint64
   200  	for _, count := range mainCounts {
   201  		totVoxels += count
   202  	}
   203  	for _, count := range cleaveCounts {
   204  		totVoxels += count
   205  	}
   206  	if idx.NumVoxels() != totVoxels {
   207  		t.Errorf("expected %d total voxels, got %d\n", totVoxels, idx.NumVoxels())
   208  	}
   209  
   210  	var bounds dvid.Bounds
   211  	bounds.Block = new(dvid.OptionalBounds)
   212  	bounds.Block.SetMinX(2)
   213  	bounds.Block.SetMinY(2)
   214  	bounds.Block.SetMaxZ(3800)
   215  	blockIndices, err = idx.GetProcessedBlockIndices(0, bounds, 0)
   216  	if err != nil {
   217  		t.Error(err)
   218  	}
   219  	expectedBlocks = map[dvid.IZYXString]struct{}{
   220  		BlockIndexToIZYXString(block2): struct{}{},
   221  	}
   222  	gotBlocks = make(map[dvid.IZYXString]struct{}, 3)
   223  	for _, zyx := range blockIndices {
   224  		gotBlocks[zyx] = struct{}{}
   225  	}
   226  	if !reflect.DeepEqual(expectedBlocks, gotBlocks) {
   227  		t.Errorf("bounded recombined index has incorrect blocks:\nExpected %v\nGot %v\n", expectedBlocks, gotBlocks)
   228  	}
   229  
   230  	// test ModifyBlocks
   231  	var sc SupervoxelChanges
   232  	if err := idx.ModifyBlocks(23, sc); err != nil {
   233  		t.Error(err)
   234  	}
   235  	if !reflect.DeepEqual(origCounts, idx.GetSupervoxelCounts()) {
   236  		t.Errorf("after nil modify blocks, full index has incorrect counts:\nExpected %v\nGot %v\n", origCounts, idx.GetSupervoxelCounts())
   237  	}
   238  	sc = make(SupervoxelChanges)
   239  	sc[23] = make(map[dvid.IZYXString]int32)
   240  	sc[23][dvid.ChunkPoint3d{87, 283, 3855}.ToIZYXString()] = -5
   241  	sc[23][dvid.ChunkPoint3d{1, 1, 1}.ToIZYXString()] = 8
   242  	if err := idx.ModifyBlocks(23, sc); err != nil {
   243  		t.Error(err)
   244  	}
   245  	testCounts := origCounts
   246  	testCounts[23] += 3
   247  	if !reflect.DeepEqual(testCounts, idx.GetSupervoxelCounts()) {
   248  		t.Errorf("after modify blocks, full index has incorrect counts:\nExpected %v\nGot %v\n", testCounts, idx.GetSupervoxelCounts())
   249  	}
   250  
   251  	sc[23][dvid.ChunkPoint3d{87, 283, 3855}.ToIZYXString()] = -50
   252  	if err := idx.ModifyBlocks(23, sc); err == nil {
   253  		t.Errorf("expected error in subtracting too many voxels in block, but got none!\n")
   254  	}
   255  }
   256  
   257  func TestCleaveIndex(t *testing.T) {
   258  	var idx Index
   259  	idx.Blocks = make(map[uint64]*proto.SVCount)
   260  
   261  	block1 := EncodeBlockIndex(1, 1, 1)
   262  	svc := new(proto.SVCount)
   263  	svc.Counts = map[uint64]uint32{
   264  		23:      100,
   265  		1001:    899,
   266  		11890:   357,
   267  		8473291: 20000,
   268  	}
   269  	idx.Blocks[block1] = svc
   270  
   271  	block2 := EncodeBlockIndex(10, 24837, 890)
   272  	svc = new(proto.SVCount)
   273  	svc.Counts = map[uint64]uint32{
   274  		87:   289,
   275  		382:  400,
   276  		1001: 1000,
   277  		3829: 10000,
   278  	}
   279  	idx.Blocks[block2] = svc
   280  
   281  	block3 := EncodeBlockIndex(87, 283, 3855)
   282  	svc = new(proto.SVCount)
   283  	svc.Counts = map[uint64]uint32{
   284  		673:  2389,
   285  		1001: 5000,
   286  		8763: 25463,
   287  	}
   288  	idx.Blocks[block3] = svc
   289  	idx.Label = 199
   290  
   291  	block4 := EncodeBlockIndex(2, 1, 1)
   292  	svc = new(proto.SVCount)
   293  	svc.Counts = map[uint64]uint32{
   294  		23:      111,
   295  		41234:   7,
   296  		8473291: 7381,
   297  	}
   298  	idx.Blocks[block4] = svc
   299  
   300  	cleavedSize, remainSize, cleaveIdx := idx.Cleave(200, []uint64{87, 382, 1001, 3829, 673, 8763})
   301  	expectedTotalSize := uint64(73396)
   302  	if cleavedSize+remainSize != expectedTotalSize {
   303  		t.Errorf("cleaved %d + remain %d voxels != %d total voxels\n", cleavedSize, remainSize, expectedTotalSize)
   304  	}
   305  	if cleavedSize != 45440 {
   306  		t.Errorf("cleaved voxels %d != 45440\n", cleavedSize)
   307  	}
   308  	supervoxels := idx.GetSupervoxels()
   309  	mainBodySupervoxels := Set{
   310  		23:      struct{}{},
   311  		11890:   struct{}{},
   312  		41234:   struct{}{},
   313  		8473291: struct{}{},
   314  	}
   315  	if !reflect.DeepEqual(supervoxels, mainBodySupervoxels) {
   316  		t.Errorf("after cleave, remain index is weird.  Expected %v, got %v\n", mainBodySupervoxels, supervoxels)
   317  	}
   318  	if len(idx.Blocks) != 2 {
   319  		t.Fatalf("after cleave, expected remain index to have 2 blocks, got %d\n", len(idx.Blocks))
   320  	}
   321  	if len(cleaveIdx.Blocks) != 3 {
   322  		t.Errorf("after cleave, expected cleaved index to have 3 blocks, got %d\n", len(cleaveIdx.Blocks))
   323  	}
   324  	cleaveSupervoxels := Set{
   325  		87:   struct{}{},
   326  		382:  struct{}{},
   327  		673:  struct{}{},
   328  		1001: struct{}{},
   329  		3829: struct{}{},
   330  		8763: struct{}{},
   331  	}
   332  	supervoxels = cleaveIdx.GetSupervoxels()
   333  	if !reflect.DeepEqual(supervoxels, cleaveSupervoxels) {
   334  		t.Errorf("after cleave, the cleaved index is weird.  Expected %v, got %v\n", cleaveSupervoxels, supervoxels)
   335  	}
   336  
   337  	remainCounts1 := map[uint64]uint32{
   338  		23:      100,
   339  		11890:   357,
   340  		8473291: 20000,
   341  	}
   342  	svc, found := idx.Blocks[block1]
   343  	if !found || svc == nil {
   344  		t.Fatalf("expected remain index to have block %s\n", BlockIndexToIZYXString(block1))
   345  	}
   346  	if !reflect.DeepEqual(svc.Counts, remainCounts1) {
   347  		t.Errorf("expected remain index to be %v, got %v\n", remainCounts1, svc.Counts)
   348  	}
   349  	remainCounts4 := map[uint64]uint32{
   350  		23:      111,
   351  		41234:   7,
   352  		8473291: 7381,
   353  	}
   354  	svc, found = idx.Blocks[block4]
   355  	if !found || svc == nil {
   356  		t.Fatalf("expected remain index to have block %s\n", BlockIndexToIZYXString(block4))
   357  	}
   358  	if !reflect.DeepEqual(svc.Counts, remainCounts4) {
   359  		t.Errorf("expected remain index to be %v, got %v\n", remainCounts4, svc.Counts)
   360  	}
   361  
   362  	cleaveCounts1 := map[uint64]uint32{
   363  		1001: 899,
   364  	}
   365  	svc, found = cleaveIdx.Blocks[block1]
   366  	if !found || svc == nil {
   367  		t.Fatalf("expected cleave index to have block %s\n", BlockIndexToIZYXString(block1))
   368  	}
   369  	if !reflect.DeepEqual(svc.Counts, cleaveCounts1) {
   370  		t.Errorf("expected cleave index for %s to be %v, got %v\n", BlockIndexToIZYXString(block1), cleaveCounts1, svc.Counts)
   371  	}
   372  	cleaveCounts2 := map[uint64]uint32{
   373  		87:   289,
   374  		382:  400,
   375  		1001: 1000,
   376  		3829: 10000,
   377  	}
   378  	svc, found = cleaveIdx.Blocks[block2]
   379  	if !found || svc == nil {
   380  		t.Fatalf("expected cleave index to have block %s\n", BlockIndexToIZYXString(block2))
   381  	}
   382  	if !reflect.DeepEqual(svc.Counts, cleaveCounts2) {
   383  		t.Errorf("expected cleave index for %s to be %v, got %v\n", BlockIndexToIZYXString(block2), cleaveCounts2, svc.Counts)
   384  	}
   385  	cleaveCounts3 := map[uint64]uint32{
   386  		673:  2389,
   387  		1001: 5000,
   388  		8763: 25463,
   389  	}
   390  	svc, found = cleaveIdx.Blocks[block3]
   391  	if !found || svc == nil {
   392  		t.Fatalf("expected cleave index to have block %s\n", BlockIndexToIZYXString(block3))
   393  	}
   394  	if !reflect.DeepEqual(svc.Counts, cleaveCounts3) {
   395  		t.Errorf("expected cleave index for %s to be %v, got %v\n", BlockIndexToIZYXString(block3), cleaveCounts3, svc.Counts)
   396  	}
   397  }