~daniele-bigoni/tensortoolbox/tt-docs

« back to all changes in this revision

Viewing changes to TensorToolbox/unittests/TestTensorWrapper.py

  • Committer: Daniele Bigoni
  • Date: 2015-01-19 11:10:20 UTC
  • Revision ID: dabi@dtu.dk-20150119111020-p0uckg4ab3xqzf47
merged with research

Show diffs side-by-side

added added

removed removed

Lines of Context:
136
136
    # Reload
137
137
    testn += 1
138
138
    TW = TT.load(store_location)
139
 
    TW.set_f(f)
 
139
    TW.set_f(f,False)
140
140
    idx = tuple( [slice(None,None,None)]*d )
141
141
    test(testn,"Reload",idx)
142
142
    os.remove(store_location + ".pkl")
599
599
    d = len(shape)
600
600
    
601
601
    # Create A
602
 
    vs = [ npr.random(size=shape[i]) for i in range(d) ]
 
602
    vs = [ 1 + npr.random(size=shape[i]) for i in range(d) ]
603
603
    A = reduce(np.multiply, np.ix_(*vs))
604
604
 
605
605
    # Create Atest
606
606
    vs_test = [ np.hstack( (vs[i], np.ones(qshape[i]-shape[i])*vs[i][-1]) ) for i in range(d) ]
 
607
    # vs_test = [ np.hstack( (vs[i], np.zeros(qshape[i]-shape[i])) ) for i in range(d) ]
607
608
    Atest = reduce(np.multiply, np.ix_(*vs_test))
608
609
 
609
610
    Aglobal = A.copy()              # Used in f in order to test fix_indices
624
625
            print_fail(testn,title,msg='Different output - idx: ' + str(idx))
625
626
            nfail += 1
626
627
        elif feval != np.prod(np.unique(Atest[idx]).shape):
 
628
        # elif feval != np.prod(np.unique(Atest[idx][Atest[idx]>0]).shape):
627
629
            print_fail(testn,title,msg='Wrong number of function evaluations - idx: ' + str(idx))
628
630
            nfail += 1
629
631
        else:
631
633
            nsucc += 1
632
634
 
633
635
    X = [ np.arange(s,dtype=int) for s in shape ]
634
 
    TW = TT.TensorWrapper(f, X, None, Q=Q, dtype=A.dtype, marshal_f=False)
 
636
    TW = TT.TensorWrapper(f, X, None, dtype=A.dtype, marshal_f=False)
 
637
    TW.set_Q( Q )
635
638
 
636
639
    ###################################################################
637
640
    # 01: Single address access
663
666
    # Reload
664
667
    testn += 1
665
668
    TW = TT.load(store_location)
666
 
    TW.set_f(f)
 
669
    TW.set_f(f,False)
667
670
    idx = tuple( [slice(None,None,None)]*d )
668
671
    test(testn,"PowQ - Reload",idx)
669
672
    os.remove(store_location + ".pkl")
814
817
    d = len(shape)
815
818
    
816
819
    # Create A
817
 
    vs = [ npr.random(size=shape[i]) for i in range(d) ]
 
820
    vs = [ 1 + npr.random(size=shape[i]) for i in range(d) ]
818
821
    A = reduce(np.multiply, np.ix_(*vs))
819
822
 
820
823
    # Create Atest
821
824
    vs_test = [ np.hstack( (vs[i], np.ones(qshape[i]-shape[i])*vs[i][-1]) ) for i in range(d) ]
 
825
    # vs_test = [ np.hstack( (vs[i], np.zeros(qshape[i]-shape[i])) ) for i in range(d) ]
822
826
    Atest = reduce(np.multiply, np.ix_(*vs_test))
823
827
 
824
828
    Aglobal = A.copy()              # Used in f in order to test fix_indices
839
843
            print_fail(testn,title,msg='Different output - idx: ' + str(idx))
840
844
            nfail += 1
841
845
        elif feval != np.prod(np.unique(Atest[idx]).shape):
 
846
        # elif feval != np.prod(np.unique(Atest[idx][Atest[idx]>0]).shape):
842
847
            print_fail(testn,title,msg='Wrong number of function evaluations - idx: ' + str(idx))
843
848
            nfail += 1
844
849
        else:
846
851
            nsucc += 1
847
852
 
848
853
    X = [ np.arange(s,dtype=int) for s in shape ]
849
 
    TW = TT.TensorWrapper(f, X, None, Q=Q, dtype=A.dtype, marshal_f=False)
 
854
    TW = TT.TensorWrapper(f, X, None, dtype=A.dtype, marshal_f=False)
 
855
    TW.set_Q( Q )
850
856
    Atest_shape = Atest.copy()             # Storing original array
851
857
    newshape = [Q]*int(math.log(np.prod(qshape),Q))
852
858
    Atest = np.reshape( Atest, newshape )
990
996
    #
991
997
    testn += 1
992
998
    Atest = Atest_shape.copy()
993
 
    TW.reset_shape()
 
999
    TW.reset_ghost_shape()
994
1000
    idx = [ slice(None,None,None) ] * TW.ndim
995
1001
    test(testn, "Reshaped PowQ - Restore original shape", idx)
996
1002
    ##
998
1004
    #############################################################################
999
1005
    #############################################################################
1000
1006
 
 
1007
    
 
1008
    #############################################################################
 
1009
    #############################################################################
 
1010
    ## Test sub views: construct a sub view and check its consistency
 
1011
    ##
 
1012
    
 
1013
    coarse_shape = [3,5,5,5]
 
1014
    full_shape = [ 2*s-1 for s in coarse_shape ]
 
1015
    X_full = [ np.linspace(0,1,s) for s in full_shape ]
 
1016
    X_coarse = [ np.linspace(0,1,s) for s in coarse_shape ]
 
1017
    d = len(full_shape)
 
1018
 
 
1019
    rnd_p = npr.random(size=d)
 
1020
    
 
1021
    tmp = [ x * p for x,p in zip(X_full, rnd_p) ]
 
1022
    Atest_full = reduce( np.add, np.ix_(*tmp) )
 
1023
    Atest_coarse = Atest_full[::2,::2,::2,::2]
 
1024
 
 
1025
    def f(X, params):
 
1026
        global feval
 
1027
        feval += 1
 
1028
        return np.dot(rnd_p,X)
 
1029
    
 
1030
    def test(testn, title, idx, view):
 
