Python 2.7.8 Too Few Args or No such file\directory

93 views Asked by At

New to Python so assume I'm ignorant for the most part. I've spent several hours searching but I haven't been able to implement a fix to the file written by my peer. Let's say I'm embarrassed that I can't figure this out on my own and don't want to ask for their help. So I'm hoping that they won't see this. Here is the code to run in terminal for plotting the data in a cvs file.

import argparse
import matplotlib.pyplot as plt
import numpy as np
import scipy
import scipy.fftpack

def read_data(filename, period):
    """
Parse input filename, returning a dict with
accel and gyro x, y, and z

Period of samples given in ms
"""
# Period to seconds
period = period/1000.

with open(filename, "rb") as f:
    data = np.loadtxt(f,delimiter=",",skiprows=1)

ret = {'accel': {}, 'gyro': {}}

ret['time'] = np.arange(0, data.shape[0]*period, period)
ret['accel']['x'] = data[:,0]
ret['accel']['y'] = data[:,1]
ret['accel']['z'] = data[:,2]
ret['gyro']['x'] = data[:,3]
ret['gyro']['y'] = data[:,4]
ret['gyro']['z'] = data[:,5]

return ret

def plot_data(data):
"""
Plot data, given in form returned by read_data

plt.show() must be called to display the plots.
"""

plt.figure()
plt.plot(data['time'], data['accel']['x'])
plt.title('X acceleration')
plt.xlabel('Time (s)')
plt.ylabel('Acceleration (g)')

plt.figure()
plt.plot(data['time'], data['accel']['y'])
plt.title('Y acceleration')
plt.xlabel('Time (s)')
plt.ylabel('Acceleration (g)')

plt.figure()
plt.plot(data['time'], data['accel']['z'])
plt.title('Z acceleration')
plt.xlabel('Time (s)')
plt.ylabel('Acceleration (g)')

plt.figure()
plt.plot(data['time'], data['gyro']['x'])
plt.title('X rotational rate')
plt.xlabel('Time (s)')
plt.ylabel('Rotational rate (deg/s)')

plt.figure()
plt.plot(data['time'], data['gyro']['y'])
plt.title('Y rotational rate')
plt.xlabel('Time (s)')
plt.ylabel('Rotational rate (deg/s)')

plt.figure()
plt.plot(data['time'], data['gyro']['z'])
plt.title('Z rotational rate')
plt.xlabel('Time (s)')
plt.ylabel('Rotational rate (deg/s)')

def compute_fft(data, period):
"""
Compute fft of all components of data, given in form returned from
read_data().

Sample period given in ms.
"""
# Period to seconds
period = period/1000.

fft = {'accel': {}, 'gyro': {}}

fft['freq'] = scipy.fftpack.fftfreq(data['time'].shape[0], d=period)

fft['accel']['x'] = scipy.fftpack.fft(data['accel']['x'])
fft['accel']['y'] = scipy.fftpack.fft(data['accel']['y'])
fft['accel']['z'] = scipy.fftpack.fft(data['accel']['z'])
fft['gyro']['x'] = scipy.fftpack.fft(data['gyro']['x'])
fft['gyro']['y'] = scipy.fftpack.fft(data['gyro']['y'])
fft['gyro']['z'] = scipy.fftpack.fft(data['gyro']['z'])

return fft

def plot_fft(fft):
"""
Plot fft data, given in the form returned from compute_fft().
"""
plt.figure()
plt.plot(fft['freq'], 20*scipy.log10(fft['accel']['x']), 'x')
plt.title('X acceleration FFT')
plt.xlabel('Frequency (Hz)')
plt.ylabel('Magnitude (dB)')

plt.figure()
plt.plot(fft['freq'], 20*scipy.log10(fft['accel']['y']), 'x')
plt.title('Y acceleration FFT')
plt.xlabel('Frequency (Hz)')
plt.ylabel('Magnitude (dB)')

plt.figure()
plt.plot(fft['freq'], 20*scipy.log10(fft['accel']['z']), 'x')
plt.title('Z acceleration FFT')
plt.xlabel('Frequency (Hz)')
plt.ylabel('Magnitude (dB)')

plt.figure()
plt.plot(fft['freq'], 20*scipy.log10(fft['gyro']['x']), 'x')
plt.title('X rotational rate FFT')
plt.xlabel('Frequency (Hz)')
plt.ylabel('Magnitude (dB)')

plt.figure()
plt.plot(fft['freq'], 20*scipy.log10(fft['gyro']['y']), 'x')
plt.title('Y rotational rate FFT')
plt.xlabel('Frequency (Hz)')
plt.ylabel('Magnitude (dB)')

plt.figure()
plt.plot(fft['freq'], 20*scipy.log10(fft['gyro']['z']), 'x')
plt.title('Z rotational rate FFT')
plt.xlabel('Frequency (Hz)')
plt.ylabel('Magnitude (dB)')

if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Plot accel and gyro data')
parser.add_argument('file', help='CSV file with data')
parser.add_argument('period', type=float, help='Sample period in ms')
args = parser.parse_args()

data = read_data(args.file, args.period)

fft = compute_fft(data, args.period)

print "Max accel x freq: %f" % (fft['freq'][fft['accel']['x'].argmax()])
print "Max accel y freq: %f" % (fft['freq'][fft['accel']['y'].argmax()])
print "Max accel z freq: %f" % (fft['freq'][fft['accel']['z'].argmax()])
print "Max gyro x freq: %f" % (fft['freq'][fft['gyro']['x'].argmax()])
print "Max gyro y freq: %f" % (fft['freq'][fft['gyro']['y'].argmax()])
print "Max gyro z freq: %f" % (fft['freq'][fft['gyro']['z'].argmax()])

plot_data(data)
plot_fft(fft)

plt.show()

Here is the command used in the terminal window and the error.

C:\Users\Documents>C:\Python27\python.exe fft.py shake_1ms.cvs, 1
Traceback (most recent call last):
File "fft.py", line 144, in <module>
data = read_data(args.file, args.period)
File "fft.py", line 17, in read_data
with open(filename, "rb") as f:
IOError: [Errno 2] No such file or directory: 'shake_1ms.cvs,'

I am just a beginner with Python and look forward to all help that can be suggested.

2

There are 2 answers

1
grovesNL On BEST ANSWER

The exception is stating that it's not able to find shake_1ms.cvs in the directory C:\Users\Documents. You may have meant to type shake_1ms.csv for a comma-separated value file (notice the csv extension instead of cvs).

...unless of course you are recording accelerometer data for CVS Pharmacy...

1
vikramls On

Remove the , from your command-line like this:

C:\Users\Documents>C:\Python27\python.exe fft.py shake_1ms.cvs 1