def singleFile(fileName, tol, rad_tol): print("Reading " + fileName) inputMatrix = pd.read_csv(fileName, sep="\s+|,| ", engine="python").values inputMatrix = inputMatrix.astype(np.float) gaObject = ga(tol, rad_tol) gaObject.cx, gaObject.cy = len(inputMatrix[0]) / 2., len(inputMatrix) / 2. gaObject.evaluate(inputMatrix, [sys.argv[1]]) if (sys.argv[1] == "G1"): print("G1: " + str(gaObject.G1)) print("Nc: " + str(gaObject.n_edges)) print("Nv: " + str(gaObject.n_points)) if (sys.argv[1] == "G2"): print("G2 " + str(gaObject.G2)) print("Number of Vectors: " + str(gaObject.totalVet)) print("Asymmetric: " + str(gaObject.totalAssimetric)) print("Diversity: " + str(gaObject.modDiversity)) if (sys.argv[1] == "G3"): print("G3 ", gaObject.G3) print("Number of Vectors: " + str(gaObject.totalVet)) print("Asymmetric: " + str(gaObject.totalAssimetric)) print("Diversity: " + str(gaObject.phaseDiversity)) if (sys.argv[1] == "G4"): print("G4 " + str(gaObject.G4)) print("Number of Vectors: " + str(gaObject.totalVet)) print("Asymmetric: " + str(gaObject.totalAssimetric)) plot_matrix2(gaObject) plt.show()
def multipleFiles(filename, tol, rad_tol): files = [line.rstrip() for line in open(filename)] save = [] header = "" for f in files: print(f) inputMatrix = pd.read_csv(f, sep="\s+|,| ", engine="python").as_matrix() inputMatrix = inputMatrix.astype(np.float) gaObject = ga(tol, rad_tol) gaObject.cx, gaObject.cy = len( inputMatrix[0]) / 2., len(inputMatrix) / 2. gaObject.evaluate(inputMatrix, [sys.argv[1]]) if (sys.argv[1] == "G1"): print(f + " - G1 -", gaObject.G1) newline = [f, gaObject.G1, gaObject.n_edges, gaObject.n_points] save.append(newline) header = "G1,Nc,Nv" elif (sys.argv[1] == "G2"): print(f + " - G2 -", gaObject.G2) newline = [ f, gaObject.G2, float(gaObject.totalAssimetric) / float(gaObject.totalVet), gaObject.modDiversity ] save.append(newline) header = "File,G2,Va,Md" elif (sys.argv[1] == "G3"): print(f + " - G3 -", gaObject.G3) newline = [ f, gaObject.G3, float(gaObject.totalAssimetric) / float(gaObject.totalVet), gaObject.phaseDiversity ] save.append(newline) header = "File,G3,Va,Fd" elif (sys.argv[1] == "G4"): print(f + " - G4 -", gaObject.G4) header = "File,G4,Va" newline = [ f, gaObject.G4, float(gaObject.totalAssimetric) / float(gaObject.totalVet) ] save.append(newline) np.savetxt("result.csv", np.array(save), fmt="%s", header=header, delimiter=',', comments='') print("Saved at result.csv")
def runGPA(mat, tol, rad_tol, gn): gaObject = ga(mat) gaObject.cx, gaObject.cy = len(mat[0]) / 2., len(mat) / 2. gaObject.evaluate(tol, rad_tol, 1.0, [gn]) if (gn == "G1"): return gaObject.G1 elif (gn == "G2"): return gaObject.G2 elif (gn == "G3"): return gaObject.G3 elif (gn == "G4"): return gaObject.G4
def iterativeScaling(inputMatrix, gn, tol, rad_tol, trim=0, step=1, glist=[]): ''' Input: inputMatrix - the matrix to be analysed trim - the spacing (must be positive or zero - check to be done) step - trim step (must be positive - negative check to be done) glist - list containing the level(the matrix diagonal) and GPA result Output: glist - list containing the level(the matrix diagonal) and GPA result Descrition: Evaluates GPA iteratively, reducing the matrix size according to the step Required at least 4x4 submatrices (it will return the input list otherwise). ''' y, x = inputMatrix.shape dminY, dmaxY = trim, y - trim dminX, dmaxX = trim, x - trim level = max(dmaxX - dminX, dmaxY - dminY) # at least 4x4 matrices condition if (dmaxX < dminX + 3 or dmaxY < dminY + 3): return glist gaObject = ga(tol, rad_tol) gaObject.cx, gaObject.cy = len(inputMatrix[0]) / 2., len(inputMatrix) / 2. gaObject.evaluate(inputMatrix[dminY:dmaxY, dminX:dmaxX], [gn]) golist = copy.deepcopy(glist) if (gn == "G1"): golist.append([level, gaObject.G1]) elif (gn == "G2"): golist.append([level, gaObject.G2]) elif (gn == "G3"): golist.append([level, gaObject.G3]) elif (gn == "G4"): golist.append([level, gaObject.G4]) golist = iterativeScaling(inputMatrix, gn, tol, rad_tol, trim + step, int(round(1.3 * step)), golist) return golist
if __name__ == "__main__": if ('-h' in sys.argv) or ('--help' in sys.argv): printError() if (sys.argv[2] == "-l") and (len(sys.argv) != 7): printError() if (sys.argv[2] != "-l") and (len(sys.argv) != 5): printError() if not ("-l" in sys.argv): fileName = sys.argv[2] tol = float(sys.argv[3]) rad_tol = float(sys.argv[4]) print("Reading " + fileName) inputMatrix = np.loadtxt(fileName) inputMatrix = inputMatrix.astype(np.float32) gaObject = ga(inputMatrix) gaObject.evaluate(tol, rad_tol, [sys.argv[1]]) if (sys.argv[1] == "G1"): print("NC", gaObject.n_edges) print("NV", gaObject.n_points) print("G1", gaObject.G1) if (sys.argv[1] == "G2"): print("Proportion", gaObject.prop) print("Div", gaObject.div) print("G2 ", gaObject.G2) else: files = [line.rstrip() for line in open(sys.argv[3])] tol = float(sys.argv[4]) rad_tol = float(sys.argv[5]) save = [] dev = getDevice()
def fragGPA(inputMatrix, gn, tol, rad_tol, bandwidth=0.4, nbands=20, hbands=5): ''' Description: Measures the GPA, for a set of filtered images (made with butterworth). As increases the band, crops the boundaries (avoiding patterns affected by boundaries) Input: inputMatrix - the matrix to be analyzed gn - Gradient Moment (i.e. G1) tol - GPA modulus tolerance (0.0-1.0) rad_tol - GPA phase tolerance (0-2*pi) bandwidth - 0.0-1.0 nbands - number of bands hbands - hilbert curve interpolator size (total of bands = 2^(2*hbands)) Output: glist - list of GPA results avgFreq - list of the band average imgList - list of filtered images interp - Scipy interpolator, for the GPA sequence layer - GPA frequency series transformed in matrix, via PCHIP interpolation and Hilbert curves gssa - GPA of layer (it uses flexible tolerance based on hbands) ''' glist = [] avgFreq = [] imgList = [] freq = np.fft.fft2(inputMatrix) sfreq = np.fft.fftshift(freq) seq = [[ 0.5 * float(i) / float(nbands) - bandwidth / 2.0, 0.5 * float(i) / float(nbands) + bandwidth / 2.0 ] for i in range(1, nbands + 1)] # Frequencies loop (to build the series) for fb in seq: avg = np.average(fb) fsfreq = filterFreq(sfreq, 3, fb) filtered = np.real(np.fft.ifft2(np.fft.ifftshift(fsfreq))).astype( np.float) filtered = crop_center(filtered, avg / 4.0, avg / 4.0) imgList.append(filtered) avgFreq.append(np.average(fb)) gaObject = ga(tol, rad_tol) gaObject.evaluate(filtered, [gn]) if (gn == "G1"): glist.append(gaObject.G1) elif (gn == "G2"): glist.append(gaObject.G2) elif (gn == "G3"): glist.append(gaObject.G3) elif (gn == "G4"): glist.append(gaObject.G4) # Starts transforming the series in a matrix dim = pow(2, hbands) interp = interpolate.PchipInterpolator(avgFreq, glist) xs = np.linspace(min(avgFreq), max(avgFreq), dim * dim) ys = interp(xs) hcurve = hilbert.HilbertCurve(hbands, 2) layer = np.array( [[ys[hcurve.distance_from_coordinates([i, j])] for i in range(dim)] for j in range(dim)]).astype(np.float) # Now measures the gradient moment gaObject = ga(1.0 / float(dim * dim), 1.0 / float(dim * dim)) gaObject.evaluate(layer, [gn]) if (gn == "G1"): gssa = gaObject.G1 elif (gn == "G2"): gssa = gaObject.G2 elif (gn == "G3"): gssa = gaObject.G3 elif (gn == "G4"): gssa = gaObject.G4 return np.array(glist), np.array(avgFreq), np.array( imgList), interp, layer, gssa
def recursiveGN(inputMatrix, gn, tol, rad_tol, maxDepth, glist=[], level=0): ''' Input: inputMatrix - the matrix gn - Gradient moment (i.e. G1) tol - GPA modulus tolerance(0.0-1.0) rad_tol - GPA radius tolerance (0-2*pi) maxDepth - the maximum tree depth glist - List of GN in each level - defaul = empty Output: glist - List of GN in each level Description: Recursevly evaluates GN, spliting in 4 segments until the maximal depth is achieved ''' #Local analysis gaObject = ga(inputMatrix) gaObject.cx, gaObject.cy = len(inputMatrix[0]) / 2., len(inputMatrix) / 2. gaObject.evaluate(tol, rad_tol, 1.0, [gn]) mlen = math.sqrt(pow(gaObject.cx, 2.0) + pow(gaObject.cy, 2.0)) if (maxDepth <= level): return glist if (gn == "G1"): glist.append([level, gaObject.G1]) elif (gn == "G2"): glist.append([level, gaObject.G2]) elif (gn == "G3"): glist.append([level, gaObject.G3]) elif (gn == "G4"): glist.append([level, gaObject.G4]) #recursion part if (maxDepth > level and inputMatrix.shape[0] > 2 and inputMatrix.shape[1] > 2): ul, ur, ll, lr = quadSplit(inputMatrix) glist = recursiveGN(ul, gn, tol, rad_tol, maxDepth, glist=glist, level=level + 1) glist = recursiveGN(ur, gn, tol, rad_tol, maxDepth, glist=glist, level=level + 1) glist = recursiveGN(ll, gn, tol, rad_tol, maxDepth, glist=glist, level=level + 1) glist = recursiveGN(lr, gn, tol, rad_tol, maxDepth, glist=glist, level=level + 1) return glist