1031
        global feval
 
1032
        global nsucc
 
1033
        global nfail
 
1034
        feval = 0
 
1035
        TW.data = {}
 
1036
        TW.set_active_view(view)
 
1037
        out = TW[idx]
 
1038
        if view == 'full': A = Atest_full
 
1039
        elif view == 'coarse': A = Atest_coarse
 
1040
        if np.any(A[idx] != out) or np.any(A[idx].shape != out.shape):
 
1041
            print_fail(testn,title,msg='Different output - idx: ' + str(idx))
 
1042
            nfail += 1
 
1043
        elif feval != np.prod(np.unique(A[idx]).shape):
 
1044
            print_fail(testn,title,msg='Wrong number of function evaluations - idx: ' + str(idx))
 
1045
            nfail += 1
 
1046
        else:
 
1047
            print_ok(testn,title)
 
1048
            nsucc += 1
 
1049
            
 
1050
    TW = TT.TensorWrapper(f, X_full, None, dtype=Atest_full.dtype, marshal_f=False)
 
1051
    TW.set_view( 'coarse', X_coarse )
 
1052
    
 
1053
    ######################################################
 
1054
    # Single index full
 
1055
    testn += 1
 
1056
    idx = (1,3,3,3)
 
1057
    test( testn, "View - Single index full", idx, 'full' )
 
1058
 
 
1059
    ######################################################
 
1060
    # Single index coarse
 
1061
    testn += 1
 
1062
    idx = (1,3,3,3)
 
1063
    test( testn, "View - Single index coarse", idx, 'coarse' )
 
1064
 
 
1065
    ######################################################
 
1066
    # Single slice full
 
1067
    testn += 1
 
1068
    idx = (1,slice(None,None,None),3,3)
 
1069
    test( testn, "View - Single slice full", idx, 'full' )
 
1070
 
 
1071
    ######################################################
 
1072
    # Single slice coarse
 
1073
    testn += 1
 
1074
    idx = (1,3,slice(None,None,None),3)
 
1075
    test( testn, "View - Single slice coarse", idx, 'coarse' )
 
1076
 
 
1077
    ######################################################
 
1078
    # Partial slice full
 
1079
    testn += 1
 
1080
    idx = (1,slice(1,3,1),3,3)
 
1081
    test( testn, "View - Partial slice full", idx, 'full' )
 
1082
 
 
1083
    ######################################################
 
1084
    # Partial index coarse
 
1085
    testn += 1
 
1086
    idx = (1,3,slice(0,2,1),3)
 
1087
    test( testn, "View - Partial slice coarse", idx, 'coarse' )
 
1088
 
 
1089
    ######################################################
 
1090
    # Multiple slice full
 
1091
    testn += 1
 
1092
    idx = (1,slice(1,3,1),slice(None,None,None),3)
 
1093
    test( testn, "View - Multiple slice full", idx, 'full' )
 
1094
 
 
1095
    ######################################################
 
1096
    # Multiple slice coarse
 
1097
    testn += 1
 
1098
    idx = (1,slice(None,None,None),slice(0,2,1),3)
 
1099
    test( testn, "View - Multiple slice coarse", idx, 'coarse' )
 
1100
 
 
1101
    ######################################################
 
1102
    # Full slice full
 
1103
    testn += 1
 
1104
    idx = tuple([slice(None,None,None)] * d)
 
1105
    test( testn, "View - Full slice full", idx, 'full' )
 
1106
 
 
1107
    ######################################################
 
1108
    # Full slice coarse
 
1109
    testn += 1
 
1110
    idx = tuple([slice(None,None,None)] * d)
 
1111
    test( testn, "View - Full slice coarse", idx, 'coarse' )
 
1112
 
 
1113
    ######################################################
 
1114
    # List full
 
1115
    testn += 1
 
1116
    nn = 2
 
1117
    idx = tuple( [ [random.randint(0,full_shape[i]-1) for j in range(nn)] for i in range(d) ] )
 
1118
    test( testn, "View - List full", idx, 'full' )
 
1119
 
 
1120
    ######################################################
 
1121
    # List coarse
 
1122
    testn += 1
 
1123
    nn = 2
 
1124
    idx = tuple( [ [random.randint(0,coarse_shape[i]-1) for j in range(nn)] for i in range(d) ] )
 
1125
    test( testn, "View - List coarse", idx, 'coarse' )
 
1126
 
 
1127
    ######################################################
 
1128
    # Single list slice full
 
1129
    testn += 1
 
1130
    nn = 2
 
1131
    idx = (1, [random.randint(0,full_shape[1]-1) for j in range(nn)], slice(None,None,None), 2)
 
1132
    test( testn, "View - Single list slice full", idx, 'full' )
 
1133
 
 
1134
    ######################################################
 
1135
    # Single list slice coarse
 
1136
    testn += 1
 
1137
    nn = 2
 
1138
    idx = (1, [random.randint(0,coarse_shape[1]-1) for j in range(nn)], slice(None,None,None), 2)
 
1139
    test( testn, "View - Single list slice coarse", idx, 'coarse' )
 
1140
 
 
1141
    ######################################################
 
1142
    # Single list slice full
 
1143
    testn += 1
 
1144
    nn = 2
 
1145
    idx = (1, slice(None,None,None), [random.randint(0,full_shape[2]-1) for j in range(nn)], 2)
 
1146
    test( testn, "View - Single list slice full", idx, 'full' )
 
1147
 
 
1148
    ######################################################
 
1149
    # Single list slice coarse
 
1150
    testn += 1
 
1151
    nn = 2
 
1152
    idx = (1, slice(None,None,None), [random.randint(0,coarse_shape[2]-1) for j in range(nn)], 2)
 
1153
    test( testn, "View - Single list slice coarse", idx, 'coarse' )
 
1154
 
 
1155
    ######################################################
 
1156
    # Single list slice full
 
1157
    testn += 1
 
1158
    nn = 2
 
1159
    idx = (1, slice(None,None,None), 2, [random.randint(0,full_shape[3]-1) for j in range(nn)])
 
1160
    test( testn, "View - Single list slice full", idx, 'full' )
 
1161
 
 
1162
    ######################################################
 
1163
    # Single list slice coarse
 
1164
    testn += 1
 
1165
    nn = 2
 
1166
    idx = (1, slice(None,None,None), 2, [random.randint(0,coarse_shape[3]-1) for j in range(nn)])
 
