Saving big data in csv file - python

I am trying to save a large matrix, 1000x1000 which follows log-normal distribution. But the saved file turns out to be empty. What am I doing incorrectly here?
import numpy as np
import csv
with open('Radius.csv', 'w') as f:
shape = 1000,1000
zmin, zmax = 0.2,0.8
n = np.prod(shape)
zc = np.array([])
while True:
z = np.random.lognormal(mean=0.2, sigma=0.5, size=n * 100)
z = z[(zmin <= z) & (z < zmax)]
z = np.r_[zc, z]
if len(z) >= n:
break
inv_r = z[:n].reshape(shape)
print("1/r =",[inv_r])
writer = csv.writer(f)
writer.writerows(zip(1,[inv_r]))

It has to do with the way you are writing to rows, the zip function takes in two iterables, you passed in an int and an iterable [list]
the while loop also only ever will go through once as it stands. If you run this:
import numpy as np
import csv
with open('Radius.csv', 'w+') as f:
shape = 1000,1000
zmin, zmax = 0.2,0.8
n = np.prod(shape)
zc = np.array([])
z = np.random.lognormal(mean=0.2, sigma=0.5, size=n * 100)
z = z[(zmin <= z) & (z < zmax)]
z = np.r_[zc, z]
inv_r = z[:n].reshape(shape)
print("1/r =",[inv_r])
writer = csv.writer(f)
writer.writerows(inv_r)
it will at least log to the csv, definitely check your zip function to make sure it does what you want it to!

Related

Writing to CSV file with numpy arrays without brackets

I'm attempting to map out coordinates for a circle and export those values to excel to then plot the values. My output CSV file returns the correct values but with brackets around the values ex x,y, [0.],[9.]. I would like to learn how to remove those brackets with my current code attempt if possible.
import numpy as np
from itertools import zip_longest
r = 9
h = 1
k = 1
# r = int(input('Radius'))
# h = int(input('h'))
# k = int(input('k'))
deg = np.arange(0, 361, 1)[:, np.newaxis]
theta = (np.pi * deg * 2)/360
x = (r * np.sin(theta) + h)
y = (r * np.cos(theta) + k)
d = x, y
Points = zip_longest(*d, fillvalue = '')
with open('test.csv', 'w', encoding="ISO-8859-1", newline='') as myfile:
wr = csv.writer(myfile)
wr.writerow(('x','y','r','h','k'))
wr.writerows(Points)
myfile.close()
x and y are arrays with shape (361,1). When you iterate over such an array (as zip_longest will do), you get a stream of arrays of shape (1,) which is where your surplus brackets come from.
One solution would be to make your arrays (361,) instead (i.e, one-dimensional). Then iterating would give scalars.
Another solution would be to use np.hstack([x,y]) instead of zip, and tossing the entire resulting (361,2) array to writerows.
(Off topic: You do not need myfile.close(); the with ... construct takes care of that for you — that's what it's for!)

savig matrix output gives zip argument error

