AttributeError: 'str' object has no attribute 'duration' - python

I am trying to edit my video using moviepy. And when I want to cut a part of it I get error:
AttributeError: 'str' object has no attribute 'duration'
Why ?
from moviepy.editor import *
clip0 = VideoFileClip('08.mkv')
clip0 = clip0.set_audio(f'../Rus_sound/08.mkv'[:-3] + 'mp3')
end = 0
start = 0
lista = [0.4,0.6]
movie1 = '08.mkv'
movie2 = '../Bubble_Background_Video3.mp4'
clip0 = VideoFileClip(movie1)
audio = f'../Rus_sound/{movie1}'[:-3] + 'mp3'
clip1 = clip0.set_audio(audio)
w = clip1.w
h = clip1.h
fps = clip1.fps
clip2 = VideoFileClip(movie2).resize(height=h, width=w).set_fps(fps)
durata = clip1.duration - end
lista = [start] + [i*durata for i in lista ] + [durata]
stocked = []
for i in range(1, len(lista)):
o = i-1
clip = clip1.subclip(lista[o], lista[i])
stocked.append(clip)
if i != len(lista)-1:
stocked.append(clip2)
clip = concatenate_videoclips(stocked, method='compose')
This is my Error traceback:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-11-42faa818ba3e> in <module>
----> 1 clip = clip1.subclip(0, 449.241)
<decorator-gen-152> in subclip(self, t_start, t_end)
~/Anaconda3/lib/python3.8/site-packages/moviepy/decorators.py in wrapper(f, *a, **kw)
87 new_kw = {k: fun(v) if k in varnames else v
88 for (k,v) in kw.items()}
---> 89 return f(*new_a, **new_kw)
90 return decorator.decorator(wrapper)
91
<decorator-gen-151> in subclip(self, t_start, t_end)
~/Anaconda3/lib/python3.8/site-packages/moviepy/decorators.py in apply_to_mask(f, clip, *a, **k)
27 the clip created with f """
28
---> 29 newclip = f(clip, *a, **k)
30 if getattr(newclip, 'mask', None):
31 newclip.mask = f(newclip.mask, *a, **k)
<decorator-gen-150> in subclip(self, t_start, t_end)
~/Anaconda3/lib/python3.8/site-packages/moviepy/decorators.py in apply_to_audio(f, clip, *a, **k)
41 newclip = f(clip, *a, **k)
42 if getattr(newclip, 'audio', None):
---> 43 newclip.audio = f(newclip.audio, *a, **k)
44 return newclip
45
~/Anaconda3/lib/python3.8/site-packages/moviepy/Clip.py in subclip(self, t_start, t_end)
382 t_start = self.duration + t_start # Remember t_start is negative
383
--> 384 if (self.duration is not None) and (t_start > self.duration):
385 raise ValueError("t_start (%.02f) " % t_start +
386 "should be smaller than the clip's " +
AttributeError: 'str' object has no attribute 'duration'

The PROBLEM here, which would have been clear if you read the documentation, is that the set_audio function does not take a string. It takes an AudioFileClip object. moviepy is smart enough not to do the actual work at that point; it just remembers what you wanted for audio. Later, when you try to use that clip, it tries to look up the audio file's duration, and it finds a string where it expected an object.
clip1 = clip0.set_audio(AudioFileClip(audio))

Related

Error - ushort format requires 0 <= number <= 0xffff

i am trying to break apart a wav file into smaller groups, mess around with it , and try to put it together again, but right at the end i get the error in the title, i was hoping that someone versed in python and numpy could explain to me what am i doing wrong, why am i getting this error.
Thank you very much
path = 'C:/Users/example/Documents/folder/'
Fs, xSom = wavfile.read('sample.wav')
xSom_ = xSom[:Fs*2]
#print(np.array_split(xSom_,37))
for i in range(37):
hrtf100 = np.fromfile(path + 'beep' + str(i*10).zfill(3) + 'boop.dat', dtype='short')
hrtf100.dtype
hrtf100.shape
hrtf100_L = hrtf100[0::2]
hrtf100_R = hrtf100[1::2]
hrtf100_R_F = hrtf100_R.astype(np.float64)
hrtf100_L_F = hrtf100_L.astype(np.float64)
hrtf100_R_F_N = hrtf100_R_F/np.abs(np.max(hrtf100_R_F))
hrtf100_L_F_N = hrtf100_L_F/np.abs(np.max(hrtf100_L_F))
xSom_split = np.array_split(xSom_,37)[i]
xSom_split.dtype
xSom_F = xSom_split.astype(np.float64)
xSom_F.dtype
xSom_F_N = xSom_F / np.abs(np.max(xSom_F))
out_L = np.convolve(hrtf100_L_F_N, xSom_F_N)
out_R = np.convolve(hrtf100_R_F_N, xSom_F_N)
out_L = out_L / np.abs(np.max(out_L))*(2**15-1)
out_R = out_R / np.abs(np.max(out_R))*(2**15-1)
out = np.zeros((len(out_L),2))
out[:,0] = out_L
out[:,1] = out_R
if i>0:
full_signal = np.concatenate((full_signal,out))
else:
full_signal = out
display(Audio(data = full_signal.astype(np.int16), rate= Fs))
Error
~\AppData\Local\Temp\ipykernel_23744\379706904.py in <module>
----> 1 BinauralRotativo()
~\AppData\Local\Temp\ipykernel_23744\2326583148.py in BinauralRotativo()
32 full_signal = out
33
---> 34 display(Audio(data = full_signal.astype(np.int16), rate= Fs))
c:\users\example\appdata\local\programs\python\python37\lib\site-packages\IPython\lib\display.py in __init__(self, data, filename, url, embed, rate, autoplay, normalize, element_id)
115 if rate is None:
116 raise ValueError("rate must be specified when data is a numpy array or list of audio samples.")
--> 117 self.data = Audio._make_wav(data, rate, normalize)
118
119 def reload(self):
c:\users\example\appdata\local\programs\python\python37\lib\site-packages\IPython\lib\display.py in _make_wav(data, rate, normalize)
147 waveobj.setsampwidth(2)
148 waveobj.setcomptype('NONE','NONE')
--> 149 waveobj.writeframes(scaled)
150 val = fp.getvalue()
151 waveobj.close()
c:\users\example\appdata\local\programs\python\python37\lib\wave.py in writeframes(self, data)
436
437 def writeframes(self, data):
--> 438 self.writeframesraw(data)
439 if self._datalength != self._datawritten:
440 self._patchheader()
c:\users\example\appdata\local\programs\python\python37\lib\wave.py in writeframesraw(self, data)
425 if not isinstance(data, (bytes, bytearray)):
426 data = memoryview(data).cast('B')
--> 427 self._ensure_header_written(len(data))
428 nframes = len(data) // (self._sampwidth * self._nchannels)
429 if self._convert:
c:\users\example\appdata\local\programs\python\python37\lib\wave.py in _ensure_header_written(self, datasize)
466 if not self._framerate:
467 raise Error('sampling rate not specified')
--> 468 self._write_header(datasize)
469
470 def _write_header(self, initlength):
c:\users\example\appdata\local\programs\python\python37\lib\wave.py in _write_header(self, initlength)
483 self._nchannels * self._framerate * self._sampwidth,
484 self._nchannels * self._sampwidth,
--> 485 self._sampwidth * 8, b'data'))
486 if self._form_length_pos is not None:
487 self._data_length_pos = self._file.tell()
error: ushort format requires 0 <= number <= 0xffff

PyMC3 Minibatch ADVI

I’m trying to use PyMC3 Minibatch ADVI for Bayesian Regression. The pm.fit function throws the following error and I’m not sure how to fix it.
It says that the ‘str’ object has no attribute ‘type’. What is any ‘str’ object from the error message here? I’ve mapped float tensors for more_replacements to the best of what I know.
advi = pm.ADVI()
tracker = pm.callbacks.Tracker(mean=advi.approx.mean.eval,std=advi.approx.std.eval)
map_tensor_batch = {'x_tensor': pm.Minibatch(X_train, dtype=float),'y_tensor':pm.Minibatch(y_train['target'],dtype=float)}
approx = advi.fit(20000, obj_optimizer=pm.sgd(learning_rate=0.01), callbacks=[tracker], more_replacements = map_tensor_batch)
Your answers will be appreciated.
Addendum: If I just say
pm.Minibatch(np.array([tuple(y.iloc[i,[0]]) for i in train_index])).type()
(Or)
map_tensor_batch['y_tensor'].type()
I get the following result:
<TensorType(float64, matrix)>
Then why does it throw the attribute error below? Again, what’s my ‘str’ object?
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-24-2824def803dc> in <module>
56 tracker = pm.callbacks.Tracker(mean=advi.approx.mean.eval,std=advi.approx.std.eval)
57 map_tensor_batch = {'x_tensor': pm.Minibatch(X_train, dtype=float),'y_tensor': pm.Minibatch(y_train['target'],dtype=float)}
---> 58 approx = advi.fit(20000, obj_optimizer=pm.sgd(learning_rate=0.01), callbacks=[tracker], more_replacements = map_tensor_batch)
59 fig = plt.figure(figsize=(16, 9))
60 mu_ax = fig.add_subplot(221)
/opt/conda/lib/python3.7/site-packages/pymc3/variational/inference.py in fit(self, n, score, callbacks, progressbar, **kwargs)
142 callbacks = []
143 score = self._maybe_score(score)
--> 144 step_func = self.objective.step_function(score=score, **kwargs)
145 if progressbar:
146 progress = progress_bar(range(n), display=progressbar)
/opt/conda/lib/python3.7/site-packages/theano/configparser.py in res(*args, **kwargs)
46 def res(*args, **kwargs):
47 with self:
---> 48 return f(*args, **kwargs)
49
50 return res
/opt/conda/lib/python3.7/site-packages/pymc3/variational/opvi.py in step_function(self, obj_n_mc, tf_n_mc, obj_optimizer, test_optimizer, more_obj_params, more_tf_params, more_updates, more_replacements, total_grad_norm_constraint, score, fn_kwargs)
358 more_updates=more_updates,
359 more_replacements=more_replacements,
--> 360 total_grad_norm_constraint=total_grad_norm_constraint,
361 )
362 if score:
/opt/conda/lib/python3.7/site-packages/pymc3/variational/opvi.py in updates(self, obj_n_mc, tf_n_mc, obj_optimizer, test_optimizer, more_obj_params, more_tf_params, more_updates, more_replacements, total_grad_norm_constraint)
244 more_obj_params=more_obj_params,
245 more_replacements=more_replacements,
--> 246 total_grad_norm_constraint=total_grad_norm_constraint,
247 )
248 resulting_updates.update(more_updates)
/opt/conda/lib/python3.7/site-packages/pymc3/variational/opvi.py in add_obj_updates(self, updates, obj_n_mc, obj_optimizer, more_obj_params, more_replacements, total_grad_norm_constraint)
284 more_replacements = dict()
285 obj_target = self(
--> 286 obj_n_mc, more_obj_params=more_obj_params, more_replacements=more_replacements
287 )
288 grads = pm.updates.get_or_compute_grads(obj_target, self.obj_params + more_obj_params)
/opt/conda/lib/python3.7/site-packages/theano/configparser.py in res(*args, **kwargs)
46 def res(*args, **kwargs):
47 with self:
---> 48 return f(*args, **kwargs)
49
50 return res
/opt/conda/lib/python3.7/site-packages/pymc3/variational/opvi.py in __call__(self, nmc, **kwargs)
401 m = 1.0
402 a = self.op.apply(self.tf)
--> 403 a = self.approx.set_size_and_deterministic(a, nmc, 0, kwargs.get("more_replacements"))
404 return m * self.op.T(a)
405
/opt/conda/lib/python3.7/site-packages/theano/configparser.py in res(*args, **kwargs)
46 def res(*args, **kwargs):
47 with self:
---> 48 return f(*args, **kwargs)
49
50 return res
/opt/conda/lib/python3.7/site-packages/pymc3/variational/opvi.py in set_size_and_deterministic(self, node, s, d, more_replacements)
1496 _node = node
1497 optimizations = self.get_optimization_replacements(s, d)
-> 1498 flat2rand = self.make_size_and_deterministic_replacements(s, d, more_replacements)
1499 node = theano.clone(node, optimizations)
1500 node = theano.clone(node, flat2rand)
/opt/conda/lib/python3.7/site-packages/pymc3/variational/opvi.py in make_size_and_deterministic_replacements(self, s, d, more_replacements)
1470 flat2rand = collections.OrderedDict()
1471 for g in self.groups:
-> 1472 flat2rand.update(g.make_size_and_deterministic_replacements(s, d, more_replacements))
1473 flat2rand.update(more_replacements)
1474 return flat2rand
/opt/conda/lib/python3.7/site-packages/pymc3/variational/opvi.py in make_size_and_deterministic_replacements(self, s, d, more_replacements)
1186 initial = tt.patternbroadcast(initial, self.symbolic_initial.broadcastable)
1187 if more_replacements:
-> 1188 initial = theano.clone(initial, more_replacements)
1189 return {self.symbolic_initial: initial}
1190
/opt/conda/lib/python3.7/site-packages/theano/scan/utils.py in clone(output, replace, strict, share_inputs)
212 )
213 )
--> 214 tmp_replace = [(x, x.type()) for x, y in items]
215 new_replace = [(x, y) for ((_, x), (_, y)) in zip(tmp_replace, items)]
216 _, _outs, _ = rebuild_collect_shared(
/opt/conda/lib/python3.7/site-packages/theano/scan/utils.py in <listcomp>(.0)
212 )
213 )
--> 214 tmp_replace = [(x, x.type()) for x, y in items]
215 new_replace = [(x, y) for ((_, x), (_, y)) in zip(tmp_replace, items)]
216 _, _outs, _ = rebuild_collect_shared(
AttributeError: 'str' object has no attribute 'type'
A workaround seems to be to use a tuple instead of a dictionary. The following doesn’t throw the error as before.
advi = pm.ADVI()
tracker = pm.callbacks.Tracker(mean=advi.approx.mean.eval,std=advi.approx.std.eval)
#map_tensor_batch = {'x_tensor': pm.Minibatch(x_tensor.eval()),'y_tensor': pm.Minibatch(y_tensor.eval())}
map_tensor_batch = (pm.Minibatch(x_tensor.eval()),pm.Minibatch(y_tensor.eval()))
approx = advi.fit(20000, obj_optimizer=pm.sgd(learning_rate=0.00001), callbacks=[tracker], more_replacements = map_tensor_batch)
But there’s another error as below:
TypeError: TensorType does not support iteration. Maybe you are using builtins.sum instead of theano.tensor.sum? (Maybe .max?)
Any fix to this?
This (https://alexioannides.com/2018/11/07/bayesian-regression-in-pymc3-using-mcmc-variational-inference/) is the example I’m trying to follow.
The blog post you are working from shows
import theano
y_tensor = theano.shared(train.y.values.astype('float64'))
x_tensor = theano.shared(train.x.values.astype('float64'))
map_tensor_batch = {y_tensor: pm.Minibatch(train.y.values, 100),
x_tensor: pm.Minibatch(train.x.values, 100)}
That is, map_tensor_batch should be a dict, but the keys are Theano tensors, not mere strings.

Dask getting "FileNotFoundError: [Errno 2] No such file or directory" in the middle of a file

I'm making a bag from a plain txt file - it's got a bunch of reviews, delimited by two newlines. But, sometimes - and I really can't predict when - it gives me FileNotFoundError: [Errno 2] No such file or directory: '/mnt/c/Workspaces/Books/Dask/foods.txt' while processing it
Here's the actual code
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
import dask.dataframe as dd
from dask.diagnostics import ProgressBar
import numpy as np
import dask.bag as bag
import os
def get_next_part(file, start_index, span_index=0, blocksize=1000):
file.seek(start_index)
buffer = file.read(blocksize + span_index).decode('cp1252')
delimiter_position = buffer.find('\n\n')
if delimiter_position == -1:
return get_next_part(file, start_index, span_index + blocksize)
else:
file.seek(start_index)
return start_index, delimiter_position
def get_item(filename, start_index, delimiter_position, encoding='cp1252'):
with open(filename, 'rb') as file_handle:
file_handle.seek(start_index)
text = file_handle.read(delimiter_position).decode(encoding)
return dict((element.split(': ')[0], element.split(': ')[1])
if len(element.split(': ')) > 1
else ('unknown', element)
for element in text.strip().split('\n'))
with open(f"{os.getcwd()}/foods.txt", 'rb') as file_handle:
size = file_handle.seek(0,2) - 1
more_data = True
output = []
current_position = next_position = 0
while more_data:
if current_position >= size:
more_data = False
else:
current_position, next_position = get_next_part(file_handle, current_position, 0)
output.append((current_position, next_position))
current_position = current_position + next_position + 2
with ProgressBar():
reviews = (bag.from_sequence(output, npartitions=104)
.map(lambda x: get_item(f"{os.getcwd()}/foods.txt",
x[0],
x[1]))
.compute())
Sometimes it works fine, but other times it gives me something along these lines (different percentage every time):
[########## ] | 26% Completed | 54.3s
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-1-90a316620d10> in <module>()
42 with ProgressBar():
43 reviews = (bag.from_sequence(output, npartitions=104)
---> 44 .map(lambda x: get_item(f"{os.getcwd()}/foods.txt",
45 x[0],
46 x[1]))
~/anaconda3/envs/py36/lib/python3.6/site-packages/dask/base.py in compute(self, **kwargs)
154 dask.base.compute
155 """
--> 156 (result,) = compute(self, traverse=False, **kwargs)
157 return result
158
~/anaconda3/envs/py36/lib/python3.6/site-packages/dask/base.py in compute(*args, **kwargs)
396 keys = [x.__dask_keys__() for x in collections]
397 postcomputes = [x.__dask_postcompute__() for x in collections]
--> 398 results = schedule(dsk, keys, **kwargs)
399 return repack([f(r, *a) for r, (f, a) in zip(results, postcomputes)])
400
~/anaconda3/envs/py36/lib/python3.6/site-packages/dask/multiprocessing.py in get(dsk, keys, num_workers, func_loads, func_dumps, optimize_graph, pool, **kwargs)
190 get_id=_process_get_id, dumps=dumps, loads=loads,
191 pack_exception=pack_exception,
--> 192 raise_exception=reraise, **kwargs)
193 finally:
194 if cleanup:
~/anaconda3/envs/py36/lib/python3.6/site-packages/dask/local.py in get_async(apply_async, num_workers, dsk, result, cache, get_id, rerun_exceptions_locally, pack_exception, raise_exception, callbacks, dumps, loads, **kwargs)
460 _execute_task(task, data) # Re-execute locally
461 else:
--> 462 raise_exception(exc, tb)
463 res, worker_id = loads(res_info)
464 state['cache'][key] = res
~/anaconda3/envs/py36/lib/python3.6/site-packages/dask/compatibility.py in reraise(exc, tb)
109 def reraise(exc, tb=None):
110 if exc.__traceback__ is not tb:
--> 111 raise exc.with_traceback(tb)
112 raise exc
113
~/anaconda3/envs/py36/lib/python3.6/site-packages/dask/local.py in execute_task()
228 try:
229 task, data = loads(task_info)
--> 230 result = _execute_task(task, data)
231 id = get_id()
232 result = dumps((result, id))
~/anaconda3/envs/py36/lib/python3.6/site-packages/dask/core.py in _execute_task()
117 func, args = arg[0], arg[1:]
118 args2 = [_execute_task(a, cache) for a in args]
--> 119 return func(*args2)
120 elif not ishashable(arg):
121 return arg
~/anaconda3/envs/py36/lib/python3.6/site-packages/dask/bag/core.py in reify()
1589 def reify(seq):
1590 if isinstance(seq, Iterator):
-> 1591 seq = list(seq)
1592 if seq and isinstance(seq[0], Iterator):
1593 seq = list(map(list, seq))
~/anaconda3/envs/py36/lib/python3.6/site-packages/dask/bag/core.py in map_chunk()
1749 else:
1750 for a in zip(*args):
-> 1751 yield f(*a)
1752
1753 # Check that all iterators are fully exhausted
<ipython-input-1-90a316620d10> in <lambda>()
44 .map(lambda x: get_item(f"{os.getcwd()}/foods.txt",
45 x[0],
---> 46 x[1]))
47 .compute())
<ipython-input-1-90a316620d10> in get_item()
18
19 def get_item(filename, start_index, delimiter_position, encoding='cp1252'):
---> 20 with open(filename, 'rb') as file_handle:
21 file_handle.seek(start_index)
22 text = file_handle.read(delimiter_position).decode(encoding)
FileNotFoundError: [Errno 2] No such file or directory: '/mnt/c/Workspaces/Books/Dask/foods.txt'
I've tried messing with the partition numbers - leaving it as default (101), or making sure it's a multiple of 4. Doesn't seem to have an effect.
Anyone know what's going on here? It usually works if I run it a second time, but that's still tough to deal with.
I'm using the latest version of Dask. Using conda, it's all in Jupyterlab, and I'm running it from Windows Subsystem for Linux
Thanks!
Wasn't able to fix my initial read method, but was able to find another way of doing the parallel read (with native Dask objects too!)
Sections were delimited with \n\n and the linedelimiter argument to bag didn't mean what I thought it meant, but with this I was able to figure a way to get the sections I needed: Why `linedelimiter` does not work for bag.read_text?
bag.read_text(
f"{os.getcwd()}/foods.txt",
encoding="cp1252",
blocksize="10MB",
linedelimiter="\n\n",
)
.map_partitions(lambda x: "".join(x).split("\n\n"))

