Beispiel #1
0
 def next_activity(self):
     """Return activity matrix for next time step"""
     A = np.zeros((self.nyg_ex, self.nxg_ex), int)
     rec = self.next_record()
     for k in range(len(rec)):
         kx = conv.kxPos(rec[k], self.nxg_ex, self.nyg_ex, self.nf)
         ky = conv.kyPos(rec[k], self.nxg_ex, self.nyg_ex, self.nf)
         A[ky, kx] = 1
     return A
Beispiel #2
0
 def next_activity(self):
    """Return activity matrix for next time step"""
    A = np.zeros((self.nyg_ex, self.nxg_ex), int)
    rec = self.next_record()
    for k in range(len(rec)):
       kx = conv.kxPos(rec[k], self.nxg_ex, self.nyg_ex, self.nf)
       ky = conv.kyPos(rec[k], self.nxg_ex, self.nyg_ex, self.nf)
       A[ky,kx] = 1
    return A
Beispiel #3
0
        row = int(y + 0.5)
        x2 = (x / 16.0)
        y2 = (y / 16.0)
        x = (x / 4.0)
        y = (y / 4.0)
        if col >= 0 and col < numcols and row >= 0 and row < numrows:
            z = P[row, col]
            return 'x=%1.4f, y=%1.4f, z=%1.4f' % (x, y, z)
        else:
            return 'x=%1.4d, y=%1.4d, x2=%1.4d, y2=%1.4d' % (int(x), int(y),
                                                             int(x2), int(y2))

    k = 16
    for ko in range(numpat):
        kxOn = conv.kxPos(ko, nx, ny, nf)
        kyOn = conv.kyPos(ko, nx, ny, nf)
        p = w.next_patch()
        poff = wOff.next_patch()
        if marginstart < kxOn < marginend:
            if marginstart < kyOn < marginend:
                acount = acount + 1
                if kxOn == margin + 1 and kyOn == margin + 1:
                    don = p
                    doff = poff
                    d = np.append(don, doff)
                else:
                    don = p
                    doff = poff
                    e = np.append(don, doff)
                    d = np.vstack((d, e))
Beispiel #4
0
nf = w.nf
margin = 10
marginstart = margin
marginend = nx - margin

nx_im = nx * (nxp + space) + space
ny_im = ny * (nyp + space) + space

im = np.zeros((nx_im, ny_im))
im[:, :] = (w.max - w.min) / 2.

h = np.zeros(9, dtype=int)
g = np.zeros(9, dtype=int)
for k in range(numpat):
    kxOn = conv.kxPos(k, nx, ny, nf)
    kyOn = conv.kyPos(k, nx, ny, nf)
    p = w.next_patch()
    if marginstart < kxOn < marginend:
        if marginstart < kyOn < marginend:
            d = whatFeature(p)
            e = d[0]
            f = d[1]
            e += 1
            f += 1
            h[e] += 1
            g[f] += 1

fig = plt.figure()
ax = fig.add_subplot(211, axisbg='darkslategray')

ax.plot(np.arange(len(h)), h, 'o', color='y')
nf = w.nf
margin = 10
marginstart = margin
marginend = nx - margin

nx_im = nx * (nxp + space) + space
ny_im = ny * (nyp + space) + space

im = np.zeros((nx_im, ny_im))
im[:,:] = (w.max - w.min) / 2.

h = np.zeros(9, dtype=int)
g = np.zeros(9, dtype=int)
for k in range(numpat):
   kxOn = conv.kxPos(k, nx, ny, nf)
   kyOn = conv.kyPos(k, nx, ny, nf)
   p = w.next_patch()
   if marginstart < kxOn < marginend:
      if marginstart < kyOn < marginend:
         d = whatFeature(p)
         e = d[0]
         f = d[1]
         e += 1
         f += 1
         h[e] += 1
         g[f] += 1 

fig = plt.figure()
ax = fig.add_subplot(211, axisbg='darkslategray')

ax.plot(np.arange(len(h)), h, 'o', color='y')
Beispiel #6
0
rheady = ry[2]
rtaily = ry[3]

nx_im = wnx * (wnxp + space) + space
ny_im = wny * (wnyp + space) + space
small = w.max / 2

im = np.zeros((nx_im, ny_im))
im[:, :] = (w.max - w.min) / 2.

count = 0
wheremp = []

for k in range(w.numPatches):
    kx = conv.kxPos(k, wnx, wny, wnf)
    ky = conv.kyPos(k, wnx, wny, wnf)
    P = w.next_patch()
    if len(P) != wnxp * wnyp:
        continue

    if kx == kxPre:
        if ky == kyPre:
            mp = P
            for i in range(wnxp * wnxp):
                if mp[i] <= small:
                    mp[i] = 0.0
                else:
                    wheremp = np.append(wheremp, count)
                count += 1
            mp = np.reshape(mp, (wnxp, wnyp))
Beispiel #7
0
nx_im = wnx * (wnxp + space) + space
ny_im = wny * (wnyp + space) + space
small = w.max / 2


im = np.zeros((nx_im, ny_im))
im[:,:] = (w.max - w.min) / 2.


count = 0
wheremp = []

for k in range(w.numPatches):
   kx = conv.kxPos(k, wnx, wny, wnf)
   ky = conv.kyPos(k, wnx, wny, wnf)
   P = w.next_patch()
   if len(P) != wnxp * wnyp:
      continue

   if kx == kxPre:
      if ky == kyPre:
         mp = P
         for i in range(wnxp*wnxp):
            if mp[i] <= small:
               mp[i] = 0.0
            else:
               wheremp = np.append(wheremp, count) 
            count += 1
         mp = np.reshape(mp, (wnxp, wnyp))