1167
    test( testn, "View - Single list slice coarse", idx, 'coarse' )
 
1168
 
 
1169
    ######################################################
 
1170
    # Sub view and PowQ
 
1171
    ######################################################
 
1172
    Q = 2
 
1173
    ext_full_shape = [ Q**(int(math.log(s,Q))+1) for s in full_shape ]
 
1174
    ext_coarse_shape = [ Q**(int(math.log(s,Q))+1) for s in coarse_shape ]
 
1175
    # Create folded tensors
 
1176
    X_ext_full = [ np.hstack( (X_full[i], np.ones(ext_full_shape[i]-full_shape[i])*X_full[i][-1]) ) for i in range(d) ]
 
1177
    X_ext_coarse = [ np.hstack( (X_coarse[i], np.ones(ext_coarse_shape[i]-coarse_shape[i])*X_coarse[i][-1]) ) for i in range(d) ]
 
1178
    tmp_ext_full = [ x * p for x,p in zip(X_ext_full, rnd_p) ]
 
1179
    Atest_ext_full = reduce( np.add, np.ix_(*tmp_ext_full) )
 
1180
    tmp_ext_coarse = [ x * p for x,p in zip(X_ext_coarse, rnd_p) ]
 
1181
    Atest_ext_coarse = reduce( np.add, np.ix_(*tmp_ext_coarse) )
 
1182
 
 
1183
    def f(X, params):
 
1184
        global feval
 
1185
        feval += 1
 
1186
        return np.dot(rnd_p,X)
 
1187
 
 
1188
    def test(testn, title, idx, view):
 
1189
        global feval
 
1190
        global nsucc
 
1191
        global nfail
 
1192
        feval = 0
 
1193
        TW.data = {}
 
1194
        TW.set_active_view(view)
 
1195
        out = TW[idx]
 
1196
        if view == 'full': A = Atest_ext_full
 
1197
        elif view == 'coarse': A = Atest_ext_coarse
 
1198
        if np.any(A[idx] != out) or np.any(A[idx].shape != out.shape):
 
1199
            print_fail(testn,title,msg='Different output - idx: ' + str(idx))
 
1200
            nfail += 1
 
1201
        elif feval != np.prod(np.unique(A[idx]).shape):
 
1202
            print_fail(testn,title,msg='Wrong number of function evaluations - idx: ' + str(idx))
 
1203
            nfail += 1
 
1204
        else:
 
1205
            print_ok(testn,title)
 
1206
            nsucc += 1
 
1207
    
 
1208
    TW.set_active_view('coarse')
 
1209
    TW.set_Q(2)
 
1210
    TW.set_active_view('full')
 
1211
    TW.set_Q(2)
 
1212
 
 
1213
    ########## 
 
1214
    # Full
 
1215
    TW.set_active_view('full')
 
1216
    TW.data = {}
 
1217
 
 
1218
    ###################################################################
 
1219
    # 01: Single address access
 
1220
    #
 
1221
    idx = (1,2,3,4)
 
1222
    feval = 0
 
1223
    out = TW[idx]
 
1224
    testn += 1
 
1225
    if Atest_ext_full[idx] != out:
 
1226
        print_fail(testn,"Sub view and PowQ - Single address access - full",msg='Different output')
 
1227
        nfail += 1
 
1228
    elif feval != 1:
 
1229
        print_fail(testn,"Sub view and PowQ - Single address access - full",msg='Wrong number of function evaluations')
 
1230
        nfail += 1
 
1231
    else:
 
1232
        print_ok(testn,"Sub view and PowQ - Single address access - full")
 
1233
        nsucc += 1
 
1234
 
 
1235
    ###################################################################
 
1236
    # Storage
 
1237
    testn += 1
 
1238
    TW.data = {}
 
1239
    store_location = "tw"
 
1240
    TW.store_location = store_location
 
1241
    TW[:,:,:,0]
 
1242
    TW.store(force=True)
 
1243
    print_ok(testn, "Sub view and PowQ - Storage - full")
 
1244
    nsucc += 1
 
1245
    # Reload
 
1246
    testn += 1
 
1247
    TW = TT.load(store_location)
 
1248
    TW.set_f(f,False)
 
1249
    idx = tuple( [slice(None,None,None)]*d )
 
1250
    test(testn,"Sub view and PowQ - Reload - full",idx, 'full')
 
1251
    os.remove(store_location + ".pkl")
 
1252
    os.remove(store_location + ".h5")
 
1253
    if os.path.isfile(store_location + ".pkl.old"):
 
1254
        os.remove(store_location + ".pkl.old")
 
1255
    if os.path.isfile(store_location + ".h5.old"):
 
1256
        os.remove(store_location + ".h5.old")
 
1257
 
 
1258
    ###################################################################
 
1259
    # Single slice
 
1260
    #
 
1261
    testn += 1
 
1262
    idx = (1,slice(None,None,None),3,4)
 
1263
    test(testn,"Sub view and PowQ - Single slice - full",idx, 'full')
 
1264
 
 
1265
    ###################################################################
 
1266
    # Partial slice
 
1267
    #
 
1268
    testn += 1
 
1269
    idx = (1,2,slice(1,3,1),4)
 
1270
    test(testn,"Sub view and PowQ - Partial slice - full",idx, 'full')
 
1271
 
 
1272
    ###################################################################
 
1273
    # Partial stepping slice
 
1274
    #
 
1275
    testn += 1
 
1276
    idx = (1,2,slice(0,4,2),4)
 
1277
    test(testn,"Sub view and PowQ - Partial stepping slice - full",idx, 'full')
 
1278
 
 
1279
    ###################################################################
 
1280
    # Multiple slice
 
1281
    #
 
1282
    testn += 1
 
1283
    idx = (1,slice(None,None,None),3,slice(0,4,2))
 
1284
    test(testn,"Sub view and PowQ - Multiple slice - full",idx, 'full')
 
1285
 
 
1286
    ###################################################################
 
1287
    # Full slice
 
1288
    #
 
1289
    testn += 1
 
1290
    idx = tuple([slice(None,None,None)] * len(shape))
 
1291
    test(testn,"Sub view and PowQ - Full slice - full",idx, 'full')
 
1292
 
 
1293
    ###################################################################
 
1294
    # List 
 
1295
    #
 
1296
    testn += 1
 
