0

The pertinent code runs as follows:

model = Prism(x1, x2, y1, y2, z1, z2, {'density': ρ})
data = np.array([prism.potential(x, y, z, [model])

I have to input ~50,000 prisms into the file. Simple testing has determined that a format which works is as follows:

model1 = Prism(x1, x2, y1, y2, z1, z2, {'density': ρ})
model2 = Prism(x1, x2, y1, y2, z1, z2, {'density': ρ})
model3 = Prism(x1, x2, y1, y2, z1, z2, {'density': ρ})
data = np.array([prism.potential(x, y, z, [model1, model2, model3])

The file I need to import the prisms from is formatted such as:

x1 x2 y1 y2 z1 z2 ρ

It contains ~50,000 lines of said data. What I am trying to figure out how to do is to import and run all 50,000 prisms in the one script. I know that it is not possible to write model1 ... model50000 into the script, I am just hoping there is a nice way around it?

Thanks in advance!

Vlad
  • 135
  • 2
  • 13
  • Figure out how to convert one line, and then use a loop to do it for all the input lines – Markku K. Dec 09 '14 at 04:45
  • I'm confused what your asking. Do you want to import it to run like part of your code? Or you you want to read the data into Python? – Torkoal Dec 09 '14 at 04:45
  • @Torkoal I am still quite inexperienced with coding. I am not even sure how to answer your question. All I want to do is figure out how to run my script, whichever method it takes. – Vlad Dec 09 '14 at 04:49
  • @MarkkuK. Thanks. I am still rather inexperienced and don't know how to do even that. – Vlad Dec 09 '14 at 04:50
  • You could store each entry into a list `model` and then access each individual one by indexing into the list. – Asish M. Dec 09 '14 at 05:03
  • In the call to `prism.potential`, where is the x, y and z coming from? Is the last argument just a list of all the Prisms? – chthonicdaemon Dec 09 '14 at 05:34
  • @chthonicdaemon They are defined elsewhere and regard a line I am simulating: n = 500, x = np.zeros(n), y = np.zeros(n), z = np.linspace(0, 5000, n) – Vlad Dec 09 '14 at 06:14

2 Answers2

3

As a general rule, if you encounter code like this, where similar variables with number endings are being used:

a1 = 4
a2 = 2
a3 = 5

You can replace these variables with a list which you can either build at once:

a = [4, 2, 5]

or build incrementally

a = []
a.append(4)
a.append(2)
a.append(5)

Applying this pattern to your problem, you'll notice you have model1, model2 and so on. This is the clue that we need a list. In fact, the prism.potential function accepts such a list.

Also, I notice you are using numpy, so you have access to the numpy.loadtxt function, which can read the file into an array.

So,

# Read the file into an array
filedata = np.loadtxt('filename')

# Build a list with all the Prisms
models = []
for x1, x2, y1, y2, z1, z2, ρ in filedata:
    models.append(Prism(x1, x2, y1, y2, z1, z2, {'density': ρ}))

Now we have a list of models which we can pass on the other functions. You'll have to be more explicit about how that part works.

chthonicdaemon
  • 19,180
  • 2
  • 52
  • 66
1

you could loop through each line in the file:

with open(fname) as f:
    content = f.readlines()
    for line in content: 
        (x1, x2, y1, y2, z1, z2, p) = line.split()  # for space separated fields
        # do something

that will read the fields in as strings, which you may need them cast to numbers, so for that as per the answer here you could do things like:

>>> a = "545.2222"
>>> float(a)
545.22220000000004
>>> int(float(a))
545

Edit: the csv module might good for this, especially if some or all of your fields are numeric and any string fields are surrounded by quotes, because it gives you some control over how fields are interpreted as a type. Pass quoting=csv.QUOTE_NONNUMERIC to the reader to tell it that all unquoted columns are numeric:

import csv
with open('data.txt') as csvfile:
    reader = csv.reader(csvfile, delimiter=' ', quoting=csv.QUOTE_NONNUMERIC)
    for row in reader:
        # row is a list that contains your values
Community
  • 1
  • 1
brobas
  • 596
  • 4
  • 5