Saving arrays within the loop with different name - python

I have a question related to the saving an array(.npy).
I have a computation that produces a new array for every loop.
Once a while (let's say every 10 steps) I would like to save my output into .npy with a specific name, let's say result_at_step_10.npy. Of course at steps 20, the file name should be result_at_step_20.npy
how could I do that in python?

If renaming the file is what your are looking for, here is the code
import os
os.rename("old_name","new_name")

Related

How do I import images with filenames corresponding to column values in a dataframe?

I'm a doctor trying to learn some code for work, and was hoping you could help me solve a problem I have with regards to importing multiple images into python.
I am working in Jupyter Notebook, where I have created a dataframe (named df_1) using pandas. In this dataframe each row represents a patient, and the first column shows the case number for each patient (e.g. 85).
Now, what I want to do is import multiple images (.bmp) from a given folder(same location as the .ipynb file). There are many images in this folder, and I do not want all of them - only the ones who have filenames corresponding to the "case_number" column in my dataframe (e.g. 85.bmp).
I already read this post, but I must admit it was way to complicated for me to understand.
Is there some simple loop (or something else) I could create to import all images with filenames corresponding to the values of the "case number" column in the dataframe?
I was imagining something like the below would be possible, I just do not know how to write it.
for i=[(df_1['case_number'()]
cv2.imread('[i].bmp')
The images don't really need to be implemented in the dataframe, but I would like to be able to view them in my notebook by using e.g. plt.imshow(85) afterwards.
Here is an image of the head of my dataframe
Thank you for helping!
You can access all of your files using this:
imageList = []
for i in range(0, len(df_1)):
cv2.imread('./' + str(df_1['case_number'][i]) + '.bmp')
imageList.append('./' + str(df_1['case_number'][i]) + '.bmp')
plt.imshow(imagelist[x])
This is looping through every item in the case_number column, the ./ shows that your file is within the current directory, using the directory path leading up to your current file. And by making everything a string and joining it you make it so that the file path is readable. The path created by joining the strings should look something like ./85.bmp, which should open your desired file. Also, you are appending the filenames to the list so that they can be accessed by the plt.imshow()
If you would like to access the files based on their name, you can use another variable (which could be set as an input) and implement the code below
fileName = input('Enter Your Value: ')
inputFile = imageList.index('./' + fileName + '.bmp')
and from here, you could use the same plt.imshow(imagelist[x]), but replace the x with the inputFile variable.

loading a set of text files whose names are stored in an array

So I have a set of text files that are some columns of numbers and whos names are stored in an excel file. I need to load in every file in the directory who's name matches one in the excel file. I also want to inform you all that I am a python beginner, and I'm honestly not very computer savvy (but I'm trying).
I start by loading the excel file into a dataframe and then converting it to an array. Then I was trying to loop through the array and load in any files that match it, with the name of the variable holding the data being the name of the text file (without the .txt)
df=pd.read_excel('names.xlsx', sheet_name="Sheet 1")
array=df.values
for i in array:
str(array[i][0])=np.loadtxt(str(array[i][0])+'.txt')
when I try to run this I get:
str(array[i][0]) = np.loadtxt(str(array[i][0])+'.txt')
^
SyntaxError: can't assign to function call
So my questions are, how can I assign that as the variable name, and because it stops before the code gets there, is it valid to load the files in the way I have?
I found a person to help and they led me to this:
df=pd.read_excel('names.xlsx', sheet_name="Sheet 1")
array=df.values
for i in array:
x,y,z=np.loadtxt(i[0]+'.txt', dtype=float)
It's not exactly what I wanted to be able to do, but I can just put the other things I was going to do with the data in the loop so that it overwrites and does it again, which will work.

How to produce multiple output files with a single input file using the command 'np.random.normal'?

I have a file with 44,586 lines of data. It is read in using pylab:
data = pl.loadtxt("20100101.txt")
density = data[:,0]
I need to run something like...
densities = np.random.normal(density, 30, 1)
np.savetxt('1.txt', np.vstack((densities.ravel())).T)
...and create a new file named 1.txt which has all 44,586 lines of my data randomised within the parameters I desire. Will my above commands be sufficient to read through and perform what I want on every line of data?
The more complicated part on top of this - is that I want to run this 1,000 times and produce 1,000 .txt files (1.txt, 2.txt ... 1000.txt) which each run the exact same command.
I become stuck when trying to run loops in scripts, as I am still very inexperienced. I am having trouble even beginning to get this running how I desire - also I am confused how to handle saving the files with their different names. I have used np.savetxt in the past, but don't know how to make it perform this task.
Thanks for any help!
There are two minor issues - the first relates to how to select the name of the files (which can be solved using pythons support for string concatenation), the second relates to np.random.normal - which only allows a size parameter when loc is a scalar.
data = pl.loadtxt("20100101.txt")
density = data[:,0]
for i in range(1, 1001):
densities = np.random.normal(loc=density, scale=30)
np.savetxt(str(i) + '.txt', densities)

How to use numpy.savez to save array with subarrays into separate .npy files

I have just recently started using numpy and was wondering some things.
I have a numpy array that looks like this after splitting it:
[array([1,2,3]),
array([4,5,6])]
I want to use numpy.savez to save the main array into the .npz archive with each subarray in its own .npy file.
I thought using this:
numpy.savez('dataFile', mainArray)
would work but it only creates the archive with a single .npy file called arr_0.npy.
Is there a way to do something like this? and if so is there a way so that I can use any array with any number of subarrays with that method. To get these arrays I am reading from a .bin file that could contain any number of elements that would split into any number of arrays. This is why I'm having a hard time.
Is there a way to add files to an already created .npz file?
After doing more research I came upon the answer to my main question. I found out that you can use the *arg to loop through the list of arrays to add them.
I changed the code to
numpy.savez('test', *[mainArray[x] for x in rang(len(mainArray))])
This gave me the solution i was looking for. Thank you for your help.
If you want to save the subarrays in your main array, then you probably need to use save manually, i.e.
mainArray = [np.array([1,2,3]), np.array([4,5,6])]
for i in range(len(mainArray)):
np.save('dataFile_%i'%i, mainArray[i] )
Or you can use savez to save subarrays separately and load them later.
mainArray = [np.array([1,2,3]), np.array([4,5,6])]
np.savez('dataFile', mainArray[0], mainArray[1])
npzfile = np.load('dataFile.npz')
npzfile['arr_0']
npzfile['arr_1']

Storing data globally in Python

Django and Python newbie here. Ok, so I want to make a webpage where the user can enter a number between 1 and 10. Then, I want to display an image corresponding to that number. Each number is associated with an image filename, and these 10 pairs are stored in a list in a .txt file.
One way to retrieve the appropriate filename is to create a NumToImage model, which has an integer field and a string field, and store all 10 NumToImage objects in the SQL database. I could then retrieve the filename for any query number. However, this does not seem like such a great solution for storing a simple .txt file which I know is not going to change.
So, what is the way to do this in Python, without using a database? I am used to C++, where I would create an array of strings, one for each of the numbers, and load these from the .txt file when the application starts. This vector would then lie within a static object such that I can access it from anywhere in my application.
How can a similar thing be done in Python? I don't know how to instantiate a Python object and then enable it to be accessible from other Python scripts. The only way I can think of doing this is to pass the object instance as an argument for every single function that I call, which is just silly.
What's the standard solution to this?
Thank you.
The Python way is quite similar: you run code at the module level, and create objects in the module namespace that can be imported by other modules.
In your case it might look something like this:
myimage.py
imagemap = {}
# Now read the (image_num, image_path) pairs from the
# file one line at a time and do:
# imagemap[image_num] = image_path
views.py
from myimage import imagemap
def my_view(image_num)
image_path = imagemap[image_num]
# do something with image_path

Categories

Resources