Beispiel #8
0
def k_stability_analysis(k, forwardjump):
    w = rw.PVReadWeights(sys.argv[1])
    feature = k - 1
    count = 0
    d = np.zeros((nxp, nyp))

    w.rewind()
    for ko in np.arange(numpat):
        kxOn = conv.kxPos(ko, nx, ny, nf)
        kyOn = conv.kyPos(ko, nx, ny, nf)
        p = w.next_patch()
        if marginstart < kxOn < marginend:
            if marginstart < kyOn < marginend:
                if cluster[count] == feature:
                    e = p
                    e = e.reshape(nxp, nyp)
                    numrows, numcols = e.shape
                    count = count + 1
                    patpos = w.file.tell()
                    patchposition.append(patpos)
                else:
                    e = d
                    count = count + 1
            else:
                e = d
        else:
            e = d
        x = (nxp) * (ko % nx)
        y = (nyp) * (ko / nx)

        im2[y:y + nyp, x:x + nxp] = e

    ##########
    # Find Valuse of K-cluster[x] Patches
    ##########

    w = rw.PVReadWeights(sys.argv[2])
    w.rewind()
    patpla = patchposition
    lenpat = len(patpla)

    number = w.numPatches
    count = 0

    exp = []
    exppn = []

    body = w.recSize + 4
    hs = w.headerSize
    filesize = os.path.getsize(sys.argv[2])
    bint = filesize / body

    bint = bint - forwardjump - 1

    if forwardjump == 0:
        4
    else:
        leap = ((body * forwardjump) + (100 * forwardjump))
        w.file.seek(leap, os.SEEK_CUR)

    for i in range(bint):
        if i == 0:
            for j in range(lenpat):
                if j == 0:
                    go = patpla[0] - hs - patchblen
                    w.file.seek(go, os.SEEK_CUR)
                    p = w.next_patch()
                    if len(p) == 0:
                        print "STOPPEP SUPER  EARLY"
                        sys.exit()
                    d = p
                    allpat = 0
                    p = w.normalize(d)
                    pn = p
                    pn = np.reshape(np.matrix(pn), (1, flatnxp))
                    p = np.reshape(np.matrix(p), (flatnxp, 1))
                    pm = pn * p
                    exppn = np.append(exppn, pn)
                    exp = np.append(exp, pm)

                else:
                    pospost = patpla[j - 1]
                    poscur = patpla[j]
                    jump = poscur - pospost - patchblen
                    w.file.seek(jump, os.SEEK_CUR)
                    p = w.next_patch()
                    if len(p) == 0:
                        print "STOPPED EARLY"
                        sys.exit()
                    d = p
                    p = w.normalize(d)
                    pn = p
                    pn = np.reshape(np.matrix(pn), (1, flatnxp))
                    p = np.reshape(np.matrix(p), (flatnxp, 1))
                    pm = pn * p
                    exppn = np.append(exppn, pn)
                    exp = np.append(exp, pm)
                    #print "Ch-Ch-Changes", exppn
        else:
            count = 0
            prejump = body - patpla[lenpat - 1] + hs
            w.file.seek(prejump, os.SEEK_CUR)
            for j in range(lenpat):
                if j == 0:
                    go = patpla[0] - 4 - patchblen
                    w.file.seek(go, os.SEEK_CUR)
                    p = w.next_patch()
                    test = p
                    if len(test) == 0:
                        print "stop"
                        input('Press Enter to Continue')
                        sys.exit()
                    d = p
                    p = w.normalize(d)
                    p = np.reshape(np.matrix(p), (flatnxp, 1))
                    j1 = 0
                    j2 = flatnxp
                    pm = np.matrix(exppn[j1:j2]) * p
                    exp = np.append(exp, pm)
                    count += 1
                else:
                    pospost = patpla[j - 1]
                    poscur = patpla[j]
                    jump = poscur - pospost - patchblen
                    w.file.seek(jump, os.SEEK_CUR)
                    p = w.next_patch()
                    test = p
                    if len(test) == 0:
                        print "stop"
                        input('Press Enter to Continue')
                        sys.exit()
                    d = p
                    p = w.normalize(d)
                    p = np.reshape(np.matrix(p), (flatnxp, 1))
                    j1 = flatnxp * j
                    j2 = flatnxp * (j + 1)
                    pm = np.matrix(exppn[j1:j2]) * p
                    exp = np.append(exp, pm)
                    count += 1

    ##########
    # Find Average of K-cluster[x] Weights
    ##########

    thenumber = lenpat
    thenumberf = float(thenumber)

    patpla = exp
    lenpat = len(patpla)

    howlong = lenpat / thenumber

    total = []
    logtotal = []

    for i in range(thenumber):
        subtotal = []
        logsubtotal = []
        for j in range(howlong):
            if i == 0:
                value = patpla[i + (thenumber * j)]
                total = np.append(total, value)
                logvalue = patpla[i + (thenumber * j)]
                logvalue = math.log10(logvalue)
                logtotal = np.append(logtotal, logvalue)
            else:
                value = patpla[i + (thenumber * j)]
                subtotal = np.append(subtotal, value)
                logvalue = patpla[i + (thenumber * j)]
                logvalue = math.log10(logvalue)
                logsubtotal = np.append(logsubtotal, logvalue)

        if i > 0:
            total = total + subtotal
        if i > 0:
            logtotal = logtotal + logsubtotal

    total = total / thenumberf
    logtotal = logtotal / thenumberf

    global total1
    global total2
    global total3
    global total4
    global total5
    global total6
    global total7
    global total8
    global total9
    global total10
    global total11
    global total12
    global total13
    global total14
    global total15
    global total16
    global logtotal1
    global logtotal2
    global logtotal3
    global logtotal4
    global logtotal5
    global logtotal6
    global logtotal7
    global logtotal8
    global logtotal9
    global logtotal10
    global logtotal11
    global logtotal12
    global logtotal13
    global logtotal14
    global logtotal15
    global logtotal16

    if feature == 0:
        total1 = [0.0]
        total2 = [0.0]
        total3 = [0.0]
        total4 = [0.0]
        total5 = [0.0]
        total6 = [0.0]
        total7 = [0.0]
        total8 = [0.0]
        total9 = [0.0]
        total10 = [0.0]
        total11 = [0.0]
        total12 = [0.0]
        total13 = [0.0]
        total14 = [0.0]
        total15 = [0.0]
        total16 = [0.0]
        logtotal1 = [0.0]
        logtotal2 = [0.0]
        logtotal3 = [0.0]
        logtotal4 = [0.0]
        logtotal5 = [0.0]
        logtotal6 = [0.0]
        logtotal7 = [0.0]
        logtotal8 = [0.0]
        logtotal9 = [0.0]
        logtotal10 = [0.0]
        logtotal11 = [0.0]
        logtotal12 = [0.0]
        logtotal13 = [0.0]
        logtotal14 = [0.0]
        logtotal15 = [0.0]
        logtotal16 = [0.0]

    if feature == 0:
        total1 = total
        logtotal1 = logtotal
    if feature == 1:
        total2 = total
        logtotal2 = logtotal
    if feature == 2:
        total3 = total
        logtotal3 = logtotal
    if feature == 3:
        total4 = total
        logtotal4 = logtotal
    if feature == 4:
        total5 = total
        logtotal5 = logtotal
    if feature == 5:
        total6 = total
        logtotal6 = logtotal
    if feature == 6:
        total7 = total
        logtotal7 = logtotal
    if feature == 7:
        total8 = total
        logtotal8 = logtotal
    if feature == 8:
        total9 = total
        logtotal9 = logtotal
    if feature == 9:
        total10 = total
        logtotal10 = logtotal
    if feature == 10:
        total11 = total
        logtotal11 = logtotal
    if feature == 11:
        total12 = total
        logtotal12 = logtotal
    if feature == 12:
        total13 = total
        logtotal13 = logtotal
    if feature == 13:
        total14 = total
        logtotal14 = logtotal
    if feature == 14:
        total15 = total
        logtotal15 = logtotal
    if feature == 15:
        total16 = total
        logtotal16 = logtotal

    return
Beispiel #9
0
        #ax.plot(np.arange(len(A15q)), A15q, color=cm.Paired(0.90) , ls = '-')
        #ax.plot(np.arange(len(A16q)), A16q, color=cm.Paired(0.96) , ls = '-')

        plt.show()

        sys.exit()
        if 1 == 1:
            kd = []
            AW = AW.reshape(lenofb, 1)
            AWO = AWO.reshape(lenofb, 1)
            count = 0

            for k in range(w.numPatches):
                p = w.next_patch()
                pO = wO.next_patch()
                kx = conv.kyPos(k, nx, ny, nf)
                ky = conv.kyPos(k, nx, ny, nf)
                if len(p) != nxp * nyp:
                    continue

                #print "p = ", p

                count += 1
                #print "count = ", count
                if AW[k] == 1:
                    if len(kd) == 0:
                        don = p
                        doff = pO
                        kd = np.append(don, doff)
                    else:
                        don = p
Beispiel #10
0
    for k in range(len(M)):
        for b in range(numbins):
            if (M[k] > bins[b]): dist[b] += 1
    dist = dist / count

    # print maximum projection
    #
    maxp = 0
    maxk = 0
    for k in range(len(M)):
        if M[k] > maxp:
            maxp = M[k]
            maxk = k

    kx = conv.kxPos(k, numcols, numrows, w.nf)
    ky = conv.kyPos(k, numcols, numrows, w.nf)
    print "maximum projected value = ", maxp, maxk, kx, ky

    M = M.reshape((numrows, numcols))

    # print averaged projection over column
    #
    s = np.zeros(numcols)
    maxs = 0.0
    maxcol = 0
    for col in range(numcols):
        s[col] = np.sum(M[:, col])
        if s[col] > maxs:
            maxs = s[col]
            maxcol = col
    s = s / numrows