1297
    idx = ([0,1],[1,2],[1,3],[0,4])
 
1298
    test(testn,"Sub view and PowQ - Lists - full",idx, 'full')
 
1299
 
 
1300
    ###################################################################
 
1301
    # Single list 
 
1302
    #
 
1303
    testn += 1
 
1304
    idx = (0,1,[1,3],3)
 
1305
    test(testn,"Sub view and PowQ - Single list - full",idx, 'full')
 
1306
 
 
1307
    ###################################################################
 
1308
    # Double list 
 
1309
    #
 
1310
    testn += 1
 
1311
    idx = (0,[0,2],[1,3],3)
 
1312
    test(testn,"Sub view and PowQ - Double list - full",idx, 'full')
 
1313
 
 
1314
    ###################################################################
 
1315
    # Single list slice
 
1316
    #
 
1317
    testn += 1
 
1318
    idx = (0,[0,2],slice(None,None,None),3)
 
1319
    test(testn,"Sub view and PowQ - Single list slice - full",idx, 'full')
 
1320
 
 
1321
    testn += 1
 
1322
    idx = (0,slice(None,None,None),[0,2],3)
 
1323
    test(testn,"Sub view and PowQ - Single list slice - full",idx, 'full')
 
1324
 
 
1325
    testn += 1
 
1326
    idx = (slice(None,None,None),0,[0,2,3],3)
 
1327
    test(testn,"Sub view and PowQ - Single list slice - full",idx, 'full')
 
1328
 
 
1329
    ###################################################################
 
1330
    # Double list slice
 
1331
    #
 
1332
    testn += 1
 
1333
    idx = ([0,1],slice(None,None,None),[0,2],3)
 
1334
    test(testn,"Sub view and PowQ - Double list slice - full",idx, 'full')
 
1335
 
 
1336
    testn += 1
 
1337
    idx = (slice(None,None,None),0,slice(None,None,None),[0,2,3])
 
1338
    test(testn,"Sub view and PowQ - Double slice list - full",idx, 'full')
 
1339
 
 
1340
    ###################################################################
 
1341
    # Lists slice
 
1342
    #
 
1343
    testn += 1
 
1344
    idx = ([0,1],[0,2],slice(None,None,None),[1,3])
 
1345
    test(testn,"Sub view and PowQ - Lists slice - full",idx, 'full')
 
1346
 
 
1347
    ###################################################################
 
1348
    # Fix indices
 
1349
    #
 
1350
    testn += 1
 
1351
    fix_idxs = [0,2]
 
1352
    fix_dims = [0,2]
 
1353
    Atmp = Atest_ext_full.copy()
 
1354
    Atest_ext_full = Atest_ext_full[ tuple([ fix_idxs[fix_dims.index(i)] if (i in fix_dims) else slice(None,None,None) for i in range(d) ]) ]
 
1355
    TW.fix_indices(fix_idxs, fix_dims)
 
1356
    idx = [ slice(None,None,None) ] * TW.ndim
 
1357
    test(testn, "Sub view and PowQ - Fix indices - full", idx, 'full')
 
1358
 
 
1359
    ###################################################################
 
1360
    # Release indices
 
1361
    #
 
1362
    testn += 1
 
1363
    Atest_ext_full = Atmp.copy()
 
1364
    TW.release_indices()
 
1365
    idx = [ slice(None,None,None) ] * TW.ndim
 
1366
    test(testn, "Sub view and PowQ - Release indices - full",idx, 'full')
 
1367
 
 
1368
    ###################################################################
 
1369
    # Fix indices 2
 
1370
    #
 
1371
    testn += 1
 
1372
    fix_idxs = [0]
 
1373
    fix_dims = [0]
 
1374
    Atmp = Atest_ext_full.copy()
 
1375
    Atest_ext_full = Atest_ext_full[ tuple([ fix_idxs[fix_dims.index(i)] if (i in fix_dims) else slice(None,None,None) for i in range(d) ]) ]
 
1376
    TW.fix_indices(fix_idxs, fix_dims)
 
1377
    idx = [ slice(None,None,None) ] + [(0,1)] * (TW.ndim-1)
 
1378
    test(testn, "Sub view and PowQ - Fix indices - second test - full",idx, 'full')
 
1379
 
 
1380
    ###################################################################
 
1381
    # Release indices
 
1382
    #
 
1383
    testn += 1
 
1384
    Atest_ext_full = Atmp.copy()
 
1385
    TW.release_indices()
 
1386
    idx = [ slice(None,None,None) ] * TW.ndim
 
1387
    test(testn, "Sub view and PowQ - Release indices - full",idx, 'full')
 
1388
 
 
1389
 
 
1390
    ##########
 
1391
    # Coarse
 
1392
    TW.set_active_view('coarse')
 
1393
    TW.data = {}
 
1394
    
 
1395
    ###################################################################
 
1396
    # 01: Single address access
 
1397
    #
 
1398
    idx = (1,2,3,4)
 
1399
    feval = 0
 
1400
    out = TW[idx]
 
1401
    testn += 1
 
1402
    if Atest_ext_coarse[idx] != out:
 
1403
        print_fail(testn,"Sub view and PowQ - Single address access - coarse",msg='Different output')
 
1404
        nfail += 1
 
1405
    elif feval != 1:
 
1406
        print_fail(testn,"Sub view and PowQ - Single address access - coarse",msg='Wrong number of function evaluations')
 
1407
        nfail += 1
 
1408
    else:
 
1409
        print_ok(testn,"Sub view and PowQ - Single address access - coarse")
 
1410
        nsucc += 1
 
1411
 
 
1412
    ###################################################################
 
1413
    # Storage
 
1414
    testn += 1
 
1415
    TW.data = {}
 
1416
    store_location = "tw"
 
1417
    TW.store_location = store_location
 
1418
    TW[:,:,:,0]
 
1419
    TW.store(force=True)
 
1420
    print_ok(testn, "Sub view and PowQ - Storage - coarse")
 
1421
    nsucc += 1
 
1422
    # Reload
 
1423
    testn += 1
 
1424
    TW = TT.load(store_location)
 
1425
    TW.set_f(f,False)
 
1426
    idx = tuple( [slice(None,None,None)]*d )
 
1427
    test(testn,"Sub view and PowQ - Reload - coarse",idx, 'coarse')
 
1428
    os.remove(store_location + ".pkl")
 
1429
    os.remove(store_location + ".h5")
 
