-
Notifications
You must be signed in to change notification settings - Fork 0
/
2D-CVM-perturb-expt-1-0-2018-01-07.py
4291 lines (3381 loc) · 221 KB
/
2D-CVM-perturb-expt-1-0-2018-01-07.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
####################################################################################################
# Alianna J. Maren
# Computing configuration variables for the Cluster Variation Method
####################################################################################################
# Import the following Python packages
import random
import itertools
import numpy as np
import pylab
import matplotlib
from math import exp
from math import log
from matplotlib import pyplot as plt
from random import randrange, uniform #(not sure this is needed, since I'm importing random)
####################################################################################################
####################################################################################################
#
# Detailed code documentation is JUST ABOVE main(), at the very end of this program.
#
####################################################################################################
####################################################################################################
#
#
# Jan. 7, 2018: Beginning work on perturbations analysis: Goal is to show that in the realm of
# h > 1 (like-with-like nearest neighbor pairings encouraged), it will be possible to return to
# a prior state even after perturbation, so that there is greater likelihood of pattern
# persistence as compared with systems where the interaction energy is 0 (h = 1).
#
# Steps are:
# 1) Create unitArray as with previous versions of code; this will be for a single set of
# x1 and h values; the unitArray will adjust to achieve free energy minimum,
# 2) Perturb the unit array by randomly changing a prescribed amount of nodes,
# 3) Allow the unitArray to come to a free energy minimum again; compare actual node (on/off)
# patterns vs. original
# Jan. 4, 2018: Found/fixed bug in communicating updated unitArray to the next calling function
#
# Dec. 17, 2017: This program generates a random population of nodes for a 2-D CVM according to a
# pre-specified ratio of x1 to x2.
# To adjust the ratio, tweak the parameter x-ratio in main.
#
#
# Dec. 17, 2017: This particular version of the program is simpler than the documentation below
# suggests. It DOES NOT use the h-values, except as a means for stepping through a loop.
# All that it does is randomly populate a 2-D CVM grid, with a random generation of
# "A" and "B" (x1 and x2) nodes, in a ratio that can be specified inside **main**.
# Fairly often, this random generation does not yield the precise ratio of x1 & x2, so it does an
# "adjustment step."
# During this adjustment (currently set to a max jrange, specified in **main**), it will look for
# a node to flip to alter the x1 / x2 ratio. If it succeeds in finding a "flippable" node (one that
# brings the x1 and x2 proportions closer to desired ratio), then it keeps the flip.
#
# This specific version of the code computes a randomly-generated distribution of x1 / x2 values,
# dependent on the h-parameter. Then, it computes the configuration variables for the grid. Based on
# these, it then computes the entropy, enthalpy, and free energy values.
#
# The crucial equations are as follows (taken from AJM's 2014 paper, "The Cluster Variation Method II:
# 2-D Grid of Zigzag Chains":
# h = exp(beta*epsilon/4) & lambda = 0 (Beginning of Appendix B, replicating Eqn. 2-16.)
# We can set beta = Boltzmann's constant = 1.
# Thus, eps1 = epsilon = 4*log(h)
# For the equilibrium case (which is where we have an analytic solution), eps0 = 0.
# Thus, x1 = x2 = 0; h controls the distribution among the z, w, & y values.
# At equilibrium, when eps1 = 0, z1 = z6 = z3 = z4 = 0.125; z2 = z5 = 0.125, however,
# they are "degenerate" so their total values each = 0.25.
#
# I am using a revised enthalpy equation (cf the original 2016 Brain Sciences paper);
# the new equation uses: 2y2-y1-y3.
#
# The following equations are taken from the 2-D CVM analtyic solution.
# They are valid ONLY for the equiprobable case; x1 = x2.
# These equations are put in here for reference; they are not used in the current version of code.
#
# The equations are taken from my 2014 Technical Report on the 2-D CVM.
# THEY NEED REVISION to account for the different enthalpy equation that I'm using now.
# hSquared = h*h
# hFourth = hSquared*hSquared
# denom = 8.0*(hFourth - 6.0*hSquared + 1.0)
# z3Analytic = (hSquared - 3.0)*(hSquared + 1.0)/denom # App. B, Eqn. 29
# z1Analytic = (1.0 - 3.0*hSquared)*(hSquared + 1.0)/denom # App. B, Eqn. 30
# y1Analytic = z1Analytic + 0.5*(0.5 - z1Analytic - z3Analytic)
# y2Analytic = z3Analytic + 0.5*(0.5 - z1Analytic - z3Analytic)
#
####################################################################################################
####################################################################################################
#
# Procedure to welcome the user and identify the code
#
####################################################################################################
####################################################################################################
def welcome ():
print
print '******************************************************************************'
print
print 'Welcome to the 2-D Cluster Variation Method'
print 'Version 1.2, 01/07/2018, A.J. Maren'
print 'This version computes the behavior of a perturbed unitArray,'
print ' based on minimizing the free energy both before and after perturbation.'
print ' '
print 'By changing parameters in the main code, the user can select:'
print ' x1 value - relative proportion of A (on) vs. B (off) nodes'
print ' h value - controls the interaction energy, thus affects the '
print ' y, w, and z configuration values for a given x, and'
print ' perturbPrcnt - the percentage of nodes in the unitArray that'
print ' will be flipped from one state to another.'
print ' '
print 'For comments, questions, or bug-fixes, contact: alianna.maren@northwestern.edu'
print 'Alternate email address: alianna@aliannajmaren.com'
print ' '
print ' NOTE: In these calculations, x1 = A (units are at value 1),'
print ' and x2 = B (units are at value 0).'
print
print '******************************************************************************'
print
return()
####################################################################################################
####################################################################################################
#
# Function to obtain the array size specifications (currently DEFINED for the user; not a choice)
#
# Note: The code is ONLY set up to work with a grid consisting of an EVEN number of rows
#
####################################################################################################
####################################################################################################
def obtainArraySizeSpecs ():
# x = input('Enter arraylength: ')
# arraylength = int(x)
# print 'arraylength is', arraylength
# x = input('Enter layers: ')
# layers = int(x)
# print 'layers is', layers
arraylength = 16
layers = 16
arraySizeList = (arraylength, layers)
return (arraySizeList)
####################################################################################################
####################################################################################################
#
# Function to randomly-generate an array, and then permute it to achieve the desired
# z1 & z3 values.
#
# Inputs: arraySizeList: a list of two integers; arrayLength and layers
# h: the interaction enthalpy parameter
# Return: the matrix unit_array, a matrix of 0's and 1's.
#
####################################################################################################
####################################################################################################
def initializeGeneratedMatrix (arraySizeList, h, x1TargetVal):
# The current version of the program has the h-value passed into this routine.
# It is not used, except to compute the "analytic" values for the configuration variables -
# which are relevant only in the equiprobable case.
# Later versions of this program will accept h in order to adjust the configuration variables by
# changing the grid population.
localArrayLength = arraySizeList[0]
localArrayLayers = arraySizeList[1]
# Create the matrix 'unit_array' so that it has a random population of 0's and 1's.
# unit_array = np.random.choice([0, 1],size=(localArrayLayers,localArrayLength)) # Create an array filled with random values
# Note: this function can be used to create proportional distributions: np.random.choice([0, 1], size=(10,), p=[1./3, 2./3])
x2TargetVal = 1.-x1TargetVal
unitArray = np.random.choice([0, 1],size=(localArrayLayers,localArrayLength), p=[x2TargetVal, x1TargetVal])
return unitArray
####################################################################################################
####################################################################################################
#
# Procedure to initialize the matrix with a randomly-generate an array, and then permute it to
# achieve the desired z1 & z3 values. This is NOT part of the "permutation" experiments;
# it is getting the first unitArray to be one which is at equilibrium for a given set of
# x and h-values.
#
# Inputs: arraySizeList: a list of two integers; arrayLength and layers
# h: the interaction enthalpy parameter
# x1TargetVal: the desired x1 in the unitArray; the first rough pass
# is randomly generated, then refined until the fraction x1 is
# within a tolerance (specified in **main** as maxXDif) of the desired x1.
# maxXDif: the maximum difference allowed for the x1 from the randomly-generated
# array and the desired x1.
# Return: the matrix unit_array, a matrix of 0's and 1's.
#
####################################################################################################
####################################################################################################
def initializeMatrix (arraySizeList, h, x1TargetVal, maxXDif):
localArrayLength = arraySizeList[0]
localArrayLayers = arraySizeList[1]
# Note: The passed value patternProb is used to determine if we are returning a stored pattern, or
# are probabilistically-generating our data.
# If patternProb = 0: probabilistic generation, dependent on h
# If patternProb > 1: select one of the N stored patterns (1 ... N)
# This is when we will randomly generate a matrix, with specified x-proportions
unitArray = initializeGeneratedMatrix (arraySizeList,h, x1TargetVal)
if not debugPrintOff:
print ' '
print ' The maximum difference in x from the target values is: ', maxXDif
# if not beforeAndAfterAdjustedMatrixPrintOff:
# print
# print 'The distribution among states A and B (x1 and x2) units is:'
# print " ( A ) x1_total =", x1_total, "( B ) x2_total =", x2_total
# print ' For a total of ', x1_total + x2_total, ' units.'
# print ' '
# Print the unit_array so that it shows as a zigzag chain (or in the 2-D case, layers of zigzag chains).
print
print 'The L x M array of units, where M (across) =', localArrayLength, 'and L (layers) =', localArrayLayers
print
# Determining "pairs" - the total number of pairs of zigzag chains - is done in __main__; "pairs" is a global variable
if not debugPrintOff:
print ' '
for i in range (0, pairs):
actualEvenRowNum = 2*i
print 'Row', actualEvenRowNum, ':', blnkspc,
for j in range(0,localArrayLength):
print unitArray[actualEvenRowNum,j], blnkspc,
print
actualOddRowNum = 2*i+1
print 'Row ', actualOddRowNum, ':', blnkspc,
print (blnkspc),
for j in range(0,localArrayLength):
print unitArray[actualOddRowNum,j], blnkspc,
print
print ' '
# print ' '
# print ' This is an effort to build a -, X representation of the unit array'
# easyGridArray = createEasyToReadGrid (arraySizeList, unitArray)
# print easyGridArray
# print ' '
return unitArray
####################################################################################################
####################################################################################################
#
# Procedure to compute configuration variables x'i and return as elements of list configXVarsList
# (Yes, the x'i were computed during array creation and randomization. They are being recomputed
# as part of computing a list of ALL the configuration variables.)
#
####################################################################################################
####################################################################################################
def computeConfigXVariables (arraySizeList, unitArray):
####################################################################################################
# This section unpacks the input variable arraySizeList
####################################################################################################
arrayLength = arraySizeList [0]
arrayLayers = arraySizeList [1]
unit_array = unitArray
# Debug print statements
if not debugPrintOff:
print ' '
print "Just entered computeConfigXVariables"
# Initialize the y'i variables
x1_total = x2_total = 0
for i in range (0,arrayLayers):
x1_partial = x2_partial = 0
# Compute the x'i values for each sub-row of the zigzag, just to see
# the distribution
# Start counting through the array elements, L->R.
for j in range(0, arrayLength):
# If the initial unit is A:
if unit_array[i,j]>0.1:
# The unit is "A," add it to x1
x1_partial = x1_partial + 1
else: # The initial unit is B:
x2_partial = x2_partial + 1
# debug prints
# print "In row", i
# print "x1_partial is", x1_partial, "x2_partial is", x2_partial
x1_total = x1_total + x1_partial
x2_total = x2_total + x2_partial
# print "x1_total (so far) is", x1_total, "x2_total (so far) is", x2_total
x1 = x1_total
x2 = x2_total
configVarsXList = (x1, x2)
# Print the locally-computed values for x1 and x2; these are not passed back to Main.
if not detailedAdjustMatrixPrintOff:
print
print 'The distribution among states A and B (x1 and x2) units is:'
print " ( A ) x1_total =", x1_total, "( B ) x2_total =", x2_total
print ' For a total of ', x1_total + x2_total, ' units.'
print ' '
# print "Leaving computeConfigXVariables for calling procedure"
# print
return (configVarsXList)
####################################################################################################
####################################################################################################
#
# Procedure to compute the set of configuration variables y'i working across a single zigzag chain
# Procedure returns a list configvar containing the three y configuration variables:
# y1 & y2 & y3
#
####################################################################################################
####################################################################################################
def computeConfigYEvenRowZigzagVariables (arraySizeList, unitArray, topRow):
####################################################################################################
# This section unpacks the input variable arraySizeList
####################################################################################################
arrayLength = arraySizeList [0]
arrayLayers = arraySizeList [1]
unit_array = unitArray
###################################################################################################
#
# Compute the nearest-neighbor values y(i)
#
###################################################################################################
# y_1 is A-A
# y_3 is B-B
# left_y_2 is A-B
# right_y_2 is B-A
#
# The total number of y'i's is the same as the total number of x'i's.
# Initialize the y'i variables
y1_total = left_y2_total = right_y2_total = y3_total = 0
###################################################################################################
#
# Compute the nearest-neighbor values y(i) for the case of
# downward-right-pointing diagonals, from top to next layer
# going L->R across the zigzag array
#
###################################################################################################
# Start counting through the layers; since we will work with a pair of
# overlapping layers (for diagonal nearest-neighbors), we use a count of
# layers - 1.
# commenting out for debug
#for i in range(0,arrayLayers-1):
# top_row = i
# next_row = i+1
top_row = topRow
next_row = topRow + 1
# Start counting through the array elements, L->R.
for j in range(0, arrayLength):
# If the initial unit is A:
if unit_array[top_row,j]>0.1:
# Compare with the same (jth) unit in the overlapping row
# comprising the zigzag chain
# If the nearest-neighbor unit is also A:
if unit_array[next_row,j] > 0.1:
# Increment the y_1; the count of A-A nearest-neighbor pairs:
y1_total = y1_total + 1
else: # The nearest-neighbor unit is B:
left_y2_total = left_y2_total + 1
else: # The initial unit is B:
if unit_array[next_row,j] > 0.1: # If the nearest-neighbor unit is A:
right_y2_total = right_y2_total + 1
else: # The nearest-neighbor unit is also B:
y3_total = y3_total + 1
# Debug section: Print totals for right-downwards-pointing diagonals
# print "Subtotals so far (downward-right-pointing-diagonals):"
# print "(A-A) y1_total =", y1_total, "(A-B) left_y2_total =", left_y2_total
# print "(B-B) y3_total =", y3_total, "(B-A) right_y2_total =", right_y2_total
###########################################################
#
# Compute the nearest-neighbor values y(i) for the case of
# upward-right-pointing diagonals, from next-to-top layer up to
# the top layer, going L->R across the zigzag array
#
###########################################################
# Recall that we are carrying forward previously-computed partial totals
# for the y'i values.
# Start counting through the layers again, however, the computations will start
# with the lower layer and look in an upward-right-diagonal to the layer above.
# commenting out for debug
#for i in range(0,arrayLayers-1):
# top_row = i
# next_row = i+1
# Start counting through the array elements, L->R.
# Since we are comparing the unit in the lower row to the one shifted diagonally
# above and over to the right, we only step through to the arraylength - 1 unit.
# A final step (after this) will be to compute the wrap-around.
for j in range(0, arrayLength-1):
# If the initial unit is A:
if unit_array[next_row,j]>0.1:
# Compare with the NEXT (j+1) unit in the overlapping top row
# comprising the zigzag chain
# If the nearest-neighbor unit is also A:
if unit_array[top_row,j+1] > 0.1:
# Increment the y_1; the count of A-A nearest-neighbor pairs:
y1_total = y1_total + 1
else: # The nearest-neighbor unit is B:
left_y2_total = left_y2_total + 1
else: # The initial unit is B:
if unit_array[top_row,j+1] > 0.1: # If the nearest-neighbor unit is A:
right_y2_total = right_y2_total + 1
else: # The nearest-neighbor unit is also B:
y3_total = y3_total + 1
# Debug section: Print totals for right-upwards-pointing diagonals
# print "Subtotals so far (downward + upward-right-pointing-diagonals):"
# print "(A-A) y1_total =", y1_total, "(A-B) left_y2_total =", left_y2_total
# print "(B-B) y3_total =", y3_total, "(B-A) right_y2_total =", right_y2_total
# Only one step remains.
# We need to compute the wrap-around for the zigzag chain (to get the total number
# of y'i's to be the same as the total number of x'i's.
# We compute the nearest-neighbor pair similarity between the last unit on the
# lower row with the first unit on the upper row.
if unit_array[next_row,arrayLength-1]>0.1:
# Compare with the FIRST unit in the overlapping top row
# comprising the zigzag chain
# If the nearest-neighbor unit is also A:
if unit_array[top_row,0] > 0.1:
# Increment the y_1; the count of A-A nearest-neighbor pairs:
y1_total = y1_total + 1
else: # The nearest-neighbor unit is B:
left_y2_total = left_y2_total + 1
else: # The initial unit is B:
if unit_array[top_row,0] > 0.1: # If the nearest-neighbor unit is A:
right_y2_total = right_y2_total + 1
else: # The nearest-neighbor unit is also B:
y3_total = y3_total + 1
#Debug section: Print message,"Computing last of the y'i values - wraparound"
# print "Computing last of the y'i values - wraparound"
# This concludes computation of the y'i totals
################################################################
if not debugPrintOff:
print
print "Totals for the y'i variables:"
print "(A-A) y1_total =", y1_total, "(A-B) left_y2_total =", left_y2_total
print "(B-B) y3_total =", y3_total, "(B-A) right_y2_total =", right_y2_total
print
################################################################
###################################################################################################
#
# Assign the computed configuration variables to elements of the configVarsList,
# which will be passed back to the calling procedure
#
###################################################################################################
y1 = y1_total
y2 = left_y2_total + right_y2_total
y3 = y3_total
configVarsYList = (y1, y2, y3)
return (configVarsYList)
# END (function to compute the Y configuration variables for an EVEN row; this
# includes the wrap-around)
####################################################################################################
####################################################################################################
#
# Procedure to compute the set of configuration variables y'i
# Procedure returns a list configvar containing the three y configuration variables:
# y1 & y2 & y3
#
####################################################################################################
####################################################################################################
def computeConfigYOddRowZigzagVariables (arraySizeList, unitArray, topRow):
####################################################################################################
# This section unpacks the input variable arraySizeList
####################################################################################################
arrayLength = arraySizeList [0]
arrayLayers = arraySizeList [1]
unit_array = unitArray
# Initialize the y'i variables
y1_total = left_y2_total = right_y2_total = y3_total = 0
###################################################################################################
#
# Compute the nearest-neighbor values y(i) for the case of
# downward-right-pointing diagonals, from top to next layer
# going L->R across the zigzag array
#
###################################################################################################
# Start counting through the layers; since we will work with a pair of
# overlapping layers (for diagonal nearest-neighbors), we use a count of
# layers - 1.
# commenting out for debug
#for i in range(0,arrayLayers-1):
# top_row = i
# next_row = i+1
top_row = topRow
next_row = topRow + 1
if top_row == arrayLayers-1: next_row = 0
# Start counting through the array elements, L->R.
for j in range(0, arrayLength-1): # Same logic as in the Even Row y(i) computation
# but we go for one (TWO???) less down the array length
# If the initial unit is A:
if unit_array[top_row,j]>0.1:
# Compare with the same (jth) unit in the overlapping row
# comprising the zigzag chain
# If the nearest-neighbor unit is also A:
if unit_array[next_row,j+1] > 0.1:
# Increment the y_1; the count of A-A nearest-neighbor pairs:
y1_total = y1_total + 1
else: # The nearest-neighbor unit is B:
left_y2_total = left_y2_total + 1
else: # The initial unit is B:
if unit_array[next_row,j+1] > 0.1: # If the nearest-neighbor unit is A:
right_y2_total = right_y2_total + 1
else: # The nearest-neighbor unit is also B:
y3_total = y3_total + 1
# Debug section: Print totals for right-downwards-pointing diagonals
# print "Subtotals so far (downward-right-pointing-diagonals):"
# print "(A-A) y1_total =", y1_total, "(A-B) left_y2_total =", left_y2_total
# print "(B-B) y3_total =", y3_total, "(B-A) right_y2_total =", right_y2_total
###########################################################
#
# Compute the nearest-neighbor values y(i) for the case of
# upward-right-pointing diagonals, from next-to-top layer up to
# the top layer, going L->R across the zigzag array
#
###########################################################
# Recall that we are carrying forward previously-computed partial totals
# for the y'i values.
# Start counting through the layers again, however, the computations will start
# with the lower layer and look in an upward-right-diagonal to the layer above.
# commenting out for debug
#for i in range(0,arrayLayers-1):
# top_row = i
# next_row = i+1
# Start counting through the array elements, L->R.
# Since we are comparing the unit in the lower row to the one shifted diagonally
# above and over to the right, we only step through to the arraylength - 1 unit.
# A final step (after this) will be to compute the wrap-around.
for j in range(0, arrayLength): # Same logic as in the Even Row y(i) computation
# But we can include the full array length (the other was truncated at arrayLength - 1)
# If the initial unit is A:
if unit_array[next_row,j]>0.1:
# Compare with the NEXT (j+1) unit in the overlapping top row
# comprising the zigzag chain
# If the nearest-neighbor unit is also A:
if unit_array[top_row,j] > 0.1:
# Increment the y_1; the count of A-A nearest-neighbor pairs:
y1_total = y1_total + 1
else: # The nearest-neighbor unit is B:
left_y2_total = left_y2_total + 1
else: # The initial unit is B:
if unit_array[top_row,j] > 0.1: # If the nearest-neighbor unit is A:
right_y2_total = right_y2_total + 1
else: # The nearest-neighbor unit is also B:
y3_total = y3_total + 1
# Debug section: Print totals for right-upwards-pointing diagonals
# print "Subtotals so far (downward + upward-right-pointing-diagonals):"
# print "(A-A) y1_total =", y1_total, "(A-B) left_y2_total =", left_y2_total
# print "(B-B) y3_total =", y3_total, "(B-A) right_y2_total =", right_y2_total
# Only one step remains.
# We need to compute the wrap-around for the zigzag chain (to get the total number
# of y'i's to be the same as the total number of x'i's.
# We compute the nearest-neighbor pair similarity between the last unit on the
# lower row with the first unit on the upper row.
if unit_array[top_row,arrayLength-1]>0.1:
# Compare with the FIRST unit in the overlapping top row
# comprising the zigzag chain
# If the nearest-neighbor unit is also A:
if unit_array[next_row,0] > 0.1:
# Increment the y_1; the count of A-A nearest-neighbor pairs:
y1_total = y1_total + 1
else: # The nearest-neighbor unit is B:
left_y2_total = left_y2_total + 1
else: # The initial unit is B:
if unit_array[next_row,0] > 0.1: # If the nearest-neighbor unit is A:
right_y2_total = right_y2_total + 1
else: # The nearest-neighbor unit is also B:
y3_total = y3_total + 1
#Debug section: Print message,"Computing last of the y'i values - wraparound"
# print "Computing last of the y'i values - wraparound"
# This concludes computation of the y'i totals
################################################################
if not debugPrintOff:
print
print "Totals for the y'i variables:"
print "(A-A) y1_total =", y1_total, "(A-B) left_y2_total =", left_y2_total
print "(B-B) y3_total =", y3_total, "(B-A) right_y2_total =", right_y2_total
print
################################################################
###################################################################################################
#
# Assign the computed configuration variables to elements of the configVarsList,
# which will be passed back to the calling procedure
#
###################################################################################################
y1 = y1_total
y2 = left_y2_total + right_y2_total
y3 = y3_total
configVarsYList = (y1, y2, y3)
return (configVarsYList)
# END (function to compute the Y configuration variables for an ODD row; this
# includes the wrap-around)
####################################################################################################
####################################################################################################
# This function runs both the even-to-odd and odd-to-even y(i) nearest neighbors; it combines the two in building
# another row on top of the basic 1-D zigzag chain
####################################################################################################
def computeConfigYVariables (arraySizeList, unitArray):
# Initialize the y'i variables
y1 = y2 = y3 = 0
if not debugPrintOff:
print ' '
print ' Starting to compute Y variables'
print ' Total number of pairs of zigzag chains is: ', pairs
print ' '
for i in range (0, pairs):
topRow = 2*i
if not debugPrintOff:
print ' Row: ', topRow
# Obtain the y(i) values from the first even-to-odd zigzag chain (0 to 1, running top-to-bottom)
configVarsYList = computeConfigYEvenRowZigzagVariables (arraySizeList, unitArray, topRow)
# Assign the returned results to the local sum for each of the z(i) triplets
y1 = y1+configVarsYList[0]
y2 = y2+configVarsYList[1]
y3 = y3+configVarsYList[2]
topRow = 2*i+1
if not debugPrintOff:
print ' '
print ' Row: ', topRow
configVarsYList = computeConfigYOddRowZigzagVariables (arraySizeList, unitArray, topRow)
# Assign the returned results to the local sum for each of the z(i) triplets
y1 = y1+configVarsYList[0]
y2 = y2+configVarsYList[1]
y3 = y3+configVarsYList[2]
# Debug section: Print totals for right-downwards-then-upwards triplets
if not debugPrintOff:
print ' '
print ' -----------'
print ' '
print "Totals for all y(i), after completing Row: ", topRow
print " (A-A) z1_total =", y1
print "(A-B) plus (B-A) z2_total =", y2
print " (B-B) z3_total =", y3
print ' '
print ' -----------'
print ' '
# Start working on the next zigzag chain
# topRow = 2*i+1
# Obtain the z(i) values from the first odd-to-even-to zigzag chain (1 to 2, running top-to-bottom)
# configVarsZListOddToEven = computeConfigZVariablesOddToEven (arraySizeList, unitArray, topRow)
# Add the returned results to the local sum for each of the z(i) triplets
# z1 = z1+configVarsZListOddToEven[0]
# z2 = z2+configVarsZListOddToEven[1]
# z3 = z3+configVarsZListOddToEven[2]
# z4 = z4+configVarsZListOddToEven[3]
# z5 = z5+configVarsZListOddToEven[4]
# z6 = z6+configVarsZListOddToEven[5]
# Debug section: Print totals for right-upwards-then-downwards triplets
# print ' '
# print ' -----------'
# print ' '
# print "Totals for all triplets, after completing Row: ", topRow
# print " (A-A-A) z1_total =", z1
# print "(A-A-B) plus (B-A-A) z2_total =", z2
# print " (A-B-A) z3_total =", z3
# print " (B-A-B) z4_total =", z4
# print "(B-B-A) plus (A-B-B) z5_total =", z5
# print " (B-B-B) z6_total =", z6
# print ' '
# print ' -----------'
# print ' '
# NOTE: Still need to write the computation for an extra odd row in grid, if it exists
configVarsYList = (y1, y2, y3)
return (configVarsYList)
# END (high-level function calling the EVEN and ODD functions to compute the Y
# configuration variables)
####################################################################################################
####################################################################################################
#
# Procedure to compute horizontal configuration variables w'i and return as elements of list configWVarsList
#
####################################################################################################
####################################################################################################
def computeConfigWHorizontalRowVariables (arraySizeList, unitArray):
####################################################################################################
# This section unpacks the input variable arraySizeList
####################################################################################################
arrayLength = arraySizeList [0]
arrayLayers = arraySizeList [1]
unit_array = unitArray
# Debug print statements
if not debugPrintOff:
print "Just entered computeConfigWVariables"
# Initialize the w'i variables
w1_total = w2_total = w3_total = 0
w1_partial = w2_partial = w3_partial = 0
for i in range (0,arrayLayers):
w1_partial = w2_partial = w3_partial = 0
# Compute the w'i values for each sub-row of the zigzag, just to see
# the distribution
# Start counting through the array elements, L->R.
for j in range(0, arrayLength):
nextNearestNeighbor = j+1
rowLimit = arrayLength-1
if j == rowLimit: nextNearestNeighbor = 0
# If the initial unit is A:
if unit_array[i,j]>0.1:
# The unit is "A," see if the next unit is "A" or "B"
if unit_array[i,nextNearestNeighbor]>0.1:
# Compare with the NEXT (j+1) unit in the SAME row
# comprising a partial row of the the zigzag chain
# If this next-nearest-neighbor unit is also "A":
w1_partial = w1_partial + 1
else: # The next-nearest-neighbor is in "B"
w2_partial = w2_partial + 1
else: # The initial unit is B:
# The unit is "B," see if the next unit is "A" or "B"
if unit_array[i,nextNearestNeighbor]>0.1:
# Compare with the NEXT (j+1) unit in the SAME row
# comprising a partial row of the the zigzag chain
# If this next-nearest-neighbor unit is also "A":
w2_partial = w2_partial + 1
else: # The next-nearest-neighbor is in "B"
w3_partial = w3_partial + 1
detailedDebugPrintOffW = True
if not detailedDebugPrintOffW:
print ' '
print "In row ", i
print "w1_partial = ", w1_partial
print "w2_partial = ", w2_partial
print "w3_partial = ", w3_partial
# Check the wrap-around value between the last unit in the row
# and the first item of this same row
# if unit_array[i,arrayLength-1]>0.1:
# # The unit is "A," see if the wraparound unit is "A" or "B"
# if unit_array[i,0] > 0.1: #This unit is "A"
# w1_partial = w1_partial + 1
# else: w2_partial = w2_partial + 1
# else:
# if unit_array[i,0] > 0.1: #This unit is "A"
# w2_partial = w2_partial + 1
# else: w3_partial = w3_partial + 1
# print "In row", i, "after wrap-around - still testing for A"
# print "w1_partial = ", w1_partial
# print "w2_partial = ", w2_partial
w1_total = w1_total + w1_partial
w2_total = w2_total + w2_partial
w3_total = w3_total + w3_partial
w1 = w1_total
w2 = w2_total
w3 = w3_total
configVarsWList = (w1, w2, w3)
################################################################
if not debugPrintOff:
print ' '
print "Totals for all horizontal w(i)"
print " (A--A) w1_total =", w1
print "(A--B) plus (B--A) w2_total =", w2
print " (B--B) w3_total =", w3
print ' '
################################################################
return (configVarsWList)
####################################################################################################
####################################################################################################
#
# Procedure to compute vertical configuration variables w'i and return as elements of list configWVarsList
#
####################################################################################################
####################################################################################################
def computeConfigWVerticalColVariables (arraySizeList, unitArray):
####################################################################################################
# This section unpacks the input variable arraySizeList
####################################################################################################
arrayLength = arraySizeList [0]
arrayLayers = arraySizeList [1]
iLimit = arrayLayers - 2
# Debug print statements
if not debugPrintOff:
print "Just entered computeConfigWVerticalVariables"
# Initialize the w'i variables
w1_total = w2_total = w3_total = 0
w1_partial = w2_partial = w3_partial = 0
#
for i in range (0, arrayLayers): # run through the rows, look at those two rows apart
vertPair = i+2
if i == iLimit: vertPair = 0
if i == iLimit+1: vertPair = 1
for j in range(0, arrayLength):
# If the initial unit is A:
if unitArray[i,j]>0.1:
# The unit is "A," see if the next unit is "A" or "B"
if unitArray[vertPair,j]>0.1:
# Compare with the NEXT (j+1) unit in the SAME row
# comprising a partial row of the the zigzag chain
# If this next-nearest-neighbor unit is also "A":
w1_partial = w1_partial + 1
else: # The next-nearest-neighbor is in "B"
w2_partial = w2_partial + 1
else: # The initial unit is B:
# The unit is "B," see if the next unit is "A" or "B"
if unitArray[vertPair,j]>0.1:
# Compare with the NEXT (j+1) unit in the SAME row
# comprising a partial row of the the zigzag chain
# If this next-nearest-neighbor unit is also "A":
w2_partial = w2_partial + 1
else: # The next-nearest-neighbor is in "B"
w3_partial = w3_partial + 1
#
w1_total = w1_total + w1_partial
w2_total = w2_total + w2_partial
w3_total = w3_total + w3_partial
w1 = w1_total
w2 = w2_total
w3 = w3_total
configVarsWList = (w1, w2, w3)
################################################################
if not debugPrintOff:
print ' '
print "Totals for all vertical w(i)"
print " (A--A) w1_total =", w1
print "(A--B) plus (B--A) w2_total =", w2
print " (B--B) w3_total =", w3
print ' '
################################################################
return (configVarsWList)
####################################################################################################
####################################################################################################