Beispiel #11
0
def k_stability_analysis(k, forwardjump):
   w = rw.PVReadWeights(sys.argv[1])
   feature = k - 1
   count = 0
   d = np.zeros((nxp,nyp))

   w.rewind()
   for ko in np.arange(numpat):
      kxOn = conv.kxPos(ko, nx, ny, nf)
      kyOn = conv.kyPos(ko, nx, ny, nf)
      p = w.next_patch()
      if marginstart < kxOn < marginend:
         if marginstart < kyOn < marginend:





   ##########
   # Find Valuse of K-cluster[x] Patches
   ##########


   w = rw.PVReadWeights(sys.argv[3])
   wOff = rw.PVReadWeights(sys.argv[4])
   w.rewind()
   wOff.rewind()
   patpla = patchposition
   lenpat = len(patpla)


   number = w.numPatches 
   count = 0

   exp = []
   expOff = []
   exppn = []
   exppnOff = []

   body = w.recSize + 4
   hs = w.headerSize
   filesize = os.path.getsize(sys.argv[3])
   bint = filesize / body


   bint = bint - forwardjump - 1

   if forwardjump == 0:
      print "43110"
   else:
      leap = (body * forwardjump)
      w.file.seek(leap, os.SEEK_CUR)






   for i in range(bint):
      if i == 0:
         for j in range(lenpat):
            if j == 0:
               p = w.next_patch()
               pOff = wOff.next_patch()
               if len(p) == 0:
                  print"STOPPEP SUPER  EARLY"
                  sys.exit()
               don = p
               doff = pOff
               d = np.append(don, doff)
               p = w.normalize(d)
               pn = p
               pn = np.reshape(np.matrix(pn),(1,32))
               p = np.reshape(np.matrix(p),(32,1))
               pm = pn * p
               exppn = np.append(exppn, pn)
               exp = np.append(exp,pm)
            else:
               p = w.next_patch()
               pOff = wOff.next_patch()
               if len(pOff) == 0:
                  print"STOPPED EARLY"
                  sys.exit()
               don = p
               doff = pOff
               d = np.append(don, doff)
               p = w.normalize(d)
               pn = p
               pn = np.reshape(np.matrix(pn),(1,32))
               p = np.reshape(np.matrix(p),(32,1))
               pm = pn * p
               exppn = np.append(exppn, pn)
               exp = np.append(exp,pm)
      else:
         count = 0
         prejump = body - patpla[lenpat-1] + hs
         w.file.seek(prejump, os.SEEK_CUR)
         wOff.file.seek(prejump, os.SEEK_CUR)
         for j in range(lenpat):
            if j == 0:
               p = w.next_patch()
               pOff = wOff.next_patch()
               test = p
               if len(test) == 0:
                  print "stop"
                  input('Press Enter to Continue')
                  sys.exit()
               don = p
               doff = pOff
               d = np.append(don, doff)
               p = w.normalize(d)
               p = np.reshape(np.matrix(p),(32,1))
               j1 = 0
               j2 = 32
               pm = np.matrix(exppn[j1:j2]) * p
               exp = np.append(exp,pm)
               count += 1
            else:
               p = w.next_patch()
               pOff = wOff.next_patch()
               test = pOff
               if len(test) == 0:
                  print "stop"
                  input('Press Enter to Continue')
                  sys.exit()
               don = p
               doff = pOff
               d = np.append(don, doff)
               p = w.normalize(d)
               p = np.reshape(np.matrix(p),(32,1))
               j1 = 32 * j
               j2 = 32 * (j +1)
               pm = np.matrix(exppn[j1:j2]) * p
               exp = np.append(exp,pm)
               count += 1



   ##########
   # Find Average of K-cluster[x] Weights
   ##########


   thenumber = lenpat
   thenumberf = float(thenumber)

   patpla = exp
   lenpat = len(patpla)


   howlong = lenpat / thenumber

   total = []
   logtotal = []

   for i in range(thenumber):
      subtotal = []
      logsubtotal = []
      for j in range(howlong):
         if i == 0:
            value = patpla[i + (thenumber * j)]
            total = np.append(total, value)
            #logvalue = patpla[i + (thenumber * j)]
            #logvalue = math.log10(logvalue)
            #logtotal = np.append(logtotal, logvalue)
         else:
            value = patpla[i + (thenumber * j)]
            subtotal = np.append(subtotal, value) 
            #logvalue = patpla[i + (thenumber * j)]
            #logvalue = math.log10(logvalue)
            #logsubtotal = np.append(logsubtotal, logvalue)
        
      if i > 0:
         total = total + subtotal
      #if i > 0:
         #logtotal = logtotal + logsubtotal


   total = total / thenumberf
   #logtotal = logtotal / thenumberf


   global total1
   global total2
   global total3
   global total4
   global total5
   global total6
   global total7
   global total8
   global total9
   global total10
   global total11
   global total12
   global total13
   global total14
   global total15
   global total16




   #global logtotal1
   #global logtotal2
   #global logtotal3
   #global logtotal4
   #global logtotal5
   #global logtotal6
   #global logtotal7
   #global logtotal8
   #global logtotal9
   #global logtotal10
   #global logtotal11
   #global logtotal12
   #global logtotal13
   #global logtotal14
   #global logtotal15
   #global logtotal16

   if feature == 0:
      total1 = total 
   if feature == 1:
      total2 = total
   if feature == 2:
      total3 = total
   if feature == 3:
      total4 = total
   if feature == 4:
      total5 = total
   if feature == 5:
      total6 = total
   if feature == 6:
      total7 = total
   if feature == 7:
      total8 = total
   if feature == 8:
      total9 = total
   if feature == 9:
      total10 = total
   if feature == 10:
      total11 = total
   if feature == 11:
      total12 = total
   if feature == 12:
      total13 = total
   if feature == 13:
      total14 = total
   if feature == 14:
      total15 = total
   if feature == 15:
      total16 = total

   return
      count = 0
      coord = 1


      nx_im2 = nx * (nxp + space) + space
      ny_im2 = ny * (nyp + space) + space

      im2 = np.zeros((nx_im2, ny_im2))
      im[:,:] = (w2.max - w2.min) / 2.



      for i in range(numpat):
         kx = conv.kxPos(i, nx, ny, nf)
         ky = conv.kyPos(i, nx, ny, nf)
         p = w2.next_patch()
         if kx == whichpatx:
            if ky == whichpaty:
               e = p
               e = e.reshape(nxp, nyp)
               numrows, numcols = e.shape
               count += 1
               im3 = e
            else:
               e = d
               count += 1
         else:
            e = d
            count += 1