1430
    if os.path.isfile(store_location + ".pkl.old"):
 
1431
        os.remove(store_location + ".pkl.old")
 
1432
    if os.path.isfile(store_location + ".h5.old"):
 
1433
        os.remove(store_location + ".h5.old")
 
1434
 
 
1435
    ###################################################################
 
1436
    # Single slice
 
1437
    #
 
1438
    testn += 1
 
1439
    idx = (1,slice(None,None,None),3,4)
 
1440
    test(testn,"Sub view and PowQ - Single slice - coarse",idx, 'coarse')
 
1441
 
 
1442
    ###################################################################
 
1443
    # Partial slice
 
1444
    #
 
1445
    testn += 1
 
1446
    idx = (1,2,slice(1,3,1),4)
 
1447
    test(testn,"Sub view and PowQ - Partial slice - coarse",idx, 'coarse')
 
1448
 
 
1449
    ###################################################################
 
1450
    # Partial stepping slice
 
1451
    #
 
1452
    testn += 1
 
1453
    idx = (1,2,slice(0,4,2),4)
 
1454
    test(testn,"Sub view and PowQ - Partial stepping slice - coarse",idx, 'coarse')
 
1455
 
 
1456
    ###################################################################
 
1457
    # Multiple slice
 
1458
    #
 
1459
    testn += 1
 
1460
    idx = (1,slice(None,None,None),3,slice(0,4,2))
 
1461
    test(testn,"Sub view and PowQ - Multiple slice - coarse",idx, 'coarse')
 
1462
 
 
1463
    ###################################################################
 
1464
    # Coarse slice
 
1465
    #
 
1466
    testn += 1
 
1467
    idx = tuple([slice(None,None,None)] * len(shape))
 
1468
    test(testn,"Sub view and PowQ - Coarse slice - coarse",idx, 'coarse')
 
1469
 
 
1470
    ###################################################################
 
1471
    # List 
 
1472
    #
 
1473
    testn += 1
 
1474
    idx = ([0,1],[1,2],[1,3],[0,4])
 
1475
    test(testn,"Sub view and PowQ - Lists - coarse",idx, 'coarse')
 
1476
 
 
1477
    ###################################################################
 
1478
    # Single list 
 
1479
    #
 
1480
    testn += 1
 
1481
    idx = (0,1,[1,3],3)
 
1482
    test(testn,"Sub view and PowQ - Single list - coarse",idx, 'coarse')
 
1483
 
 
1484
    ###################################################################
 
1485
    # Double list 
 
1486
    #
 
1487
    testn += 1
 
1488
    idx = (0,[0,2],[1,3],3)
 
1489
    test(testn,"Sub view and PowQ - Double list - coarse",idx, 'coarse')
 
1490
 
 
1491
    ###################################################################
 
1492
    # Single list slice
 
1493
    #
 
1494
    testn += 1
 
1495
    idx = (0,[0,2],slice(None,None,None),3)
 
1496
    test(testn,"Sub view and PowQ - Single list slice - coarse",idx, 'coarse')
 
1497
 
 
1498
    testn += 1
 
1499
    idx = (0,slice(None,None,None),[0,2],3)
 
1500
    test(testn,"Sub view and PowQ - Single list slice - coarse",idx, 'coarse')
 
1501
 
 
1502
    testn += 1
 
1503
    idx = (slice(None,None,None),0,[0,2,3],3)
 
1504
    test(testn,"Sub view and PowQ - Single list slice - coarse",idx, 'coarse')
 
1505
 
 
1506
    ###################################################################
 
1507
    # Double list slice
 
1508
    #
 
1509
    testn += 1
 
1510
    idx = ([0,1],slice(None,None,None),[0,2],3)
 
1511
    test(testn,"Sub view and PowQ - Double list slice - coarse",idx, 'coarse')
 
1512
 
 
1513
    testn += 1
 
1514
    idx = (slice(None,None,None),0,slice(None,None,None),[0,2,3])
 
1515
    test(testn,"Sub view and PowQ - Double slice list - coarse",idx, 'coarse')
 
1516
 
 
1517
    ###################################################################
 
1518
    # Lists slice
 
1519
    #
 
1520
    testn += 1
 
1521
    idx = ([0,1],[0,2],slice(None,None,None),[1,3])
 
1522
    test(testn,"Sub view and PowQ - Lists slice - coarse",idx, 'coarse')
 
1523
 
 
1524
    ###################################################################
 
1525
    # Fix indices
 
1526
    #
 
1527
    testn += 1
 
1528
    fix_idxs = [0,2]
 
1529
    fix_dims = [0,2]
 
1530
    Atmp = Atest_ext_coarse.copy()
 
1531
    Atest_ext_coarse = Atest_ext_coarse[ tuple([ fix_idxs[fix_dims.index(i)] if (i in fix_dims) else slice(None,None,None) for i in range(d) ]) ]
 
1532
    TW.fix_indices(fix_idxs, fix_dims)
 
1533
    idx = [ slice(None,None,None) ] * TW.ndim
 
1534
    test(testn, "Sub view and PowQ - Fix indices - coarse", idx, 'coarse')
 
1535
 
 
1536
    ###################################################################
 
1537
    # Release indices
 
1538
    #
 
1539
    testn += 1
 
1540
    Atest_ext_coarse = Atmp.copy()
 
1541
    TW.release_indices()
 
1542
    idx = [ slice(None,None,None) ] * TW.ndim
 
1543
    test(testn, "Sub view and PowQ - Release indices - coarse",idx, 'coarse')
 
1544
 
 
1545
    ###################################################################
 
1546
    # Fix indices 2
 
1547
    #
 
1548
    testn += 1
 
1549
    fix_idxs = [0]
 
1550
    fix_dims = [0]
 
1551
    Atmp = Atest_ext_coarse.copy()
 
1552
    Atest_ext_coarse = Atest_ext_coarse[ tuple([ fix_idxs[fix_dims.index(i)] if (i in fix_dims) else slice(None,None,None) for i in range(d) ]) ]
 
1553
    TW.fix_indices(fix_idxs, fix_dims)
 
1554
    idx = [ slice(None,None,None) ] + [(0,1)] * (TW.ndim-1)
 
1555
    test(testn, "Sub view and PowQ - Fix indices - second test - coarse",idx, 'coarse')
 
1556
 
 
1557
    ###################################################################
 