I would like to write the outputs of the following code in a text file. It gives this error:
for x in zip(c(), R1):
TypeError: zip argument #1 must support iteration
I could not find any solution. Any help please?
import numpy as np
from math import *
from scipy.integrate import quad
from scipy.integrate import odeint
xx=np.array([0.01,0.012])
yy=np.array([32.95388698,33.87900347])
Cov=[[137,168],[28155,-2217]]
with open('txtfile.txt', 'w') as f:
for j in range (1,20):
R1=np.random.uniform(0,1)
Omn=0.32+R1
Odn=1-Omn
def dl(n):
fn=xx[n]*Odn+yy[n]*Omn
return fn
def c():
f_list = []
for i in range(2): #the value '2' reflects matrix size
f_list.append(dl(i))
r1=[f_list]
r2=[[f] for f in f_list]
a=np.dot(r1,Cov)
b=np.dot(a,r2)
matrix=np.linalg.det(b)
return matrix
for x in zip(c(), R1):
f.write("{0}\t{1}\n".format(x[0],x[1]))
I appreciate your help.
Both c() and R1 are both simple values, not lists. So to write them to a file with a tab, you would just need:
f.write("{}\t{}\n".format(c(), R1))
For example:
import numpy as np
from math import *
from scipy.integrate import quad
from scipy.integrate import odeint
def dl(n):
return xx[n] * Odn + yy[n] * Omn
def c():
f_list = []
for i in range(2): #the value '2' reflects matrix size
f_list.append(dl(i))
r1 = [f_list]
r2 = [[f] for f in f_list]
a = np.dot(r1, Cov)
b = np.dot(a, r2)
matrix = np.linalg.det(b)
return matrix
xx = np.array([0.01, 0.012])
yy = np.array([32.95388698, 33.87900347])
Cov = [[137, 168], [28155, -2217]]
with open('txtfile.txt', 'w') as f:
for j in range (1,20):
R1 = np.random.uniform(0, 1)
Omn = 0.32 + R1
Odn = 1 - Omn
f.write("{}\t{}\n".format(c(), R1))
This would create your txtfile.txt as follows:
35206063.6746 0.777596199441
45374454.3839 0.926105934266
3990656.69091 0.0493187574204
28925205.8769 0.674852617966
45542873.2768 0.928417018276
4412088.81481 0.0683471360264
20148228.6097 0.510253466599
6934013.9475 0.166927414742
18602042.1473 0.477747802178
49485237.1146 0.981343401759
31379848.1448 0.716219179241
21670623.7641 0.541061316417
25859179.9751 0.620631842725
10642383.5164 0.28331967175
14640960.1091 0.387697186294
5183085.91921 0.100940240452
12734994.2117 0.340005554729
26863086.7454 0.638722906359
6227944.29448 0.141453730959
To write extra variable for each row, I would recommend you switch to using a CSV writer as follows:
import numpy as np
from math import *
from scipy.integrate import quad
from scipy.integrate import odeint
import csv
def dl(n):
return xx[n] * Odn + yy[n] * Omn
def c():
f_list = [dl(i) for i in range(2)]
r1 = [f_list]
r2 = [[f] for f in f_list]
a = np.dot(r1, Cov)
b = np.dot(a, r2)
matrix = np.linalg.det(b)
return matrix
xx = np.array([0.01, 0.012])
yy = np.array([32.95388698, 33.87900347])
Cov = [[137, 168], [28155, -2217]]
with open('txtfile.txt', 'w', newline='') as f:
csv_output = csv.writer(f, delimiter='\t')
for j in range (1,20):
R1 = np.random.uniform(0, 1)
Omn = 0.32 + R1
Odn = 1 - Omn
csv_output.writerow([c(), R1])
Input to the zip function must be *iterables such an array or list.
Please try below, hopefully it will work.
for x in zip([c()], [R1]):
f.write("{0}\t{1}\n".format(x[0],x[1]))
Documentation for zip in python3 is available here.
When you are using zip(), you are working with lists.
List contains arrays, your function and random number are just number without [] which demonstrats an array. so you can use without its for loop containing zip().
f.write("{0}\t{1}\n".format(c(),R1))
The other point: Bring functions out of the with open order.

matlab colormap in python