def patchover(x, huk, testnumber):

    w = rw.PVReadWeights(sys.argv[x])

    space = 1

    d = np.zeros((4, 4))

    nx = w.nx
    ny = w.ny
    nxp = w.nxp
    nyp = w.nyp
    coor = nxp * nyp
    # print coor

    patlen = nxp * nyp
    numpat = w.numPatches
    nf = w.nf
    check = sys.argv[1]
    a = check.find("w")
    b = check[a].strip("w")
    # print b

    margin = 10

    marginstart = margin
    marginend = nx - margin
    acount = 0
    patchposition = []

    nx = w.nx
    ny = w.ny
    nxp = w.nxp
    nyp = w.nyp

    test = 0

    if testnumber == 0:
        for i in range(1):
            while test == 0:
                testnumber = random.randint(0, numpat)
                for i in range(128):
                    for j in range(128):
                        if marginstart < i < marginend:
                            if marginstart < j < marginend:
                                if testnumber == (j + (i * 128)):
                                    test = 1

    # testnumber = (69 + (62 * 128))
    postpat = []

    for ko in range(numpat):
        kxOn = conv.kxPos(ko, nx, ny, nf)
        kyOn = conv.kyPos(ko, nx, ny, nf)
        prepat = w.file.tell()
        p = w.next_patch()
        postpat = w.file.tell()
        # print postpat
        if marginstart < kxOn < marginend:
            if marginstart < kyOn < marginend:
                if testnumber == (kxOn + (kyOn * 128)):
                    patpos = w.file.tell()
                    # print
                    # print "kx", kxOn
                    # print "ky", kyOn

    donepat = postpat - prepat
    # print donepat

    w.rewind()

    exp = []
    exppn = []
    exp2 = []
    exppn2 = []

    body = w.recSize + 4
    # print "body = ", body
    body = 475140
    hs = w.headerSize
    # print "hs = ", hs
    filesize = os.path.getsize(sys.argv[x])
    bint = filesize / body
    # print "bint = ", bint
    # sys.exit()

    # print
    # print "Number of steps = ", bint
    # forwardjump = input('How many steps forward:')
    forwardjump = 0
    bint = bint - forwardjump

    sqnxynum = math.sqrt(bint)
    nxynum = int(round(sqnxynum, 0))
    nxynum += 1

    if forwardjump == 0:
        1
    else:
        leap = (body * forwardjump) + (100 * forwardjump)
        w.file.seek(leap, os.SEEK_CUR)

    global count
    if huk == 0:
        count = 0
    allpat = []
    count2 = 1

    for i in range(bint):
        if i == 0:
            # print "count2 = ", count2
            go = patpos - hs - donepat
            w.file.seek(go, os.SEEK_CUR)
            p = w.next_patch()
            # print count
            # print w.file.tell()
            if len(p) == 0:
                print "STOPPEP SUPER  EARLY"
                sys.exit()
            don = p
            allpat = don

            P = np.reshape(p, (nxp, nyp))
            numrows, numcols = P.shape
            x = space + (space + nxp) * (count % nx2)
            y = space + (space + nyp) * (count / nx2)
            im[y : y + nyp, x : x + nxp] = P
            p = w.normalize(don)
            pn = p
            pn = np.reshape(np.matrix(pn), (1, coor))
            p = np.reshape(np.matrix(p), (coor, 1))
            pm = pn * p
            exppn2 = np.append(exppn2, pn)
            exp2 = np.append(exp2, pm)

            count2 += 1
            count += 1
        else:
            # print "count2 = ", count2
            prejump = body - patpos + hs
            w.file.seek(prejump, os.SEEK_CUR)
            go = patpos - 4 - donepat
            w.file.seek(go, os.SEEK_CUR)
            p = w.next_patch()
            # print w.file.tell()
            test = p
            # print count
            if len(test) == 0:
                print "stop"
                input("Press Enter to Continue")
                sys.exit()
            hh = bint / 16
            yy = i % hh
            # print "yy = ", yy

            if yy == 0:
                don = p
                allpat = np.append(allpat, don)

                P = np.reshape(p, (nxp, nyp))
                numrows, numcols = P.shape
                x = space + (space + nxp) * (count % nx2)
                y = space + (space + nyp) * (count / nx2)
                im[y : y + nyp, x : x + nxp] = P

                p = w.normalize(don)
                p = np.reshape(np.matrix(p), (coor, 1))
                j1 = 0
                j2 = coor
                pm = np.matrix(exppn2[j1:j2]) * p
                exp2 = np.append(exp2, pm)
                count += 1
                count2 += 1

    # allpat = np.split(allpat, (count - 1))
    # lenallpat = len(allpat)
    # print np.shape(allpat)
    # print "count", count
    # print

    # for i in range(lenallpat):
    #   if i == 0:
    #      other = -(i + 1)
    #      p = w.normalize(allpat[other])
    #      pn = p
    #      pn = np.reshape(np.matrix(pn),(1,patlen))
    #      p = np.reshape(np.matrix(p),(patlen,1))
    #      pm = pn * p
    #      exppn = np.append(exppn, pn)
    #      exp = np.append(exp,pm)
    #   else:
    #      other = -(i + 1)
    #      p = w.normalize(allpat[other])
    #      p = np.reshape(np.matrix(p),(patlen,1))
    #      j1 = 0
    #      j2 = patlen
    #      pm = np.matrix(exppn[j1:j2]) * p
    #      exp = np.append(exp,pm)

    wir = [[im], [testnumber]]
    return wir