1558
    # Release indices
 
1559
    #
 
1560
    testn += 1
 
1561
    Atest_ext_coarse = Atmp.copy()
 
1562
    TW.release_indices()
 
1563
    idx = [ slice(None,None,None) ] * TW.ndim
 
1564
    test(testn, "Sub view and PowQ - Release indices - coarse",idx, 'coarse')
 
1565
 
 
1566
    ######################################################
 
1567
    # Sub view and Reshaped PowQ
 
1568
    ######################################################
 
1569
    TW.data = {}
 
1570
    newshape_full = [Q]*int(math.log(np.prod(ext_full_shape),Q))
 
1571
    newshape_coarse = [Q]*int(math.log(np.prod(ext_coarse_shape),Q))
 
1572
    Atest_ext_full_shape = Atest_ext_full.copy()
 
1573
    Atest_ext_full = np.reshape( Atest_ext_full, newshape_full )
 
1574
    TW.set_active_view('full')
 
1575
    TW.reshape( newshape_full )
 
1576
    Atest_ext_coarse_shape = Atest_ext_coarse.copy()
 
1577
    Atest_ext_coarse = np.reshape( Atest_ext_coarse, newshape_coarse )
 
1578
    TW.set_active_view('coarse')
 
1579
    TW.reshape( newshape_coarse )
 
1580
 
 
1581
    ########## 
 
1582
    # Full
 
1583
    TW.set_active_view('full')
 
1584
    TW.data = {}
 
1585
 
 
1586
    ###################################################################
 
1587
    # Reshaped PowQ - Single slice
 
1588
    #
 
1589
    testn += 1
 
1590
    idx = (0,slice(None,None,None)) + tuple([ random.randint(0,Q-1) for i in range(len(newshape_full)-2) ])
 
1591
    test(testn,"Reshaped PowQ - Single slice - full",idx,'full')
 
1592
 
 
1593
    ###################################################################
 
1594
    # Reshaped PowQ - Partial slice
 
1595
    #
 
1596
    testn += 1
 
1597
    idx = (0,slice(0,1,1)) + tuple([ random.randint(0,Q-1) for i in range(len(newshape_full)-2) ])
 
1598
    test(testn,"Reshaped PowQ - Partial slice - full",idx,'full')
 
1599
 
 
1600
    ###################################################################
 
1601
    # Reshaped PowQ - Multiple slice
 
1602
    #
 
1603
    testn += 1
 
1604
    idx = (slice(0,1,1),0,slice(None,None,None)) + tuple([ random.randint(0,Q-1) for i in range(len(newshape_full)-3) ])
 
1605
    test(testn,"Reshaped PowQ - Multiple slice - full",idx,'full')
 
1606
 
 
1607
    ###################################################################
 
1608
    # Reshaped PowQ - Full slice
 
1609
    #
 
1610
    testn += 1
 
1611
    idx = tuple([slice(None,None,None)] * len(Atest_ext_full.shape))
 
1612
    test(testn,"Reshaped PowQ - Full slice - full",idx,'full')
 
1613
 
 
1614
    ###################################################################
 
1615
    # Reshaped PowQ - List 
 
1616
    #
 
1617
    testn += 1
 
1618
    nn = 2
 
1619
    idx = tuple( [ [random.randint(0,Q-1) for j in range(nn)] for i in range(len(newshape_full)) ] )
 
1620
    test(testn,"Reshaped PowQ - Lists - full",idx,'full')
 
1621
 
 
1622
    ###################################################################
 
1623
    # Reshaped PowQ - Single list 
 
1624
    #
 
1625
    testn += 1
 
1626
    nn = 3
 
1627
    idx = (1,[random.randint(0,Q-1) for j in range(nn)]) + tuple([ random.randint(0,Q-1) for i in range(len(newshape_full)-2) ])
 
1628
    test(testn,"Reshaped PowQ - Single list - full",idx,'full')
 
1629
 
 
1630
    ###################################################################
 
1631
    # Reshaped PowQ - Double list 
 
1632
    #
 
1633
    testn += 1
 
1634
    nn = 2
 
1635
    idx = (1,[random.randint(0,Q-1) for j in range(nn)],[random.randint(0,Q-1) for j in range(nn)]) + tuple([ random.randint(0,Q-1) for i in range(len(newshape_full)-3) ])
 
1636
    test(testn,"Reshaped PowQ - Double list - full",idx,'full')
 
1637
 
 
1638
    ###################################################################
 
1639
    # Reshaped PowQ - Single list slice
 
1640
    #
 
1641
    testn += 1
 
1642
    nn = 2
 
1643
    idx = (1,slice(None,None,None),[random.randint(0,Q-1) for j in range(nn)]) + tuple([ random.randint(0,Q-1) for i in range(len(newshape_full)-3) ])
 
1644
    test(testn,"Reshaped PowQ - Single list slice - full",idx,'full')
 
1645
 
 
1646
    testn += 1
 
1647
    nn = 3
 
1648
    idx = (1,[random.randint(0,Q-1) for j in range(nn)],slice(None,None,None)) + tuple([ random.randint(0,Q-1) for i in range(len(newshape_full)-3) ])
 
1649
    test(testn,"Reshaped PowQ - Single list slice - full",idx,'full')
 
1650
 
 
1651
    testn += 1
 
1652
    nn = 3
 
1653
    idx = (1,[random.randint(0,Q-1) for j in range(nn)],0,slice(None,None,None)) + tuple([ random.randint(0,Q-1) for i in range(len(newshape_full)-4) ])
 
1654
    test(testn,"Reshaped PowQ - Single list slice - full",idx,'full')
 
1655
 
 
1656
    ###################################################################
 
1657
    # Reshaped PowQ - Double list slice
 
1658
    #
 
1659
    testn += 1
 
1660
    nn = 3
 
1661
    idx = (1,[random.randint(0,Q-1) for j in range(nn)],slice(None,None,None),[random.randint(0,Q-1) for j in range(nn)]) + tuple([ random.randint(0,Q-1) for i in range(len(newshape_full)-4) ])
 
1662
    test(testn,"Reshaped PowQ - Double list slice - full",idx,'full')
 
1663
 
 
1664
    testn += 1
 
1665
    nn = 3
 
1666
    idx = (slice(None,None,None),0,slice(None,None,None),[random.randint(0,Q-1) for j in range(nn)]) + tuple([ random.randint(0,Q-1) for i in range(len(newshape_full)-4) ])
 
