0

I am using the following code to write an array of statistic values into a text file (row wise):

data_in = np.loadtxt('input_file.asc')

with open('output_file.txt', 'a+') as outfile:
    values = data_in[:,1]
    min_data = np.min(values)
    max_data = np.max(values)
    avg_data = np.mean(values)
    stats = np.array([min_data, max_data, avg_data])
    np.savetxt(outfile, stats.reshape(1,stats.shape[0]), delimiter=' ', newline='\n')

I want to use this code to read multiple data_in files and append the statistics into a new row of 'output_file.txt'. I'm using the reshape function as suggested in a previous post here, so every batch of statistics is stored in a new row.

My problem is when I want to access the data stored in 'output_file.txt' for later plotting. If I use:

stats_out = np.loadtxt('output_file.txt') 
min_values = stats_out[:,0]
max_values = stats_out[:,1] 
avg_values = stats_out[:,2]

I get the Error message: IndexError: too many indices

Is this error caused because I'm using the reshape function?

An example of output_file.txt after running the script several times is:

0.077 -0.330 0.303
0.107 -0.506 0.350
0.092 -0.548 0.405
0.138 -2.358 0.445
0.100 -0.461 0.337

An example of input_file.asc:

-1492.292540 -87.984545   95.515548       NaN    NaN  8.18
-1491.349103 -87.982864   93.908249       NaN    NaN 10.55
-1490.405666 -87.979607   92.304869       NaN    NaN 10.73
-1489.462227 -87.974784   90.707865       NaN  7.740 10.60
-1488.518791 -87.968404   89.119650       NaN    NaN 10.18
-1487.575353 -87.960482   87.542539       NaN    NaN 10.10
-1486.631915 -87.951037   85.978759       NaN    NaN 10.11
6
  • Please post the full traceback. At the moment I'm not able to reproduce the problem. Commented Jul 28, 2014 at 9:29
  • Not too sure how traceback works, sorry. I've added an example of my input_file.asc to the question Commented Jul 28, 2014 at 10:29
  • Traceback is the full output of your programm code after the error arises. Commented Jul 28, 2014 at 10:58
  • After running the script I'm still not able to reproduce the problem. Commented Jul 28, 2014 at 11:03
  • min_values = stats_out[:,0] IndexError: too many indices I'm running Python2.7.8 |Anaconda 2.0.1 Commented Jul 28, 2014 at 11:40

1 Answer 1

1

The problem is that your output file has only one line. So with stats_out = np.loadtxt('output_file.txt') you will only get a onedimensional array. If you then call stats_out[:,0] you will get an IndexError.

To solve this problem, you should check the number of dimensions of your data, for example:

stats_out = np.loadtxt('output_file.txt')
if stats_out.ndim > 1:
    min_values = stats_out[:,0]
    max_values = stats_out[:,1] 
    avg_values = stats_out[:,2]
else:
    min_values = stats_out[0]
    max_values = stats_out[1] 
    avg_values = stats_out[2]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.