'method-wrapper' object is not iterable in AES CTR pycrypto library

I'm trying to implement an encryption/decryption function in python2.
This is the encryption scheme:
However, I'm getting a 'method-wrapper' object is not iterable error in the AES CTR function of the pycrypto library
This is the stacktrace:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-5-1c44f21fcf83> in <module>()
45 l = random.randint(16,48)
46 m = random_bytes(l)
---> 47 assert specialDecryption(k, specialEncryption(k, m)) == m
<ipython-input-5-1c44f21fcf83> in specialEncryption(k, m)
7 # compute PRF
8 r = random_bytes(KEYLENGTH/8)
----> 9 prf = lengthQuadruplingPRF(k, r)
10
11 # xor
<ipython-input-4-59fb6141461b> in lengthQuadruplingPRF(k, r)
34 assert len(k) == KEYLENGTH/8
35 assert len(r) <= KEYLENGTH/8
---> 36 obj = AES.new(k, AES.MODE_CTR, counter=make_counter())
37 output = obj.encrypt(r*4)
38 return output
/usr/local/lib/python2.7/site-packages/Crypto/Cipher/AES.pyc in new(key, mode, *args, **kwargs)
204
205 kwargs["add_aes_modes"] = True
--> 206 return _create_cipher(sys.modules[__name__], key, mode, *args, **kwargs)
207
208
/usr/local/lib/python2.7/site-packages/Crypto/Cipher/__init__.pyc in _create_cipher(factory, key, mode, *args, **kwargs)
77 raise TypeError("IV is not meaningful for the ECB mode")
78
---> 79 return modes[mode](factory, **kwargs)
/usr/local/lib/python2.7/site-packages/Crypto/Cipher/_mode_ctr.pyc in _create_ctr_cipher(factory, **kwargs)
323 # 'counter' used to be a callable object, but now it is
324 # just a dictionary for backward compatibility.
--> 325 _counter = dict(counter)
326 try:
327 counter_len = _counter.pop("counter_len")
TypeError: 'method-wrapper' object is not iterable
Here's the code:
if __name__ == '__main__':
k = os.urandom(KEYLENGTH/8) # generate key
l = random.randint(16,48)
m = os.urandom(l)
c = specialEncryption(k, m) ## FIRST IN THE FAILURE STACK
def specialEncryption(k, m):
... other code
# compute PRF
r = os.urandom(KEYLENGTH/8)
prf = lengthQuadruplingPRF(k, r) ## SECOND IN THE FAIL STACK
... other code
def make_counter():
import struct
def gen():
i = 0;
while True:
yield struct.pack('>QQ', 0, i)
i += 1
return gen().next
def lengthQuadruplingPRF(k, r):
# Input: 16 byte key, 16 byte value
# Output: 64 byte pseudorandom bytes
obj = AES.new(k, AES.MODE_CTR, counter=make_counter()) ## FAILS HERE
output = obj.encrypt(r*4)
return output
Your counter should be an iterable capable of initializing a dict, not a bound method.
I suspect changing your make_counter function to:
return gen()
from:
return gen().next
is enough to fix it.