I need the orignal image once again but its returning greyscale
my teacher said do colormap at the beginning and said that it happens in matlab. so i want to do similar task in python too
Here is my code have a look
please help
ENCRYPTION:
import numpy as np
from PIL import Image
x = Image.open('1.jpg', 'r')
x = x.convert('L')
y = np.asarray(x.getdata(), dtype=np.int).reshape((x.size[1], x.size[0]))#changed image to matrix getdata for matrix value # dtype is int type reshape used to break 1d array into 2d array
y = np.asarray(y, dtype=np.uint8)#if values still in range 0-255!
#print(y)
z = y
w = Image.fromarray(y, mode='L')
w.save('grey_scale.bmp')
for i in range(len(z)):
for j in range(len(z[i])):
a = z[i][j]
p = int(bin(a)[2:])
p = '%08d' % p
p = p[::-1]
z[i][j] = int(p, 2)
#print(z)
C = Image.fromarray(z)
C.save('decryption.bmp')
print("DONE")
DECRYPTION:
import numpy as np
from PIL import Image
x = Image.open('decryption.bmp', 'r')
y = np.asarray(x.getdata(), dtype=np.int).reshape((x.size[1], x.size[0]))#changed image to matrix getdata for matrix value
#print(y) # dtype is int type reshape used to break 1d array into 2d array
y = np.asarray(y, dtype=np.uint8)#if values still in range 0-255!
#print(y)
z = y
for i in range(len(z)):
for j in range(len(z[i])):
a = z[i][j]
p = int(bin(a)[2:])
p = '%08d' % p
p = p[::-1]
z[i][j] = int(p, 2)
#print(z)
C = Image.fromarray(z)
C.save('Final.bmp')
print("DONE")
I think(?) what you're looking for is a Colour Mapping function like:
def create_colourmap(colour, grey):
c_map = numpy.zeros((256,4), dtype=numpy.float64)
for i in range(colour.shape[0]):
for j in range(colour.shape[1]):
tone = grey[i,j]
c_map[tone,3] +=1
count = c_map[tone, 3]
c_map[tone,:3] = (c_map[tone,:3] * (count-1) + colour[i,j])/count
return c_map.astype(numpy.uint8)[:,:3]
Which would give you ONE colour value for each Greyscale value (or Tone).
Your 'bit reversal' procedure could be simplified by another function:
def reverse_bits(arr):
for i in range(arr.shape[0]):
for j in range(arr.shape[1]):
arr[i][j] = int('{0:08b}'.format(arr[i][j])[::-1], 2)
return arr
Putting this all together, your encrypt function would be:
def encrypt(infile, outfile):
with Image.open(infile, 'r') as colour_img:
colour_arr = numpy.array(colour_img)
grey_img = colour_img.convert('L')
grey_arr = numpy.array(grey_img)
c_map = create_colourmap(colour_arr, grey_arr)
with Image.fromarray(c_map) as cmap_img:
cmap_img.save(outfile[:-4]+'_cmap.bmp')
grey_arr = reverse_bits(grey_arr)
with Image.fromarray(grey_arr) as c:
c.save(outfile)
print("Encryption DONE")
And your decrypt function:
def decrypt(infile, outfile):
with Image.open(infile, 'r') as x:
y = numpy.array(x)
y = reverse_bits(y)
colour_arr = numpy.zeros((y.shape[0], y.shape[1], 3), dtype=numpy.uint8)
with Image.open(infile[:-4]+'_cmap.bmp') as cmap_img:
cmap = numpy.array(cmap_img)
for i in range(256):
colour_arr[y==i] = cmap[i]
with Image.fromarray(colour_arr) as c:
c.save(outfile)
print("Decryption DONE")
You'll notice that the Colour Mapping will not completely restore the image colour, but will give the image some hue. I don't know what your assignment is about, but you may want to send the Colour Map along with the encrypted image, or give them another image with similar colouring.
I hope this helps you, but you will need to lean how this works, and comment where necessary to explain whats going on.
Good luck!

Python plotting loop - where is my memory going?