def k_stability_analysis(k, forwardjump):
    w = rw.PVReadWeights(sys.argv[1])
    feature = k - 1
    count = 0
    d = np.zeros((nxp, nyp))

    w.rewind()
    for ko in np.arange(numpat):
        kxOn = conv.kxPos(ko, nx, ny, nf)
        kyOn = conv.kyPos(ko, nx, ny, nf)
        p = w.next_patch()
        if marginstart < kxOn < marginend:
            if marginstart < kyOn < marginend:
                if cluster[count] == feature:
                    e = p
                    e = e.reshape(nxp, nyp)
                    numrows, numcols = e.shape
                    count = count + 1
                    patpos = w.file.tell()
                    patchposition.append(patpos)
                else:
                    e = d
                    count = count + 1
            else:
                e = d
        else:
            e = d
        x = (nxp) * (ko % nx)
        y = (nyp) * (ko / nx)

        im2[y : y + nyp, x : x + nxp] = e

    ##########
    # Find Valuse of K-cluster[x] Patches
    ##########

    w = rw.PVReadWeights(sys.argv[3])
    wOff = rw.PVReadWeights(sys.argv[4])
    w.rewind()
    wOff.rewind()
    patpla = patchposition
    lenpat = len(patpla)

    number = w.numPatches
    count = 0

    exp = []
    expOff = []
    exppn = []
    exppnOff = []

    body = w.recSize + 4
    hs = w.headerSize
    filesize = os.path.getsize(sys.argv[3])
    bint = filesize / body

    bint = bint - forwardjump

    if forwardjump == 0:
        4
    else:
        leap = (body * forwardjump) + (100 * forwardjump)
        w.file.seek(leap, os.SEEK_CUR)

    for i in range(bint):
        if i == 0:
            for j in range(lenpat):
                if j == 0:
                    go = patpla[0] - hs - 20
                    w.file.seek(go, os.SEEK_CUR)
                    wOff.file.seek(go, os.SEEK_CUR)
                    p = w.next_patch()
                    pOff = wOff.next_patch()
                    if len(p) == 0:
                        print "STOPPEP SUPER  EARLY"
                        sys.exit()
                    don = p
                    doff = pOff

                    d = np.append(don, doff)
                    fallpat = d

                    # p = w.normalize(d)
                    # pn = p
                    # pn = np.reshape(np.matrix(pn),(1,32))
                    # p = np.reshape(np.matrix(p),(32,1))
                    # pm = pn * p
                    # exppn = np.append(exppn, pn)
                    # exp = np.append(exp,pm)

                else:
                    pospost = patpla[j - 1]
                    poscur = patpla[j]
                    jump = poscur - pospost - 20
                    w.file.seek(jump, os.SEEK_CUR)
                    wOff.file.seek(jump, os.SEEK_CUR)
                    p = w.next_patch()
                    pOff = wOff.next_patch()
                    if len(pOff) == 0:
                        print "STOPPED EARLY"
                        sys.exit()
                    don = p
                    doff = pOff
                    d = np.append(don, doff)
                    nallpat = d
                    fallpat = np.vstack((fallpat, nallpat))

                    # p = w.normalize(d)
                    # pn = p
                    # pn = np.reshape(np.matrix(pn),(1,32))
                    # p = np.reshape(np.matrix(p),(32,1))
                    # pm = pn * p
                    # exppn = np.append(exppn, pn)
                    # exp = np.append(exp,pm)
        else:
            count = 0
            prejump = body - patpla[lenpat - 1] + hs
            w.file.seek(prejump, os.SEEK_CUR)
            wOff.file.seek(prejump, os.SEEK_CUR)
            for j in range(lenpat):
                if j == 0:
                    go = patpla[0] - 4 - 20
                    w.file.seek(go, os.SEEK_CUR)
                    wOff.file.seek(go, os.SEEK_CUR)
                    p = w.next_patch()
                    pOff = wOff.next_patch()
                    test = p
                    if len(test) == 0:
                        print "stop"
                        input("Press Enter to Continue")
                        sys.exit()
                    don = p
                    doff = pOff
                    d = np.append(don, doff)
                    nfallpat = d

                    # p = w.normalize(d)
                    # p = np.reshape(np.matrix(p),(32,1))
                    # j1 = 0
                    # j2 = 32
                    # pm = np.matrix(exppn[j1:j2]) * p
                    # exp = np.append(exp,pm)
                    # count += 1
                else:
                    pospost = patpla[j - 1]
                    poscur = patpla[j]
                    jump = poscur - pospost - 20
                    w.file.seek(jump, os.SEEK_CUR)
                    wOff.file.seek(jump, os.SEEK_CUR)
                    p = w.next_patch()
                    pOff = wOff.next_patch()
                    test = pOff
                    if len(test) == 0:
                        print "stop"
                        input("Press Enter to Continue")
                        sys.exit()
                    don = p
                    doff = pOff
                    d = np.append(don, doff)
                    nfallpat = np.vstack((nfallpat, d))

                    # p = w.normalize(d)
                    # p = np.reshape(np.matrix(p),(32,1))
                    # j1 = 32 * j
                    # j2 = 32 * (j +1)
                    # pm = np.matrix(exppn[j1:j2]) * p
                    # exp = np.append(exp,pm)
                    # count += 1

            fallpat = np.dstack((fallpat, nfallpat))
    # print fallpat
    # print np.shape(fallpat)
    # print bint

    exp = []
    exppn = []
    dsallpat = np.dsplit(fallpat, bint)
    for i in range(bint):
        postds = dsallpat[-(i + 1)]
        sh = np.shape(postds)
        sh = sh[0]
        # print sh
        if i == 0:
            for j in range(sh):
                if j == 0:
                    d = postds[j]
                    p = w.normalize(d)
                    pn = p
                    pn = np.reshape(np.matrix(pn), (1, 32))
                    p = np.reshape(np.matrix(p), (32, 1))
                    pm = pn * p
                    exppn = np.append(exppn, pn)
                    exp = np.append(exp, pm)
                else:
                    d = postds[j]
                    p = w.normalize(d)
                    pn = p
                    pn = np.reshape(np.matrix(pn), (1, 32))
                    p = np.reshape(np.matrix(p), (32, 1))
                    pm = pn * p
                    exppn = np.append(exppn, pn)
                    exp = np.append(exp, pm)
        else:
            for j in range(sh):
                if j == 0:
                    d = postds[j]
                    p = w.normalize(d)
                    p = np.reshape(np.matrix(p), (32, 1))
                    j1 = 0
                    j2 = 32
                    pm = np.matrix(exppn[j1:j2]) * p
                    exp = np.append(exp, pm)
                    count += 1
                else:
                    d = postds[j]
                    p = w.normalize(d)
                    p = np.reshape(np.matrix(p), (32, 1))
                    j1 = 32 * j
                    j2 = 32 * (j + 1)
                    pm = np.matrix(exppn[j1:j2]) * p
                    exp = np.append(exp, pm)
                    count += 1

    ##########
    # Find Average of K-cluster[x] Weights
    ##########

    thenumber = lenpat
    thenumberf = float(thenumber)

    patpla = exp
    lenpat = len(patpla)

    howlong = lenpat / thenumber

    total = []
    logtotal = []

    for i in range(thenumber):
        subtotal = []
        logsubtotal = []
        for j in range(howlong):
            if i == 0:
                value = patpla[i + (thenumber * j)]
                total = np.append(total, value)
                logvalue = patpla[i + (thenumber * j)]
                logvalue = math.log10(logvalue)
                logtotal = np.append(logtotal, logvalue)
            else:
                value = patpla[i + (thenumber * j)]
                subtotal = np.append(subtotal, value)
                logvalue = patpla[i + (thenumber * j)]
                logvalue = math.log10(logvalue)
                logsubtotal = np.append(logsubtotal, logvalue)

        if i > 0:
            total = total + subtotal
        if i > 0:
            logtotal = logtotal + logsubtotal

    total = total / thenumberf
    logtotal = logtotal / thenumberf

    global total1
    global total2
    global total3
    global total4
    global total5
    global total6
    global total7
    global total8
    global total9
    global total10
    global total11
    global total12
    global total13
    global total14
    global total15
    global total16
    global logtotal1
    global logtotal2
    global logtotal3
    global logtotal4
    global logtotal5
    global logtotal6
    global logtotal7
    global logtotal8
    global logtotal9
    global logtotal10
    global logtotal11
    global logtotal12
    global logtotal13
    global logtotal14
    global logtotal15
    global logtotal16

    if feature == 0:
        total1 = [0]
        total2 = [0]
        total3 = [0]
        total4 = [0]
        total5 = [0]
        total6 = [0]
        total7 = [0]
        total8 = [0]
        total9 = [0]
        total10 = [0]
        total11 = [0]
        total12 = [0]
        total13 = [0]
        total14 = [0]
        total15 = [0]
        total16 = [0]
        logtotal1 = [0]
        logtotal2 = [0]
        logtotal3 = [0]
        logtotal4 = [0]
        logtotal5 = [0]
        logtotal6 = [0]
        logtotal7 = [0]
        logtotal8 = [0]
        logtotal9 = [0]
        logtotal10 = [0]
        logtotal11 = [0]
        logtotal12 = [0]
        logtotal13 = [0]
        logtotal14 = [0]
        logtotal15 = [0]
        logtotal16 = [0]

    # global logtotal1
    # global logtotal2
    # global logtotal3
    # global logtotal4
    # global logtotal5
    # global logtotal6
    # global logtotal7
    # global logtotal8
    # global logtotal9
    # global logtotal10
    # global logtotal11
    # global logtotal12
    # global logtotal13
    # global logtotal14
    # global logtotal15
    # global logtotal16

    if feature == 0:
        total1 = total
        logtotal1 = logtotal
    if feature == 1:
        total2 = total
        logtotal2 = logtotal
    if feature == 2:
        total3 = total
        logtotal3 = logtotal
    if feature == 3:
        total4 = total
        logtotal4 = logtotal
    if feature == 4:
        total5 = total
        logtotal5 = logtotal
    if feature == 5:
        total6 = total
        logtotal6 = logtotal
    if feature == 6:
        total7 = total
        logtotal7 = logtotal
    if feature == 7:
        total8 = total
        logtotal8 = logtotal
    if feature == 8:
        total9 = total
        logtotal9 = logtotal
    if feature == 9:
        total10 = total
        logtotal10 = logtotal
    if feature == 10:
        total11 = total
        logtotal11 = logtotal
    if feature == 11:
        total12 = total
        logtotal12 = logtotal
    if feature == 12:
        total13 = total
        logtotal13 = logtotal
    if feature == 13:
        total14 = total
        logtotal14 = logtotal
    if feature == 14:
        total15 = total
        logtotal15 = logtotal
    if feature == 15:
        total16 = total
        logtotal16 = logtotal

    return
