Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Some i/o problems

Tags:

python

io

numpy

Disclaimer: This is for an assignment. If you feel I'm just "asking you to do my homework for me" let me know and I'll ask a more broad question, or just give me hints if you can please.

Ok so I've got two sets of 100 files. The first set is called cell_spks_n where n=1,...,100 and the second set is called cell_dirs_n, where n=1,...,100. numpy's loadtxt loads these files in a 5x8 array which is perfect. I want to load these all up and do some stuff to them. Now my issue is naming all these files. I thought of making two lists called dirs and spks, and storing the arrays in them sequentially. However something goes wrong and it only appends one element that numpy loads and I'm not sure what's going wrong.

from numpy import *

files = 100
for i in range(1, files+1):
    dirs = []
    spks = []
    if (0<i<9):
        dirs_name = 'neurondata/cell_dirs_00' + str(i) + '.txt' 
        spks_name = 'neurondata/cell_spks_00' + str(i) + '.txt'
        dirs.append(loadtxt(dirs_name))
        spks.append(loadtxt(spks_name))
    elif (9<i<=99):
        dirs_name = 'neurondata/cell_dirs_0' + str(i) + '.txt' 
        spks_name = 'neurondata/cell_spks_0' + str(i) + '.txt'
        dirs.append(loadtxt(dirs_name))
        spks.append(loadtxt(spks_name))
    else:
        dirs.append(loadtxt('neurondata/cell_dirs_100.txt'))
        spks.append(loadtxt('neurondata/cell_spks_100.txt'))


# Fancy stuff gets done here

I think it might even be a bad idea loading these as arrays which I'll have to mind my indexing to access the data. The ideal case would be to have some kind of loop that goes something like this:

for i in range(1,files+1):
    spk_i = loadtxt('cell_spks_i')
    dir_i = loadtxt('cell_dirs_i')

Thoughts?

Edit: I forgot to some output

If I say

for item in spks:
    print item
print shape(spks)

I get as output

[[ 25.287356   23.655914   22.988506   14.285714    2.3809524   4.3478261
19.354839   11.764706 ]
[ 16.129032   26.666667   19.565217    7.2289157   5.8823529  13.861386
7.0588235  12.195122 ]
[ 13.157895   16.86747    26.190476   29.62963    12.121212   12.307692
27.5        19.047619 ]
[ 18.518519   25.396825   34.482759   14.814815   20.224719    9.4117647
6.6666667  21.686747 ]
[ 32.55814    22.988506   26.506024   21.782178   13.114754    2.7777778
14.814815    8.6021505]]
(1, 5, 8)
like image 991
faskiat Avatar asked Apr 16 '26 20:04

faskiat


1 Answers

You reset dirs and spks at every iteration, so basically it start a fresh new list every time your loop runs. Getting the dirs and spks declaration outside the loop should do the trick.

like image 103
Murmur Avatar answered Apr 18 '26 08:04

Murmur