I'm trying to extract data from blosum62 matrix
I have done the following code:
from Bio.SubsMat import MatrixInfo
blosum = MatrixInfo.blosum62
blosum['N','D']
et gets following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: ('N', 'D')
But in blosum62 matrix there is a value of ('N','D') = 1
Why is it giving KeyError ?
I think Blosum62 is a lower triangular matrix so if ('N','D') is not working then go with ('D','N')
the solution is
pair = ('N','D')
if pair not in blosum62_matrix:
value = blosum62_matrix[(tuple(reversed(pair)))]
else:
value = blosum62_matrix[pair]
I think it will work.
Accroding to http://biopython.org/DIST/docs/api/Bio.SubsMat.MatrixInfo-module.html#blosum62 blosom62 returns a dictionary. So try blosum[('N','D')].
Related
In my code I need to convert an image to a numpy array, then from an array to a list. After performing some changes to the list I need to convert back to an image but I get this error
Traceback (most recent call last):
File "/home/owner/anaconda3/envs/proj1/lib/python3.7/site-packages/PIL/Image.py", line 2714, in fromarray
mode, rawmode = _fromarray_typemap[typekey]
KeyError: ((1, 1, 3), '<i8')
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/owner/PycharmProjects/arrays3/test.py", line 31, in <module>
im = Image.fromarray(b)
File "/home/owner/anaconda3/envs/proj1/lib/python3.7/site-packages/PIL/Image.py", line 2716, in fromarray
raise TypeError("Cannot handle this data type: %s, %s" % typekey)
TypeError: Cannot handle this data type: (1, 1, 3), <i8
I know that the error is occuring due to the transition from array to list and back but I'm not sure why. Here is some code that produces the same error but makes no modifications to the contents of the image data, as the print statement returns true.
im = Image.open("wp2793461-windows-98-wallpaper-pack.jpg")
a = np.asarray(im)
lst = a.tolist()
b = np.asarray(lst)
print(np.array_equal(a, b))
im = Image.fromarray(b)
im.save("new.jpg")
Nice conundrum! I was looking at what the differences between a and b are, and found that numpy's dtype is different for both.
>>> print(a.dtype)
uint8
>>> print(b.dtype)
int64
If you create b in the following way, it works again:
b = np.asarray(lst, dtype=a.dtype)
I have built a code for a codewars problem. I think it is correct but it shows me an error I don't understand.
Can you tell me what am I doing wrong?
import math
def waterbombs(fire, w):
s=""
countx=0
for i in fire:
if i=="x":
countx+=1
elif i=="Y":
countx=0
return sum(math.ceil(countx/w))
waterbombs("xxYxx", 3)
This is the error:
Traceback (most recent call last):
File "D:\Curso Python Pildorasinformaticas\Ejercicios Codewars\Aerial Firefighting.py", line 16, in <module>
waterbombs("xxYxx", 3)
File "D:\Curso Python Pildorasinformaticas\Ejercicios Codewars\Aerial Firefighting.py", line 13, in waterbombs
return sum(math.ceil(countx/w))
TypeError: 'int' object is not iterable
[Finished in 0.2s]
Why are you doing sum(math.ceil(countx/w)) ?
What is the objective of the sum method here, since there is only value returned by math.ceil ?
The sum would throw that error if you pass a single value to it. You're supposed to pass a list of values of the sum method.
For eg: sum(5) would give you the same error you see above, but sum([5]) would return you 5.
I have this for-loop:
for i in range(1000000000, 1000000030):
foo(i)
When I execute it this error is given:
Traceback (most recent call last):
File "/CENSORED/Activity.py", line 11, in <module>
for i in range(1000000000, 10000000030):
OverflowError: range() result has too many items.
As far as I know, this range-object should have exactly 30 elements...
Where is the problem?
Edit:
I have removed the extra zero, now I get this:
Traceback (most recent call last):
File "/CENSORED/Activity.py", line 12, in <module>
factorizeInefficient(i)
MemoryError
Edit 2:
def factorizeInefficient(n):
teiler = list()
for i in range(n):
if i != 0:
if (n%i)==0:
teiler.append(i)
print teiler
Just found the solution myself: There is a range(n) object in this as well and this causes the memory Error...
An extra question: How did you guys know this was python 2? (Btw you were right...)
Copy/pasting the range() part of your code:
>>> len(range(1000000000, 10000000030))
9000000030
So there are actually about 9 billion elements in the range. Your first argument is presumably missing a zero, or second argument has a zero too many ;-)
count your zeros once again ;) I'd say it's one too much.
I have a sample json:
I want to use the json module of python and recurse through to find the "MaxSize" in "pevcwebasg". Have the following code:
import json
param_file_handle = json.load(open("sample.json"))
print param_file_handle['Resources']['pevcwebasg']['Type']
resources = param_file_handle['Resources']
for asg in resources:
print asg["Type"]
The out put of which is :
> AWS::AutoScaling::AutoScalingGroup Traceback (most recent call last):
> File "test.py", line 8, in <module>
> print asg['Type'] TypeError: string indices must be integers
What I dont get is this line "print param_file_handle['Resources']['pevcwebasg']['Type']" works fine and gets me the output but when i recurse through and try and find asg["Type"], it fails. Any better way to do this ? I need to recurse through the tree and find the value.
Edit 1:
as I do recurse through values, I get stuck with error.
param_file_handle = json.load(open("sample.json"))
resources = param_file_handle['Resources']
for asg in resources.values():
if asg["Type"] == "AWS::AutoScaling::AutoScalingGroup":
for values in asg['Properties']:
print values["MaxSize"]
error :
Traceback (most recent call last):
File "test.py", line 9, in <module>
print values["MaxSize"]
TypeError: string indices must be integers
for asg in resources:
This iterates through the keys of resources, rather than the values. Try:
for asg in resources.values():
param_file_handle = json.load(open("sample.json"))
resources = param_file_handle['Resources']
for asg in resources.values():
if asg["Type"] == "AWS::AutoScaling::AutoScalingGroup":
print asg["Properties"]["MaxSize"]
try this.
you broke hirerchy, missed "pevcwebasg"
resources = param_file_handle['Resources']['pevcwebasg']
for asg in resources:
print asg["Type"]
I am trying to find the maximum value in a scipy.sparse matrix. The docs here say there is a .max method. Oddly, however, this doesn't work for me:
>>> import scipy.sparse as sps
>>> a = sps.csr_matrix((3,3))
>>> a[0,0] = 1
>>> a.max()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/scipy/sparse/base.py", line 393, in __getattr__
raise AttributeError(attr + " not found")
AttributeError: max not found
Any idea why this might be happening?
(I am using scipy version 0.11.0)
Some archeology in the scipy docs website shows that method being introduced in scipy 13.0.
If you do not want to upgrade, you can get the maximum of the non-zero entries of your sparse matrix with a.data.max().
If you want it to be the maximum considering the zeros as well, which is what the .max() method does, do something along the lines of:
m = a.data.max()
if m < 0 and a.nnz < a.shape[0]*a.shape[1]:
m = 0