def k_stability_analysis(k, forwardjump):
   w = rw.PVReadWeights(sys.argv[1])
   feature = k - 1
   count = 0
   d = np.zeros((nxp,nyp))

   w.rewind()
   for ko in np.arange(numpat):
      kxOn = conv.kxPos(ko, nx, ny, nf)
      kyOn = conv.kyPos(ko, nx, ny, nf)
      p = w.next_patch()
      if marginstart < kxOn < marginend:
         if marginstart < kyOn < marginend:
            if cluster[count] == feature:
               e = p
               e = e.reshape(nxp, nyp)
               numrows, numcols = e.shape
               count = count + 1
               patpos = w.file.tell()
               patchposition.append(patpos)
            else:
               e = d
               count = count + 1
         else:
            e = d
      else:
         e = d
      x = (nxp) * (ko % nx)
      y = ( nyp) * (ko / nx)

      im2[y:y+nyp, x:x+nxp] = e

   ##########
   # Find Valuse of K-cluster[x] Patches
   ##########


   w = rw.PVReadWeights(sys.argv[2])
   w.rewind()
   patpla = patchposition
   lenpat = len(patpla)


   number = w.numPatches 
   count = 0

   exp = []
   exppn = []

   body = w.recSize + 4
   hs = w.headerSize
   filesize = os.path.getsize(sys.argv[2])
   bint = filesize / body


   bint = bint - forwardjump - 1

   if forwardjump == 0:
      4
   else:
      leap = ((body * forwardjump) + (100 * forwardjump))
      w.file.seek(leap, os.SEEK_CUR)






   for i in range(bint):
      if i == 0:
         for j in range(lenpat):
            if j == 0:
               go = patpla[0] - hs - patchblen
               w.file.seek(go, os.SEEK_CUR)
               p = w.next_patch()
               if len(p) == 0:
                  print"STOPPEP SUPER  EARLY"
                  sys.exit()
               d = p
               allpat = 0
               p = w.normalize(d)
               pn = p
               pn = np.reshape(np.matrix(pn),(1,flatnxp))
               p = np.reshape(np.matrix(p),(flatnxp,1))
               pm = pn * p
               exppn = np.append(exppn, pn)
               exp = np.append(exp,pm)
               
            else:
               pospost = patpla[j - 1]
               poscur = patpla[j]
               jump = poscur - pospost - patchblen
               w.file.seek(jump, os.SEEK_CUR)
               p = w.next_patch()
               if len(p) == 0:
                  print"STOPPED EARLY"
                  sys.exit()
               d = p
               p = w.normalize(d)
               pn = p
               pn = np.reshape(np.matrix(pn),(1,flatnxp))
               p = np.reshape(np.matrix(p),(flatnxp,1))
               pm = pn * p
               exppn = np.append(exppn, pn)
               exp = np.append(exp,pm)
               #print "Ch-Ch-Changes", exppn
      else:
         count = 0
         prejump = body - patpla[lenpat-1] + hs
         w.file.seek(prejump, os.SEEK_CUR)
         for j in range(lenpat):
            if j == 0:
               go = patpla[0] - 4 - patchblen
               w.file.seek(go, os.SEEK_CUR)
               p = w.next_patch()
               test = p
               if len(test) == 0:
                  print "stop"
                  input('Press Enter to Continue')
                  sys.exit()
               d = p
               p = w.normalize(d)
               p = np.reshape(np.matrix(p),(flatnxp,1))
               j1 = 0
               j2 = flatnxp
               pm = np.matrix(exppn[j1:j2]) * p
               exp = np.append(exp,pm)
               count += 1
            else:
               pospost = patpla[j - 1]
               poscur = patpla[j]
               jump = poscur - pospost - patchblen
               w.file.seek(jump, os.SEEK_CUR)
               p = w.next_patch()
               test = p
               if len(test) == 0:
                  print "stop"
                  input('Press Enter to Continue')
                  sys.exit()
               d = p
               p = w.normalize(d)
               p = np.reshape(np.matrix(p),(flatnxp,1))
               j1 = flatnxp * j
               j2 = flatnxp * (j +1)
               pm = np.matrix(exppn[j1:j2]) * p
               exp = np.append(exp,pm)
               count += 1



   ##########
   # Find Average of K-cluster[x] Weights
   ##########


   thenumber = lenpat
   thenumberf = float(thenumber)

   patpla = exp
   lenpat = len(patpla)


   howlong = lenpat / thenumber

   total = []
   logtotal = []

   for i in range(thenumber):
      subtotal = []
      logsubtotal = []
      for j in range(howlong):
         if i == 0:
            value = patpla[i + (thenumber * j)]
            total = np.append(total, value)
            logvalue = patpla[i + (thenumber * j)]
            logvalue = math.log10(logvalue)
            logtotal = np.append(logtotal, logvalue)
         else:
            value = patpla[i + (thenumber * j)]
            subtotal = np.append(subtotal, value) 
            logvalue = patpla[i + (thenumber * j)]
            logvalue = math.log10(logvalue)
            logsubtotal = np.append(logsubtotal, logvalue)
        
      if i > 0:
         total = total + subtotal
      if i > 0:
         logtotal = logtotal + logsubtotal


   total = total / thenumberf
   logtotal = logtotal / thenumberf


   global total1
   global total2
   global total3
   global total4
   global total5
   global total6
   global total7
   global total8
   global total9
   global total10
   global total11
   global total12
   global total13
   global total14
   global total15
   global total16
   global logtotal1
   global logtotal2
   global logtotal3
   global logtotal4
   global logtotal5
   global logtotal6
   global logtotal7
   global logtotal8
   global logtotal9
   global logtotal10
   global logtotal11
   global logtotal12
   global logtotal13
   global logtotal14
   global logtotal15
   global logtotal16

   if feature == 0:
      total1 = [0.0]
      total2 = [0.0]
      total3 = [0.0]
      total4 = [0.0]
      total5 = [0.0]
      total6 = [0.0]
      total7 = [0.0]
      total8 = [0.0]
      total9 = [0.0]
      total10 = [0.0]
      total11 = [0.0]
      total12 = [0.0]
      total13 = [0.0]
      total14 = [0.0]
      total15 = [0.0]
      total16 = [0.0]
      logtotal1 = [0.0]
      logtotal2 = [0.0]
      logtotal3 = [0.0]
      logtotal4 = [0.0]
      logtotal5 = [0.0]
      logtotal6 = [0.0]
      logtotal7 = [0.0]
      logtotal8 = [0.0]
      logtotal9 = [0.0]
      logtotal10 = [0.0]
      logtotal11 = [0.0]
      logtotal12 = [0.0]
      logtotal13 = [0.0]
      logtotal14 = [0.0]
      logtotal15 = [0.0]
      logtotal16 = [0.0]

   if feature == 0:
      total1 = total 
      logtotal1 = logtotal
   if feature == 1:
      total2 = total
      logtotal2 = logtotal
   if feature == 2:
      total3 = total
      logtotal3 = logtotal
   if feature == 3:
      total4 = total
      logtotal4 = logtotal
   if feature == 4:
      total5 = total
      logtotal5 = logtotal
   if feature == 5:
      total6 = total
      logtotal6 = logtotal
   if feature == 6:
      total7 = total
      logtotal7 = logtotal
   if feature == 7:
      total8 = total
      logtotal8 = logtotal
   if feature == 8:
      total9 = total
      logtotal9 = logtotal
   if feature == 9:
      total10 = total
      logtotal10 = logtotal
   if feature == 10:
      total11 = total
      logtotal11 = logtotal
   if feature == 11:
      total12 = total
      logtotal12 = logtotal
   if feature == 12:
      total13 = total
      logtotal13 = logtotal
   if feature == 13:
      total14 = total
      logtotal14 = logtotal
   if feature == 14:
      total15 = total
      logtotal15 = logtotal
   if feature == 15:
      total16 = total
      logtotal16 = logtotal

   return