I have the following loop which plots a figure and then saves it onto disk, repeated iteratively 120 times. Python's RAM use initially is around 2.2GB (Data and SeaP arrays are 120x721x1440) so quite large to start. However RAM use increases on each loop iteration, so much so that by quarter of the way through (i = 30) RAM use is 7.9GB and rising. Is there a memory leak? Or a way I can prevent this increasing, I see no reason as to why it should be increasing - code is trivial. Code in the loop in question below.
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np
from scipy.ndimage.filters import minimum_filter, maximum_filter
Lon = np.linspace(-180,180,1440)
Lat = np.linspace(-90,90,721)
Lon,Lat = np.meshgrid(Lon,Lat)
m = Basemap(projection='laea',width=10000000,height=6500000,resolution ='l',lat_ts=50,lat_0=55,lon_0=-25)
X, Y = m(Lon, Lat)
def Make_SLP(PRMSL,X,Y,Cont_Int,window=30):
mn = minimum_filter(PRMSL, size=window, mode='wrap')
mx = maximum_filter(PRMSL, size=window, mode='wrap')
local_min, local_max = np.nonzero(PRMSL == mn), np.nonzero(PRMSL == mx)
clevs = np.arange(900,1100.,4.)
csl = m.contour(X,Y,PRMSL,np.arange(950,1050,Cont_Int),colors='k',linewidths=0.3)
xlows = X[local_min]; xhighs = X[local_max]
ylows = Y[local_min]; yhighs = Y[local_max]
lowvals = PRMSL[local_min]; highvals = PRMSL[local_max]
xyplotted = []
# don't plot if there is already a L or H within dmin meters.
yoffset = 0.022*(m.ymax-m.ymin)
dmin = yoffset
for x,y,p in zip(xlows, ylows, lowvals):
if x < m.xmax and x > m.xmin and y < m.ymax and y > m.ymin:
dist = [np.sqrt((x-x0)**2+(y-y0)**2) for x0,y0 in xyplotted]
if not dist or min(dist) > dmin:
plt.text(x,y,'L',fontsize=16,fontweight='bold',
ha='center',va='center',color='b',clip_on=True)
plt.text(x,y-yoffset,repr(int(p)),fontsize=9,
ha='center',va='top',color='b',
bbox = dict(boxstyle="square",ec='None',fc=(1,1,1,0.5)),clip_on=True)
xyplotted.append((x,y))
# plot highs as red H's, with max pressure value underneath.
xyplotted = []
for x,y,p in zip(xhighs, yhighs, highvals):
if x < m.xmax and x > m.xmin and y < m.ymax and y > m.ymin:
dist = [np.sqrt((x-x0)**2+(y-y0)**2) for x0,y0 in xyplotted]
if not dist or min(dist) > dmin:
plt.text(x,y,'H',fontsize=16,fontweight='bold',
ha='center',va='center',color='r',clip_on=True)
plt.text(x,y-yoffset,repr(int(p)),fontsize=9,
ha='center',va='top',color='r',
bbox = dict(boxstyle="square",ec='None',fc=(1,1,1,0.5)),clip_on=True)
xyplotted.append((x,y))
return plt
for i in range(0,100):
plt = Make_SLP(np.random.rand(721,1440)*1000,X,Y,10,window=30)
print i

Creating a Matrix of Floats to do Polynomial Regression

I'm trying to do a polynomial regression of csv file I have (or any other csv file). I am not sure how to build a matrix that contains the data set I have. Here is the current code I have.
from matplotlib.pyplot import *
import numpy as np
import csv
from math import *
f=open("data_setshort.csv", "r")
csv_f = csv.reader(f)
xval = []
yval = []
polyreg = []
for row in csv_f:
xval.append(row[0])
yval.append(row[1])
f.close()
x = np.array(xval)
y = np.array(yval)
xlist = [float(i) for i in x]
ylist = [float(i) for i in y]
print xlist
print ylist
def poly_fit(x,y):
for i in range(1, len(x)):
M = np.matrix(x[i],y[i])
return M
Matrix = poly_fit(xlist,ylist)
print Matrix
The poly_fit(x,y) is the function I am trying to build to do the polynomial regression.
Maybe I misunderstood exactly what you're trying to do, but if it's fitting a polynomial from continuous x and y values, then this will do it:
import numpy as np
xi = np.random.uniform(-3, 3, 30)
ni = np.random.uniform(0, .4, 30)
coefficients = np.polyfit(xi, ni, 3)
print coefficients
Then, to use it to generate y values given new x values:
new_x = 2.5
polynomial = np.poly1d(coefficients)
new_y = polynomial(new_x)

Categories