1667
    test(testn,"Reshaped PowQ - Double slice list - full",idx,'full')
 
1668
 
 
1669
    ###################################################################
 
1670
    # Reshaped PowQ - Lists slice
 
1671
    #
 
1672
    testn += 1
 
1673
    nn = 3
 
1674
    idx = ([random.randint(0,Q-1) for j in range(nn)],[random.randint(0,Q-1) for j in range(nn)],slice(None,None,None)) + tuple( [ [random.randint(0,Q-1) for j in range(nn)] for i in range(len(newshape_full)-3) ] )
 
1675
    test(testn,"Reshaped PowQ - Lists slice - full",idx,'full')
 
1676
 
 
1677
    ###################################################################
 
1678
    # Reshaped PowQ - Fix indices
 
1679
    #
 
1680
    testn += 1
 
1681
    fix_idxs = [0,0,1]
 
1682
    fix_dims = [0,3,2]
 
1683
    Atmp = Atest_ext_full.copy()
 
1684
    Atest_ext_full = Atest_ext_full[ tuple([ fix_idxs[fix_dims.index(i)] if (i in fix_dims) else slice(None,None,None) for i in range(d) ]) ]
 
1685
    TW.fix_indices(fix_idxs, fix_dims)
 
1686
    idx = [ slice(None,None,None) ] * TW.ndim
 
1687
    test(testn, "Reshaped PowQ - Fix indices - full",idx,'full')
 
1688
 
 
1689
    ###################################################################
 
1690
    # Reshaped PowQ - Release indices
 
1691
    #
 
1692
    testn += 1
 
1693
    Atest_ext_full = Atmp.copy()
 
1694
    TW.release_indices()
 
1695
    idx = [ slice(None,None,None) ] * TW.ndim
 
1696
    test(testn, "Reshaped PowQ - Release indices - full",idx,'full')
 
1697
 
 
1698
    ###################################################################
 
1699
    # Reshaped PowQ - Fix indices 2
 
1700
    #
 
1701
    testn += 1
 
1702
    fix_idxs = [0]
 
1703
    fix_dims = [0]
 
1704
    Atmp = Atest_ext_full.copy()
 
1705
    Atest_ext_full = Atest_ext_full[ tuple([ fix_idxs[fix_dims.index(i)] if (i in fix_dims) else slice(None,None,None) for i in range(d) ]) ]
 
1706
    TW.fix_indices(fix_idxs, fix_dims)
 
1707
    idx = [ slice(None,None,None) ] + [(0,1)] * (TW.ndim-1)
 
1708
    test(testn, "Reshaped PowQ - Fix indices - second test - full",idx,'full')
 
1709
 
 
1710
    ###################################################################
 
1711
    # Reshaped PowQ - Release indices
 
1712
    #
 
1713
    testn += 1
 
1714
    Atest_ext_full = Atmp.copy()
 
1715
    TW.release_indices()
 
1716
    idx = [ slice(None,None,None) ] * TW.ndim
 
1717
    test(testn, "Reshaped PowQ - Release indices - full",idx,'full')
 
1718
 
 
1719
    ###################################################################
 
1720
    # Reshaped PowQ - Restore original shape
 
1721
    #
 
1722
    testn += 1
 
1723
    Atest_ext_full = Atest_ext_full_shape.copy()
 
1724
    TW.reset_ghost_shape()
 
1725
    idx = [ slice(None,None,None) ] * TW.ndim
 
1726
    test(testn, "Reshaped PowQ - Restore original shape - full",idx,'full')
 
1727
 
 
1728
    ########## 
 
1729
    # Coarse
 
1730
    TW.set_active_view('coarse')
 
1731
    TW.data = {}
 
1732
 
 
1733
    ###################################################################
 
1734
    # Reshaped PowQ - Single slice
 
1735
    #
 
1736
    testn += 1
 
1737
    idx = (0,slice(None,None,None)) + tuple([ random.randint(0,Q-1) for i in range(len(newshape_coarse)-2) ])
 
1738
    test(testn,"Reshaped PowQ - Single slice - coarse",idx,'coarse')
 
1739
 
 
1740
    ###################################################################
 
1741
    # Reshaped PowQ - Partial slice
 
1742
    #
 
1743
    testn += 1
 
1744
    idx = (0,slice(0,1,1)) + tuple([ random.randint(0,Q-1) for i in range(len(newshape_coarse)-2) ])
 
1745
    test(testn,"Reshaped PowQ - Partial slice - coarse",idx,'coarse')
 
1746
 
 
1747
    ###################################################################
 
1748
    # Reshaped PowQ - Multiple slice
 
1749
    #
 
1750
    testn += 1
 
1751
    idx = (slice(0,1,1),0,slice(None,None,None)) + tuple([ random.randint(0,Q-1) for i in range(len(newshape_coarse)-3) ])
 
1752
    test(testn,"Reshaped PowQ - Multiple slice - coarse",idx,'coarse')
 
1753
 
 
1754
    ###################################################################
 
1755
    # Reshaped PowQ - Coarse slice
 
1756
    #
 
1757
    testn += 1
 
1758
    idx = tuple([slice(None,None,None)] * len(Atest_ext_coarse.shape))
 
1759
    test(testn,"Reshaped PowQ - Coarse slice - coarse",idx,'coarse')
 
1760
 
 
1761
    ###################################################################
 
1762
    # Reshaped PowQ - List 
 
1763
    #
 
1764
    testn += 1
 
1765
    nn = 2
 
1766
    idx = tuple( [ [random.randint(0,Q-1) for j in range(nn)] for i in range(len(newshape_coarse)) ] )
 
1767
    test(testn,"Reshaped PowQ - Lists - coarse",idx,'coarse')
 
1768
 
 
1769
    ###################################################################
 
1770
    # Reshaped PowQ - Single list 
 
1771
    #
 
1772
    testn += 1
 
1773
    nn = 3
 
1774
    idx = (1,[random.randint(0,Q-1) for j in range(nn)]) + tuple([ random.randint(0,Q-1) for i in range(len(newshape_coarse)-2) ])
 
1775
    test(testn,"Reshaped PowQ - Single list - coarse",idx,'coarse')
 
1776
 
 
1777
    ###################################################################
 
1778
    # Reshaped PowQ - Double list 
 
1779
    #
 
1780
    testn += 1
 