def patchover(x, huk, testnumber):

    w = rw.PVReadWeights(sys.argv[x])

    space = 1

    d = np.zeros((4, 4))

    nx = w.nx
    ny = w.ny
    nxp = w.nxp
    nyp = w.nyp
    coor = nxp * nyp
    #print coor

    patlen = nxp * nyp
    numpat = w.numPatches
    nf = w.nf
    check = sys.argv[1]
    a = check.find("w")
    b = check[a].strip("w")
    #print b

    margin = 10

    marginstart = margin
    marginend = nx - margin
    acount = 0
    patchposition = []

    nx = w.nx
    ny = w.ny
    nxp = w.nxp
    nyp = w.nyp

    test = 0

    if testnumber == 0:
        for i in range(1):
            while test == 0:
                testnumber = random.randint(0, numpat)
                for i in range(128):
                    for j in range(128):
                        if marginstart < i < marginend:
                            if marginstart < j < marginend:
                                if testnumber == (j + (i * 128)):
                                    test = 1

    #testnumber = (69 + (62 * 128))
    postpat = []

    for ko in range(numpat):
        kxOn = conv.kxPos(ko, nx, ny, nf)
        kyOn = conv.kyPos(ko, nx, ny, nf)
        prepat = w.file.tell()
        p = w.next_patch()
        postpat = w.file.tell()
        #print postpat
        if marginstart < kxOn < marginend:
            if marginstart < kyOn < marginend:
                if testnumber == (kxOn + (kyOn * 128)):
                    patpos = w.file.tell()
                    #print
                    #print "kx", kxOn
                    #print "ky", kyOn

    donepat = postpat - prepat
    #print donepat

    w.rewind()

    exp = []
    exppn = []
    exp2 = []
    exppn2 = []

    body = w.recSize + 4
    #print "body = ", body
    body = 475140
    hs = w.headerSize
    #print "hs = ", hs
    filesize = os.path.getsize(sys.argv[x])
    bint = filesize / body
    #print "bint = ", bint
    #sys.exit()

    #print
    #print "Number of steps = ", bint
    #forwardjump = input('How many steps forward:')
    forwardjump = 0
    bint = bint - forwardjump

    sqnxynum = math.sqrt(bint)
    nxynum = int(round(sqnxynum, 0))
    nxynum += 1

    if forwardjump == 0:
        1
    else:
        leap = ((body * forwardjump) + (100 * forwardjump))
        w.file.seek(leap, os.SEEK_CUR)

    global count
    if huk == 0:
        count = 0
    allpat = []
    count2 = 1

    for i in range(bint):
        if i == 0:
            #print "count2 = ", count2
            go = patpos - hs - donepat
            w.file.seek(go, os.SEEK_CUR)
            p = w.next_patch()
            #print count
            #print w.file.tell()
            if len(p) == 0:
                print "STOPPEP SUPER  EARLY"
                sys.exit()
            don = p
            allpat = don

            P = np.reshape(p, (nxp, nyp))
            numrows, numcols = P.shape
            x = space + (space + nxp) * (count % nx2)
            y = space + (space + nyp) * (count / nx2)
            im[y:y + nyp, x:x + nxp] = P
            p = w.normalize(don)
            pn = p
            pn = np.reshape(np.matrix(pn), (1, coor))
            p = np.reshape(np.matrix(p), (coor, 1))
            pm = pn * p
            exppn2 = np.append(exppn2, pn)
            exp2 = np.append(exp2, pm)

            count2 += 1
            count += 1
        else:
            #print "count2 = ", count2
            prejump = body - patpos + hs
            w.file.seek(prejump, os.SEEK_CUR)
            go = patpos - 4 - donepat
            w.file.seek(go, os.SEEK_CUR)
            p = w.next_patch()
            #print w.file.tell()
            test = p
            #print count
            if len(test) == 0:
                print "stop"
                input('Press Enter to Continue')
                sys.exit()
            hh = bint / 16
            yy = i % hh
            #print "yy = ", yy

            if yy == 0:
                don = p
                allpat = np.append(allpat, don)

                P = np.reshape(p, (nxp, nyp))
                numrows, numcols = P.shape
                x = space + (space + nxp) * (count % nx2)
                y = space + (space + nyp) * (count / nx2)
                im[y:y + nyp, x:x + nxp] = P

                p = w.normalize(don)
                p = np.reshape(np.matrix(p), (coor, 1))
                j1 = 0
                j2 = coor
                pm = np.matrix(exppn2[j1:j2]) * p
                exp2 = np.append(exp2, pm)
                count += 1
                count2 += 1

    #allpat = np.split(allpat, (count - 1))
    #lenallpat = len(allpat)
    #print np.shape(allpat)
    #print "count", count
    #print

    #for i in range(lenallpat):
    #   if i == 0:
    #      other = -(i + 1)
    #      p = w.normalize(allpat[other])
    #      pn = p
    #      pn = np.reshape(np.matrix(pn),(1,patlen))
    #      p = np.reshape(np.matrix(p),(patlen,1))
    #      pm = pn * p
    #      exppn = np.append(exppn, pn)
    #      exp = np.append(exp,pm)
    #   else:
    #      other = -(i + 1)
    #      p = w.normalize(allpat[other])
    #      p = np.reshape(np.matrix(p),(patlen,1))
    #      j1 = 0
    #      j2 = patlen
    #      pm = np.matrix(exppn[j1:j2]) * p
    #      exp = np.append(exp,pm)

    wir = [[im], [testnumber]]
    return wir
      row = int(y+0.5)
      x2 = (x / 20.0)
      y2 = (y / 20.0) 
      x = (x / 5.0)
      y = (y / 5.0)
      if col>=0 and col<numcols and row>=0 and row<numrows:
         z = P[row,col]
         return 'x=%1.4f, y=%1.4f, z=%1.4f'%(x, y, z)
      else:
         return 'x=%1.4d, y=%1.4d, x2=%1.4d, y2=%1.4d'%(int(x), int(y), int(x2), int(y2))


   k = 24
   for ko in range(numpat):
      kxOn = conv.kxPos(ko, nx, ny, nf)
      kyOn = conv.kyPos(ko, nx, ny, nf)
      p = w.next_patch()
      if marginstart < kxOn < marginend:
         if marginstart < kyOn < marginend:
            acount = acount + 1
            if kxOn == margin + 1 and kyOn == margin + 1:
               d = p
            else:
               d = np.vstack((d,p))

   wd = sp.whiten(d)
   result = sp.kmeans2(wd, k)
   cluster = result[1]

   k2 = k / 2