Errno 2 when filtering data by a specific value

I am not sure why I am getting this error. Basically I am organizing the data and then making correlation graphs on different set of variables.
The data is being filtered by the "estado" column values. My functions work correctly with all of the different values but with "PR/Horra". This is when I get the " No such file or directory: 'PR/Horra1.pdf' " error. I am not sure of why is this because all of the other values work correctly using the same functions.
Data:
Animal peso estado ult. peso edad incre
actual fecha anterior (meses) peso
kg peso
SAN-13-09 510 PR/Horra 1/31/2017 500 47 10
SAN-13-51 507 Palp- 1/31/2017 500 42 7
SAN-14-10 366 PR 1/31/2017 359 34 7
SAN-14-21 462 Palp- 1/31/2017 451 33 11
SAN-14-26 310 Palp- 1/31/2017 307 32 3
SAN-14-27 300 Palp- 1/31/2017 293 30 7
SAN-14-33 380 Palp- 1/31/2017 374 29 6
SAN-14-37 377 Palp- 1/31/2017 372 29 5
SAN-15-14 365 Nc 1/31/2017 351 22 14
SAN-15-15 341 Nc 1/31/2017 333 22 8
SAN-15-58 297 Ser 1/31/2017 277 16 20
Code:
# make imports
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
import xlrd
import pylab
from PyPDF2 import PdfFileMerger
# define a correlation
def pearsonr(x, y):
# Assume len(x) == len(y)
n = len(x)
sum_x = float(sum(x))
sum_y = float(sum(y))
sum_x_sq = sum(map(lambda x: pow(x, 2), x))
sum_y_sq = sum(map(lambda x: pow(x, 2), y))
psum = sum(map(lambda x, y: x * y, x, y))
num = psum - (float(sum_x) * float(sum_y/n))
den = pow((sum_x_sq - pow(sum_x, 2) / n) * (sum_y_sq - pow(sum_y, 2) / n), 0.5)
if den == 0: return 0
return num / den
# define a correlation graph
def plot(x, y, num):
matplotlib.rcParams['axes.unicode_minus'] = False
fig, ax = plt.subplots()
corr = pearsonr(x,y)
name = x.name + ' vs ' + y.name + ' - Pearson: ' + str(round(corr ,3))
ax.set_title(name)
ax.set_xlabel(x.name)
ax.set_ylabel(y.name)
ax.plot(x,y, 'o')
pylab.savefig(num + '.pdf')
def groups(a, b, c):
x = a[[b , c]]
return x
def openData(a):
# open data and clean columns
data = pd.read_excel(a, header = 2 )
data = data.set_index('Animal')
return data
def organizeData(data, filters):
#filter data
if filters != 'None':
data = data[data['Estado actual'] == filters]
# organize data
group1 = groups(data, 'Peso actual kg', 'peso anterior' )
group2 = groups(data, 'Peso actual kg', 'Edad (Meses)' )
group3 = groups(data, 'Peso actual kg', 'incre peso' )
group4 = groups(data, 'peso anterior', 'Edad (Meses)' )
group5 = groups(data, 'peso anterior', 'incre peso' )
group6 = groups(data, 'Edad (Meses)', 'incre peso' )
return group1, group2, group3, group4, group5, group6
def createG(b,a):
plotss = plot(b.iloc[:,0], b.iloc[:,1],a)
return plotss
def name_of_graph (filters,num):
name = filters + str(num)
return name
def all(filters, data_file):
a,b,c,d,e,f = organizeData(data_file, filters)
# create graphs and save pdfs
plot1 = createG(a, name_of_graph(filters,1))
plot2 = createG(b, name_of_graph(filters,2))
plot3 = createG(c, name_of_graph(filters,3))
plot4 = createG(d, name_of_graph(filters,4))
plot5 = createG(e, name_of_graph(filters,5))
plot6 = createG(f, name_of_graph(filters,6))
return plot1, plot2, plot3, plot4, plot5, plot6
data_file = openData('/Users/username/works/dtest/data/data.xls')
all("None", data_file)
all("PR/Horra", data_file)
all("Palp-", data_file)
all("PR", data_file)
all("Nc", data_file)
all("Ser", data_file)
Error:
[Errno 2] No such file or directory: 'PR/Horra1.pdf'
Expanded error:
FileNotFoundError Traceback (most recent call last)
<ipython-input-10-2f38564bb9d3> in <module>()
1 all("None", data_file)
----> 2 all("PR/Horra", data_file)
3 all("Palp-", data_file)
4 all("PR", data_file)
5 all("Nc", data_file)
<ipython-input-6-d0d4157a5102> in all(filters, data_file)
59 a,b,c,d,e,f = organizeData(data_file, filters)
60 # create graphs and save pdfs
---> 61 plot1 = createG(a, name_of_graph(filters,1))
62 plot2 = createG(b, name_of_graph(filters,2))
63 plot3 = createG(c, name_of_graph(filters,3))
<ipython-input-6-d0d4157a5102> in createG(b, a)
49
50 def createG(b,a):
---> 51 plotss = plot(b.iloc[:,0], b.iloc[:,1],a)
52 return plotss
53
<ipython-input-6-d0d4157a5102> in plot(x, y, num)
23 ax.set_ylabel(y.name)
24 ax.plot(x,y, 'o')
---> 25 pylab.savefig(num + '.pdf')
26
27 def groups(a, b, c):
/Users/user/envs/dtest/lib/python3.6/site-packages/matplotlib/pyplot.py in savefig(*args, **kwargs)
695 def savefig(*args, **kwargs):
696 fig = gcf()
--> 697 res = fig.savefig(*args, **kwargs)
698 fig.canvas.draw_idle() # need this if 'transparent=True' to reset colors
699 return res
/Users/user/envs/dtest/lib/python3.6/site-packages/matplotlib/figure.py in savefig(self, *args, **kwargs)
1571 self.set_frameon(frameon)
1572
-> 1573 self.canvas.print_figure(*args, **kwargs)
1574
1575 if frameon:
/Users/user/envs/dtest/lib/python3.6/site-packages/matplotlib/backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, **kwargs)
2250 orientation=orientation,
2251 bbox_inches_restore=_bbox_inches_restore,
-> 2252 **kwargs)
2253 finally:
2254 if bbox_inches and restore_bbox:
/Users/user/envs/dtest/lib/python3.6/site-packages/matplotlib/backends/backend_pdf.py in print_pdf(self, filename, **kwargs)
2517 file = filename._file
2518 else:
-> 2519 file = PdfFile(filename)
2520 try:
2521 file.newPage(width, height)
/Users/user/envs/dtest/lib/python3.6/site-packages/matplotlib/backends/backend_pdf.py in __init__(self, filename)
420 self.tell_base = 0
421 if is_string_like(filename):
--> 422 fh = open(filename, 'wb')
423 elif is_writable_file_like(filename):
424 try:
FileNotFoundError: [Errno 2] No such file or directory: 'PR/Horra1.pdf'
Easy fix. Thanks to Achampion comment I learned that the problem was that '/' is not allowed as part of filename.
Allowed characters in filename
Note: I also changed the name of the all function.
Thank you

Categories