1781
    nn = 2
 
1782
    idx = (1,[random.randint(0,Q-1) for j in range(nn)],[random.randint(0,Q-1) for j in range(nn)]) + tuple([ random.randint(0,Q-1) for i in range(len(newshape_coarse)-3) ])
 
1783
    test(testn,"Reshaped PowQ - Double list - coarse",idx,'coarse')
 
1784
 
 
1785
    ###################################################################
 
1786
    # Reshaped PowQ - Single list slice
 
1787
    #
 
1788
    testn += 1
 
1789
    nn = 2
 
1790
    idx = (1,slice(None,None,None),[random.randint(0,Q-1) for j in range(nn)]) + tuple([ random.randint(0,Q-1) for i in range(len(newshape_coarse)-3) ])
 
1791
    test(testn,"Reshaped PowQ - Single list slice - coarse",idx,'coarse')
 
1792
 
 
1793
    testn += 1
 
1794
    nn = 3
 
1795
    idx = (1,[random.randint(0,Q-1) for j in range(nn)],slice(None,None,None)) + tuple([ random.randint(0,Q-1) for i in range(len(newshape_coarse)-3) ])
 
1796
    test(testn,"Reshaped PowQ - Single list slice - coarse",idx,'coarse')
 
1797
 
 
1798
    testn += 1
 
1799
    nn = 3
 
1800
    idx = (1,[random.randint(0,Q-1) for j in range(nn)],0,slice(None,None,None)) + tuple([ random.randint(0,Q-1) for i in range(len(newshape_coarse)-4) ])
 
1801
    test(testn,"Reshaped PowQ - Single list slice - coarse",idx,'coarse')
 
1802
 
 
1803
    ###################################################################
 
1804
    # Reshaped PowQ - Double list slice
 
1805
    #
 
1806
    testn += 1
 
1807
    nn = 3
 
1808
    idx = (1,[random.randint(0,Q-1) for j in range(nn)],slice(None,None,None),[random.randint(0,Q-1) for j in range(nn)]) + tuple([ random.randint(0,Q-1) for i in range(len(newshape_coarse)-4) ])
 
1809
    test(testn,"Reshaped PowQ - Double list slice - coarse",idx,'coarse')
 
1810
 
 
1811
    testn += 1
 
1812
    nn = 3
 
1813
    idx = (slice(None,None,None),0,slice(None,None,None),[random.randint(0,Q-1) for j in range(nn)]) + tuple([ random.randint(0,Q-1) for i in range(len(newshape_coarse)-4) ])
 
1814
    test(testn,"Reshaped PowQ - Double slice list - coarse",idx,'coarse')
 
1815
 
 
1816
    ###################################################################
 
1817
    # Reshaped PowQ - Lists slice
 
1818
    #
 
1819
    testn += 1
 
1820
    nn = 3
 
1821
    idx = ([random.randint(0,Q-1) for j in range(nn)],[random.randint(0,Q-1) for j in range(nn)],slice(None,None,None)) + tuple( [ [random.randint(0,Q-1) for j in range(nn)] for i in range(len(newshape_coarse)-3) ] )
 
1822
    test(testn,"Reshaped PowQ - Lists slice - coarse",idx,'coarse')
 
1823
 
 
1824
    ###################################################################
 
1825
    # Reshaped PowQ - Fix indices
 
1826
    #
 
1827
    testn += 1
 
1828
    fix_idxs = [0,0,1]
 
1829
    fix_dims = [0,3,2]
 
1830
    Atmp = Atest_ext_coarse.copy()
 
1831
    Atest_ext_coarse = Atest_ext_coarse[ tuple([ fix_idxs[fix_dims.index(i)] if (i in fix_dims) else slice(None,None,None) for i in range(d) ]) ]
 
1832
    TW.fix_indices(fix_idxs, fix_dims)
 
1833
    idx = [ slice(None,None,None) ] * TW.ndim
 
1834
    test(testn, "Reshaped PowQ - Fix indices - coarse",idx,'coarse')
 
1835
 
 
1836
    ###################################################################
 
1837
    # Reshaped PowQ - Release indices
 
1838
    #
 
1839
    testn += 1
 
1840
    Atest_ext_coarse = Atmp.copy()
 
1841
    TW.release_indices()
 
1842
    idx = [ slice(None,None,None) ] * TW.ndim
 
1843
    test(testn, "Reshaped PowQ - Release indices - coarse",idx,'coarse')
 
1844
 
 
1845
    ###################################################################
 
1846
    # Reshaped PowQ - Fix indices 2
 
1847
    #
 
1848
    testn += 1
 
1849
    fix_idxs = [0]
 
1850
    fix_dims = [0]
 
1851
    Atmp = Atest_ext_coarse.copy()
 
1852
    Atest_ext_coarse = Atest_ext_coarse[ tuple([ fix_idxs[fix_dims.index(i)] if (i in fix_dims) else slice(None,None,None) for i in range(d) ]) ]
 
1853
    TW.fix_indices(fix_idxs, fix_dims)
 
1854
    idx = [ slice(None,None,None) ] + [(0,1)] * (TW.ndim-1)
 
1855
    test(testn, "Reshaped PowQ - Fix indices - second test - coarse",idx,'coarse')
 
1856
 
 
1857
    ###################################################################
 
1858
    # Reshaped PowQ - Release indices
 
1859
    #
 
1860
    testn += 1
 
1861
    Atest_ext_coarse = Atmp.copy()
 
1862
    TW.release_indices()
 
1863
    idx = [ slice(None,None,None) ] * TW.ndim
 
1864
    test(testn, "Reshaped PowQ - Release indices - coarse",idx,'coarse')
 
1865
 
 
1866
    ###################################################################
 
1867
    # Reshaped PowQ - Restore original shape
 
1868
    #
 
1869
    testn += 1
 
1870
    Atest_ext_coarse = Atest_ext_coarse_shape.copy()
 
1871
    TW.reset_ghost_shape()
 
1872
    idx = [ slice(None,None,None) ] * TW.ndim
 
1873
    test(testn, "Reshaped PowQ - Restore original shape - coarse",idx,'coarse')
 
1874
 
 
1875
 
 
1876
    #############################################################################
 
1877
    #############################################################################
 
1878
 
1001
1879
    print_summary("TensorWrapper", nsucc, nfail)
1002
1880
 
1003
1881
    return (nsucc,nfail)