def k_stability_analysis(k, forwardjump):
   w = rw.PVReadWeights(sys.argv[1])
   feature = k - 1
   count = 0
   d = np.zeros((nxp,nyp))

   w.rewind()
   for ko in np.arange(numpat):
      kxOn = conv.kxPos(ko, nx, ny, nf)
      kyOn = conv.kyPos(ko, nx, ny, nf)
      p = w.next_patch()
      if marginstart < kxOn < marginend:
         if marginstart < kyOn < marginend:
            if cluster[count] == feature:
               e = p
               e = e.reshape(nxp, nyp)
               numrows, numcols = e.shape
               count = count + 1
               patpos = w.file.tell()
               patchposition.append(patpos)
            else:
               e = d
               count = count + 1
         else:
            e = d
      else:
         e = d
      x = (nxp) * (ko % nx)
      y = ( nyp) * (ko / nx)

      im2[y:y+nyp, x:x+nxp] = e

   ##########
   # Find Valuse of K-cluster[x] Patches
   ##########


   w = rw.PVReadWeights(sys.argv[3])
   wOff = rw.PVReadWeights(sys.argv[4])
   w.rewind()
   wOff.rewind()
   patpla = patchposition
   lenpat = len(patpla)


   number = w.numPatches 
   count = 0

   exp = []
   expOff = []
   exppn = []
   exppnOff = []

   body = w.recSize + 4
   hs = w.headerSize
   filesize = os.path.getsize(sys.argv[3])
   bint = filesize / body


   bint = bint - forwardjump

   if forwardjump == 0:
      4
   else:
      leap = ((body * forwardjump) + (100 * forwardjump))
      w.file.seek(leap, os.SEEK_CUR)






   for i in range(bint):
      if i == 0:
         for j in range(lenpat):
            if j == 0:
               go = patpla[0] - hs - 20
               w.file.seek(go, os.SEEK_CUR)
               wOff.file.seek(go, os.SEEK_CUR)
               p = w.next_patch()
               pOff = wOff.next_patch()
               if len(p) == 0:
                  print"STOPPEP SUPER  EARLY"
                  sys.exit()
               don = p
               doff = pOff
               

               d = np.append(don, doff)
               fallpat = d


               #p = w.normalize(d)
               #pn = p
               #pn = np.reshape(np.matrix(pn),(1,32))
               #p = np.reshape(np.matrix(p),(32,1))
               #pm = pn * p
               #exppn = np.append(exppn, pn)
               #exp = np.append(exp,pm)
               
            else:
               pospost = patpla[j - 1]
               poscur = patpla[j]
               jump = poscur - pospost - 20
               w.file.seek(jump, os.SEEK_CUR)
               wOff.file.seek(jump, os.SEEK_CUR)
               p = w.next_patch()
               pOff = wOff.next_patch()
               if len(pOff) == 0:
                  print"STOPPED EARLY"
                  sys.exit()
               don = p
               doff = pOff
               d = np.append(don, doff)
               nallpat = d
               fallpat = np.vstack((fallpat, nallpat))


               #p = w.normalize(d)
               #pn = p
               #pn = np.reshape(np.matrix(pn),(1,32))
               #p = np.reshape(np.matrix(p),(32,1))
               #pm = pn * p
               #exppn = np.append(exppn, pn)
               #exp = np.append(exp,pm)
      else:
         count = 0
         prejump = body - patpla[lenpat-1] + hs
         w.file.seek(prejump, os.SEEK_CUR)
         wOff.file.seek(prejump, os.SEEK_CUR)
         for j in range(lenpat):
            if j == 0:
               go = patpla[0] - 4 - 20
               w.file.seek(go, os.SEEK_CUR)
               wOff.file.seek(go, os.SEEK_CUR)
               p = w.next_patch()
               pOff = wOff.next_patch()
               test = p
               if len(test) == 0:
                  print "stop"
                  input('Press Enter to Continue')
                  sys.exit()
               don = p
               doff = pOff
               d = np.append(don, doff)
               nfallpat = d

               #p = w.normalize(d)
               #p = np.reshape(np.matrix(p),(32,1))
               #j1 = 0
               #j2 = 32
               #pm = np.matrix(exppn[j1:j2]) * p
               #exp = np.append(exp,pm)
               #count += 1
            else:
               pospost = patpla[j - 1]
               poscur = patpla[j]
               jump = poscur - pospost - 20
               w.file.seek(jump, os.SEEK_CUR)
               wOff.file.seek(jump, os.SEEK_CUR)
               p = w.next_patch()
               pOff = wOff.next_patch()
               test = pOff
               if len(test) == 0:
                  print "stop"
                  input('Press Enter to Continue')
                  sys.exit()
               don = p
               doff = pOff
               d = np.append(don, doff)
               nfallpat = np.vstack((nfallpat, d))




               #p = w.normalize(d)
               #p = np.reshape(np.matrix(p),(32,1))
               #j1 = 32 * j
               #j2 = 32 * (j +1)
               #pm = np.matrix(exppn[j1:j2]) * p
               #exp = np.append(exp,pm)
               #count += 1  

         fallpat = np.dstack((fallpat, nfallpat))
   #print fallpat
   #print np.shape(fallpat)
   #print bint

   exp = []
   exppn = []
   dsallpat = np.dsplit(fallpat, bint)
   for i in range(bint):
      postds = dsallpat[-(i + 1)]
      sh = np.shape(postds)
      sh = sh[0]
      #print sh
      if i == 0:
         for j in range(sh):
            if j == 0:
               d = postds[j]
               p = w.normalize(d)
               pn = p
               pn = np.reshape(np.matrix(pn), (1,32))
               p = np.reshape(np.matrix(p), (32,1))
               pm = pn * p
               exppn = np.append(exppn, pn)
               exp = np.append(exp, pm)
            else:
               d = postds[j]
               p = w.normalize(d)
               pn = p
               pn = np.reshape(np.matrix(pn),(1,32))
               p = np.reshape(np.matrix(p),(32,1))
               pm = pn * p
               exppn = np.append(exppn, pn)
               exp = np.append(exp, pm)
      else:
         for j in range(sh):
            if j == 0:
               d = postds[j]
               p = w.normalize(d)
               p = np.reshape(np.matrix(p),(32,1))
               j1 = 0
               j2 = 32
               pm = np.matrix(exppn[j1:j2]) * p
               exp = np.append(exp, pm)
               count += 1
            else:
               d = postds[j]
               p = w.normalize(d)
               p = np.reshape(np.matrix(p),(32,1))
               j1 = 32 * j
               j2 = 32 * (j + 1)
               pm = np.matrix(exppn[j1:j2]) * p
               exp = np.append(exp, pm)
               count += 1







   ##########
   # Find Average of K-cluster[x] Weights
   ##########


   thenumber = lenpat
   thenumberf = float(thenumber)

   patpla = exp
   lenpat = len(patpla)


   howlong = lenpat / thenumber

   total = []
   logtotal = []

   for i in range(thenumber):
      subtotal = []
      logsubtotal = []
      for j in range(howlong):
         if i == 0:
            value = patpla[i + (thenumber * j)]
            total = np.append(total, value)
            logvalue = patpla[i + (thenumber * j)]
            logvalue = math.log10(logvalue)
            logtotal = np.append(logtotal, logvalue)
         else:
            value = patpla[i + (thenumber * j)]
            subtotal = np.append(subtotal, value) 
            logvalue = patpla[i + (thenumber * j)]
            logvalue = math.log10(logvalue)
            logsubtotal = np.append(logsubtotal, logvalue)
        
      if i > 0:
         total = total + subtotal
      if i > 0:
         logtotal = logtotal + logsubtotal


   total = total / thenumberf
   logtotal = logtotal / thenumberf


   global total1
   global total2
   global total3
   global total4
   global total5
   global total6
   global total7
   global total8
   global total9
   global total10
   global total11
   global total12
   global total13
   global total14
   global total15
   global total16
   global logtotal1
   global logtotal2
   global logtotal3
   global logtotal4
   global logtotal5
   global logtotal6
   global logtotal7
   global logtotal8
   global logtotal9
   global logtotal10
   global logtotal11
   global logtotal12
   global logtotal13
   global logtotal14
   global logtotal15
   global logtotal16

   
   if feature == 0:
      total1=[0]
      total2=[0]
      total3=[0]
      total4=[0]
      total5=[0]
      total6=[0]
      total7=[0]
      total8=[0]
      total9=[0]
      total10=[0]
      total11=[0]
      total12=[0]
      total13=[0]
      total14=[0]
      total15=[0]
      total16=[0]
      logtotal1=[0]
      logtotal2=[0]
      logtotal3=[0]
      logtotal4=[0]
      logtotal5=[0]
      logtotal6=[0]
      logtotal7=[0]
      logtotal8=[0]
      logtotal9=[0]
      logtotal10=[0]
      logtotal11=[0]
      logtotal12=[0]
      logtotal13=[0]
      logtotal14=[0]
      logtotal15=[0]
      logtotal16=[0]



   #global logtotal1
   #global logtotal2
   #global logtotal3
   #global logtotal4
   #global logtotal5
   #global logtotal6
   #global logtotal7
   #global logtotal8
   #global logtotal9
   #global logtotal10
   #global logtotal11
   #global logtotal12
   #global logtotal13
   #global logtotal14
   #global logtotal15
   #global logtotal16

   if feature == 0:
      total1 = total 
      logtotal1 = logtotal
   if feature == 1:
      total2 = total
      logtotal2 = logtotal
   if feature == 2:
      total3 = total
      logtotal3 = logtotal
   if feature == 3:
      total4 = total
      logtotal4 = logtotal
   if feature == 4:
      total5 = total
      logtotal5 = logtotal
   if feature == 5:
      total6 = total
      logtotal6 = logtotal
   if feature == 6:
      total7 = total
      logtotal7 = logtotal
   if feature == 7:
      total8 = total
      logtotal8 = logtotal
   if feature == 8:
      total9 = total
      logtotal9 = logtotal
   if feature == 9:
      total10 = total
      logtotal10 = logtotal
   if feature == 10:
      total11 = total
      logtotal11 = logtotal
   if feature == 11:
      total12 = total
      logtotal12 = logtotal
   if feature == 12:
      total13 = total
      logtotal13 = logtotal
   if feature == 13:
      total14 = total
      logtotal14 = logtotal
   if feature == 14:
      total15 = total
      logtotal15 = logtotal
   if feature == 15:
      total16 = total
      logtotal16 = logtotal

   return

      plt.show() 

      sys.exit()

      if 1 == 1:
         kd = []
         AW = AW.reshape(lenofb, 1)
         AWO = AWO.reshape(lenofb, 1)
         count = 0

         for k in range(w.numPatches):
            p = w.next_patch()
            pO = wO.next_patch()
            kx = conv.kyPos(k, nx, ny, nf)
            ky = conv.kyPos(k, nx, ny, nf)
            if len(p) != nxp * nyp:
               continue

            #print "p = ", p

            count += 1
            #print "count = ", count
            if AW[k] == 1:
               if len(kd) == 0:
                  don = p
                  doff = pO
                  kd = np.append(don, doff)
               else:
                  don = p
Beispiel #20
0
   count = numrows * numcols
   for k in range(numbins): bins[k] = k * 1.0/(numbins-1)
   for k in range(len(M)):
      for b in range(numbins):
         if (M[k] > bins[b]): dist[b] += 1
   dist = dist/count

   # print maximum projection
   #
   maxp = 0
   maxk = 0
   for k in range(len(M)):
      if M[k] > maxp: maxp = M[k]; maxk = k

   kx = conv.kxPos(k, numcols, numrows, w.nf)
   ky = conv.kyPos(k, numcols, numrows, w.nf)
   print "maximum projected value = ", maxp, maxk, kx, ky

   M = M.reshape( (numrows,numcols) )

   # print averaged projection over column
   #
   s = np.zeros(numcols)
   maxs = 0.0
   maxcol = 0
   for col in range(numcols):
      s[col] = np.sum(M[:,col])
      if s[col] > maxs: maxs = s[col]; maxcol = col
   s = s/numrows
   print "(maxcol, maxsum) = (", maxcol, ",", maxs/numrows, ")"