0

I'm working with a script that fits a line to a data set and outputs some fit parameters. The command that runs the fitter looks like:

fitter = Fitter(hdf2.root.OM01, plot=False).

I'd like to loop this command so that it outputs the fit parameters for the files 'hdf2.01', 'hdf2.02', ... 'hdf2.50'

How can I loop this code?

7
  • That doesn't look like valid Python syntax... can you post an actual code snippet? Commented Apr 19, 2012 at 19:18
  • do you mean fitter = Fitter('hdf2.01')? Commented Apr 19, 2012 at 19:23
  • What would you like to see? The Fitter function is very lengthy as you would expect, but I have a short script that contains the code mentioned above. It opens the data file and runs the fitter: hdf2 = tables.openFile('waveforms.hdf5') fitter = Fitter(hdf2.root.OM01, plot=False) I'd like to modify this script so that I can loop through the "01" part. Commented Apr 19, 2012 at 19:25
  • Yes Jason, that's what I meant. Commented Apr 19, 2012 at 19:27
  • 2
    Have you tried to loop it yet or are you asking for someone to do it for you? Commented Apr 19, 2012 at 19:31

1 Answer 1

3

If I understand correctly, you have an object that has numbered properties (OM01 through OM50). You can generate these attribute names with a loop, and use getattr to retrieve them from the object.

If your object is called root, as it is in your comment, you could do the following:

parameters = []
for i in range(1, 51):
    parameter = getattr(root, 'OM%02d' % i)
    parameters.append(parameter)

# now all of your parameters are in the parameter array

or, if you like list comprehensions (and who doesn't):

parameters = [getattr(root, 'OM%02d' % i) for i in range(1, 51)]

I guess you need to use these parameters with Fitter, which you could do as follows:

results = []
for i in range(1, 51):
    parameter = getattr(hdf2.root, 'OM%02d' % i)
    result = Fitter(parameter, plot=False)
    results.append(result)

EDIT: If your files just have the names "hdf2.root.OM01", "hdf2.root.OM02", etc. you can just do this:

results = []
for i in range(1, 51):
    filename = 'hdf2.root.OM%02d' % i
    result = Fitter(filename, plot=False)
    results.append(result)
Sign up to request clarification or add additional context in comments.

8 Comments

If you were going to generate them separately, why not simply use a list comprehension: [getattr(root, 'OM%02d' % i) for i in range(1, 51)]? Of course, the real question is, why not just use a loop and do it, rather than generating a list of parameters?
yeah, I figured the loop would be easier to read, but was tempted to write the list comprehension.
In this case I'd argue the list comp is pretty clear. Not overly long, and not nested.
Thanks, but I don't believe that using getattr in this way will achieve what I'm trying to do. The parameters aren't attributes of the object - they're the result of running the fitter on a data file. What I'd like to do is something like this: for i in range(1,3): fitter = WaveformFitter(hdf2.root.OM'54%03d' % i)
@Brian, do you just mean something like this? results=[]; for i in range(n): results.append(Fitter(hdf2.root.OM01, plot=False))?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.