How to define a constant function defined in intervals in python? - python

I want to define a simple function which assumes different constant value (y=[1,4,2,3]) for defined intervals.
I implement it in this way:
import numpy as np
def f(x):
if (x>=0 and x<=1900):
return 1
if (x>1900 and x<=3600):
return 4
if (x>3600 and x<=5400):
return 2
if (x>5400 and x<=7200):
return 3
x=np.linspace(0,7200,1000)
y=f(x)
However, when I run the script, an error appears:
"ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()"
Do you know how to fix this?

The reason is that your function is only applicable to a single element rather than vectorization. np.vectorize is a general solution, but its performance is poor. For the example here, you can use np.searchsorted to vectorize:
>>> np.array([1, 1, 4, 2, 3])[np.searchsorted([0, 1900, 3600, 5400, 7200], x)]
array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 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 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3])

x is not what you think it is.
Try print(x) and see what it is actually looking like. It is first up a list and inside the list contains
[ 0. 7.20720721 14.41441441 21.62162162 28.82882883 .... 7192.79279279 7200. ]
I am unsure what you are trying to achieve but x is an array and not a single value, therefor you either need to loop over it or point to the exact index you want to test.

Related

Rosalind - Consensus and Profile - Issue with answer formatting

I am working on the Consensus and Profile problem on Rosalind, and I am so close to getting it done. My answer is correct, I have the right consensus string and the correct matrix, but I am having issues formatting my data for the answer. Rosalind expects the answer to look like:
ATGCAACT
A: 5 1 0 0 5 5 0 0
C: 0 0 1 4 2 0 6 1
G: 1 1 6 3 0 1 0 0
T: 1 5 0 0 0 1 1 6
My raw output looks like this:
{'A': [5, 3, 3, 3, 1, 4, 2, 1, 3, 5, 2, 2, 2, 3, 1, 3, 2, 2, 2, 4, 4, 4, 1, 2, 1, 3, 1, 2, 1, 2, 2, 3, 2, 1, 3, 5, 3, 4, 2, 2, 2, 3, 3, 2, 0, 0, 1, 2, 2, 4, 3, 5, 2, 4, 3, 1, 2, 2, 2, 3], 'C': [2, 1, 3, 2, 1, 2, 2, 1, 3, 2, 1, 2, 3, 2, 6, 3, 4, 1, 2, 0, 3, 2, 4, 2, 1, 3, 3, 3, 6, 2, 2, 1, 5, 5, 3, 0, 1, 1, 2, 3, 3, 5, 3, 2, 1, 2, 3, 5, 0, 2, 3, 2, 3, 2, 5, 3, 4, 3, 2, 4], 'G': [1, 3, 2, 4, 3, 2,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2, 3, 2, 2, 3, 3, 0, 3, 3, 4, 6, 5, 3, 6, 3, 2, 2, 1, 2, 0, 3, 2, 5, 2, 1]
But the editing still doesn't matter because of the metric f**k ton of commas and brackets that would need to be manually deleted as well, especially considering the fact that you only have 5 minutes to submit your answer - I've tried and I have found it impossible to format my answer manually in the five-minute window.
I was wondering if anyone knew of some tips, tricks, or solutions that can help me get over this hurdle. I have seen some other solutions, but they essentially require me to take a different approach to logic, which pisses me off because I spent a lot of time thinking about this answer and also creating my own function that manages the FASTA file format from scratch.
Here is my source code:
data = open('/Users/danielpintard/Downloads/rosalind_cons (1).txt', 'r').read()
if '>' in data :
data_array = data.split('>')
for i in data_array:
if i == '':
data_array.remove(i)
for i in data_array: data_array[data_array.index(i)] = i.split('\n', 2)
#create profile
prof_sequences = []
for i in data_array:
data_array[data_array.index(i)] = i[1]
prof_sequences.append(i[1])
n = len(prof_sequences[0])
profile_matrix = {
'A': [0]*n,
'C': [0]*n,
'G': [0]*n,
'T': [0]*n,
}
for dna in prof_sequences:
for position, nucleotide in enumerate(dna):
profile_matrix[nucleotide][position] += 1
result = []
#still having a hard time understanding this block of code
for position in range(n):
max_count = 0
max_nucleotide = None
for nucleotide in profile_matrix:
if profile_matrix[nucleotide][position] > max_count:
max_count = profile_matrix[nucleotide][position]
max_nucleotide = nucleotide
result.append(max_nucleotide)
print(profile_matrix)
print(result)
And here is the data:
>Rosalind_7283
TATTCATTGATCATATGAAGCCTTGCGACCTGCCCGGTTCTGAAGTCAGCTAGCACATTA
GTGTCAAGGTATTAGTGTAGTTGCTGACTCGAACGTGTGTTAATATTCATGTAGGGGTCT
GGCGACCCAATAGGCGCGTGGTGTACCGAATTGTGCACACACACGTGTATTTCGAACGCA
AGATGCAGCCGAATCAGACCGTAGTAAACCGTTTGAGTGGCGTTTTGGCGTGAGAAGGCT
TAGGTGTTACAAGTGCAGCGCGGGTGCATTTTCTCCGGCTTGGAGCAATAGTCCCTATGC
ATCGGCCCCGTATATGAGGATCGCATTACGCAACATCGTAAGCCTTGCACATCTGGCAAA
TGCACGGCTCTCATTATAGTTGCCAAAAATCAGCCCTACCACACGTAATATTCAAGGCTG
TGCTTGTCCAACTAGTTGGCGAATGATCCTCCAAGATTGCGGCGGGGTATAATCCCGCAC
GTCCGAATACCAATGTTCGAGTGCGGCACTACCAAGATGCGAGTCGGCGTGATATCGAGG
TTCACATAGGGGACGTTTATGTCCTTTGGATGTCTCGCCAACTCCATTCTATCATTAGGT
TGGCGGTCAGCAGGATGGAGCATAGTCATGAGCTTGAGTACTGTGCGGCTCGGAAGAGGG
GCCGTATGGGTCTTCGGACAAACGTAGGTATTACAGGCCAAAAACGCTCAGAAAAAACGC
TATCTTAATGACCATTTGATAAACGTTCCCTTGCCGATTTAGAGTGACTTAGTGCAATGT
TGCGATTTCTACTACACTCAAGCTGTGTTAGGGATAATCCATAGCACAGGCCCGCTCGCC
CGTGCCCTGCCTTGCACGACAGGGCTAAGCGGCTCAAGAAGTTTCTACGCAACGTACCAC
GACCAGCTGGACCTACCGATAGACTTACCATATTCTAAGAATAAAACGGACCCTTATGTG
AGTGAGCGCAAGCAATATGGTTTGCCCGTTTGC
>Rosalind_6559
TGCGGCCTATGTGACGCTCAACCCGGGACGCACATGAGTACATCTTTCTTCAACGTCCGC
GAACACAGCCCAATTCGATTAATTGCCACGATTGTGTGGCACGCACTTACTGAAACCGGT
GGTGATAGGCATAGGTTTAGACCAGGGCTCGGGACAGCTGGTCTAGGTCGTGACTAATCA
ATGGTTTAAATGATGCACCCTTGTATCGGTATGCCTGTGTTTATCAGGAATGCCCATACA
TTTTGAGAAACGCTTATGGTTATTACACAAGCGAGGGAAGCGAGCTAGCGGCGTCCGAGA
ACTATAAAGGCAATCCTGACATGACGAGCGCAGAATCACCCCCTGAATCCCGGTTACGAT
ATGGGCCATTCGGGGAGCAAACGGCTGACTCTTCGGTAAAGTAATTTGCCAGGAACATTG
ATATATGCGCTGACCCTATTGATTATCCAACAAATACTACATTCAGCCCCAGGTCCCACG
TTAGGCGTAAGTTAAGAATTTATGTACGCAATCGCCAATATCCGCAAGACGTCCCCGCTG
ACAGTGAGGCTTAGTGGCGCCGATGGTATTCAGAAATGGAGCGCTCCTCTGTTGCACTCG
GCTCGTCAACCTTCTTGCTATTAACATATAAGAGTGAATGGGTGAGGTAGTAAACGTAAT
TGCCAAGATCGATAGAAGTGTTGGACGGAACATTGGAGCAAGGAACGCCGCTGAGCCAGG
GGACTAATGCCAGAGTGGAACCTGGTGGGAATTAAACACTTGTATACGTTGACAAGCTGA
GACATTCTAAAACACGTAATATAACATGCATCCACTAATGGATTCCCTTTCGCCTCTTGG
CTGGGATACATTTGCGCTTGGGAGCAGGAGATAGGGAGTCAAGGTGACATTGTGGGAATT
CACAAAGCTTCCTATCTAATGTTAGTACTTTAGCCACGGGTTAACCAGGACTGTTCTATA
GTTCCAACTCTCCATTATCCAAAACAAGCAGCA
>Rosalind_3098
GGCATAGGGACGCCGATGTAAGGAAATCCTCTAGTTTGAGCCCGGTGCTTACCGCAGTCC
TCGGCTTTCGTTCTGTTACAGACGTCCTAGGACTCAGTCGCCACCTACGCGGGGTGCATT
AGTCAGGTCCGAAGCCTCTATAGCGCTTTTTAGGAATGGAGCGTTTAAACGAGCCTGCGT
ATATTCGCTACCAAATCTCAGGGGCGGCTCAGATACAACGGGGTTCATCAGTTTGGATAT
CAGTGTTCGCTGGGTAAGTCTGACTCCGGCTCACAGATAGTTAGAAGGTCGCACATGATG
ATTACAACTTCTGCCGCTGACTTGGGAGTCTAGCGCTTGTCACAGACGCGCTAATGCGGC
ACATCTATTTCATAAAAGTACAAGCAATATGCCGCGAGGCCCCGTCGTATTTGATTCGAA
GGATTTAACTCATAACGCGGCCCTCAGCAGCTGCGGGCGTACGGAAGCCTCAACTTTGCG
ATTCTGTCGCACCTGCCTAGCTTAAGGAACCCCGATGCCGGTATCCACCGGACGTTTCGA
TTGCAAGATCTTGGCATGCCGCTACCTGTTGGAATTCAGTTATTAGTCCTACTCAGGAGG
GATAGCCGAACGGCACAAAGGCTTCGTTTGACAAGCACAGATGCATCTACTTAACTCGAT
AGCCTCAAAGAGTGTTTGCTCCGAGAGGGCCATCAGAGTAACTACCACGGCAAGAAGCGC
CTTCTTCCATGGCACACTCAAAAAGGTCATCTGAAGAGCCCATTTTTACCCACGGGATCC
CGCCACTAGACTCGTCACACTAAAACATAGAAGCAAGGCTGTAAGCGTACTCGGGTGTCC
CTAGCTACTTGACCCTGCGCTTTGATTTTCACCCAATCCAGCGCGTTAGCCAAACACCGG
CTCATGTGCGAGACACCTCTTGGACGGTACGAATACGCTTACTCCCACTCAGAACTGCTA
TCCGTGGGGTCCGTGGGGAGCCGGCGCAAAGAA
>Rosalind_2635
AACCTAAGCCACCTCGCGGTGTAACGCGCATCTGCAATCATCAGTTTCAGTCGGCGCAGC
GGAGCCCGGACAGCCTGTGCCGTACAAACCTGAAGCTGCTTACCTCGATTCATGCCAGGT
ATGAAGTATTCCGACGCTAATATCCTTTGGAATGGTTGCCAAGTCTCTACCAGCTACTCC
CATGACCGCATGACATATTCGACACGGTCTCTGAATGAGGTACGGTATTGCTTTCATTCT
AGTACGTTGCCCGACCTATGTACATCCGTCAACCACGGGGTGATCATACCTAAATTTGAA
TTAAAAAGTAGCGGAGCTACCGGACTGGTAGACTCCTCATCGCTCGGTTCAGTAGAAGGG
CTGGCCCTTTTCCTATCACTGTCCGTCCATTTCGTGTGTTTTAGGTGGTTTAGATATACC
TCTCATCGAAGAGTTGACCGTGTGATTAAATGAACGAACATTAAAGAGCGTGTGTTTAAA
TGCACGCAACACTAAAGGTGGAACATGGCGGTCGCCGTTATCGCATGGGTCTACTTGATC
GAAACTCAAGAGCATTGCAGACACAGGGACCCGTCAGGGTTTGTAAGCTGCGCGCTAATA
GTGCAACGTCCTAGGGTCGACTCCATGACGTAATGCAACTCTGGTTGACAATTCGTGAAG
TCGGAGTAAAGCTCCTGGCGCGCTGCACCCCCGGCTTCACCGTAGTTCCTACATTCTCGG
TCTAGTCGTGTGGGAATCACATCTGCTCCGAGGGTAAGGGGATTGGCATATAATGTGAGG
TAGCCGGCTAGGCGTATTAGCAACATCGTTGTCTATTGACTTGGAAGTTCTCTGTAGGAC
GTCGTCAGTCGGTAATCGCTGGTTTTAACTAAGGAGACACTGCTGGCACCGATGGCCGGG
GAGACCATTATGTATTCGGAGTGCCTCCGTTGTGGTGAATAACCAGGACTAATGAGGCCA
ACATAATACTAGACGTATACTATTTAGTGCGCT
>Rosalind_6087
ATTCGATGAATTTCCTCGATAGCGGCTCCGATTTAACACTACCTTGCCTTGACTCTCTAC
ACAGTAAGTACCCCCCGCAACTGGGGGACATTTTAGTGGCCCTTTGCGGAGTAGGGGTGT
TAGGTGTCGGCGTAAAGCGGATTCGATCAAACCCTGATCATCGGCTGAAATGGCCTCGAC
GGTGCTACTCTCAGTGACCTGCTGTTCCCGTAGCCTTTTAATACTCAATCCCTCGATCCG
CTATTCGACCAATCTCGAACTTGAATTCGGTGCGAATGAAACTCCAGTACGGTATGGCTT
GGACCGACGACGGAAGGAACTGCAACGTACCGACTTAATTTGGCTTCAATTCCTACCGAG
CATCATGCGGAAGCTACGCAATTGGATCTCAACAACCCCAAGAGACATTATAGTAGGACA
CACTTTATGGGATGCCGGGGACGGCATCTTCTGCAGGTTGGGAGGGCATCTTGCCTAGGT
GCCAACCTTCGGACGCTCAATGCTCTTACGGTCGGCAGGCTGTTCACGGAGGGCCTTATT
GGAAAAAGGTTATTTCACAAACGTTAAGTCCCTCAGATGACGTCTTGCGTCTCGCCAAGC
CTTTCTAGCTCCCGTCCAGGGCTTGAGCTTTCTTGACACGATAGCTTCCACGTTGACTCT
GAAAATCTCGAAAAACCGAAGGGGAGAGATGCGTCTTGGATCGTCCATAATGCTTCAGAC
GCTTCTAGCCTACCAGGTTGGTTAACAAGTTAATCCGCTAACTTATTGGCGCGTGAGCGA
CAGGACCGCGTCAGACTCATAGATACAGGGCTCATGGGGGCTATGTGTCTAATATGATCG
GCGACAAAGAGTTATGTAATGGCTTGGCTAGGAGACATAAAGGGGGACTTGATAGCGTTT
ACGAGCCTGTTCGGCCTCCCAAAGTTAACTAGATGAGACAGGATGTGCCCCGACACCCAC
GACTTCGTAAGGTAGAATAACGGACATAAGTCC
>Rosalind_4481
AAGGTGCTCAGAGACCTCGTTATGGATTGGTAACTATAGCAATTGCTTAAATCACGTTGT
TCAAATTTTGGGAACTGAATATGCTTCGGGCAATAGTATGAGTAGTCTAAATTGGGGAGT
GTAAGTGCGATTGGACACCACAAAGACAGGTAGTGAATGGGAGAGATTTGTTTGTAGCGC
GTTCGTGCGCGGGACGAGAAATGAATATCCTATTATCTGAAACCCGCCGCTGGGGCTGTA
GCGCCAAGAGCTTTCAGCGGGAGCTCCATGCGTGGAATCTTGCATCTACAATCACATATT
GGTAAGTAGCAACACTGACTGCAAGTACCACTCCCAGGAGAAGACTAGCCATTCAGTGTC
GCCGCTCACAAAGGGCGTAAAATGACATTCATGACGGCTAGCAGCGGACCACGATCCGTG
GCTCGCCGACACTCGGAACCATTCTTGTCTAATAGCTCAGCCCCAGGCTTTTCAACAGGG
GGCGACGCGACGAGCCTAATCGTTACGGATAAGGAGTGCGCACTAACTCGTCATCGGGGA
TAGACCAATTCTTGGAAAAGCAATCCTTAATATGATAGCTACTTGATGCATCTGTCGGCC
GGGGGACTGGACTGTCCTGAAATTGCTTAGGACTATATTTGAGCTTCCACTCCCACCCAG
GGGTGAGCAGATCCTGCCAAACGCGTATCCACTTAGATAAGCTCTTTAGCAAGGGGGCAG
CCTTTTTTCATCATGGTCTGCATTCGTGACTGAAATAATTCATCTCCACTGTACGTTACC
ATACCCTGACCACAATTTTTCCCAATGGGGTCATGCAAACGTACACACGTTTTGCGGCTG
GCTGAATTGCCGACTCATTTGTCCCGTATGCTAGCCCTGCTTGGATTCATAATTGTCTCG
CTCCGGACGTATTCGGGCCTGTGACAATCTTCCCACCTCATAGAACGCCCCAGAATACTC
GTTTTGCTGATGTCGCAGAACATTCTCCTCAGA
>Rosalind_0954
CTAATCTTGCGAATCAATCACAGGTGCGTTGATCCAGAGTCGTAGTTTTACAGTATGCAA
TGTATATTCTTTCTGATGGGACGAGTTTGCATGCAGTAGTTGGGTACTATGCCAGTGCGA
GACCGTCCCTCACCTAAATGCTATGCAGGGTTTCTCTACGATCAAATAGTCAAGTTGCTC
AGCCTCATCACATTGTGAATCACGGACAGACTGTAATTGTCAGCGTGTTCTCTAGGCAAA
TCGCCTTCCTTCTATCGACCTCCTTAGGTCCCCGTGAGGATCTCCTTATCCTGAAAAGTA
CAATCGGATACTTAGATTCTTCGCTCACTCTAATAGGTGGCTATACAGAAGTTTTATGGA
TAAGGGGTGTACGAAATCTTCGAGGGTGTATACCGCTGCTAGAACTCCATACATGATAAC
AACCAATCCTTAGCTAGTATACGAGGGATATGATAACGTTCCACCACCTCTTAAACTTTT
AAATTTGATCGCGGGTGGCCGTCGAAGTGTACGTATGAGATTGGGGCGGTTGTAGTTGCC
AGTGAAAGGCATATGCGGATGGCCTTTGGGTCCTGGTCATTCTTTCTCGCAGGTCGAGCC
AGTGCCTCAAATGAAATTTTCTCCTTAGCAACGACTCCTTAGTTAGAGAAACCAATCCCC
CCATGCCTGCGGATCGTGGTCAGCATGACGTCTGGTTGAACCCTTAGCTGAACAGATGGC
GTATTGCCGTACGAGGGGACCTTATAGGCGGCCTACCACACCAGACGAAGAGTCCGAAGG
TACGCCAAACGCATATTCAGGACGTAAGTGGGAGGACCCTGAGCCTCATTGCCGACTGAA
GGTGAATCGCTGGCCCACTGCTAGTTCCTCCCTTCGCTAATGGTCACGGGAATATCGCCA
CCTCGTCGATGACGCTCGATTAGACCTGTAGGAACACAACATACTAGGTGGACACGGGAC
ACCGATTTACCCACGCCGGACAGTCGTTCTTAT
>Rosalind_3750
ACAGTGTCATGGGATCTGGAGACGTATCCAAGCTAAACGCGCGTTCTATACAGACGTCGA
AACACGGGGGGCGAACTGCTTTAGCGACATGCTCTTACTGAAGTCTAGACGCTAAGGGCT
TTAGACAGCGAATAGTGGTTGATAGGTATTGAGCCATCCGTGTAGAGCGTTAGAAGGCCA
CGGCTTACTTGGTTAAAAGCTGATTTGGGCGGTTACATTCTGGGGTTTAAATACTATCGA
GTATCGATGCTTTTCTATGTATTGAAGACTGGTAAGCTTTCCCCGACCAGGTCGCGCCAT
CGTACCTTCTGGGGAAACTAATGCGGCTGAGTCGGCGACTTCAGGATGTCCCGATACACG
CAGCGTCACAGGTAAACTCGCCTTATAACGCGTCCCCGTCGATAAGGCCGACCCTTTCAG
ATGCGCGGTGCTCCTTCGATTGTTGACGACGCCATCCGAGGTCCAGACGTCTGAGGCCAC
GTGATCGGCCCCCTGTTACTGAGAAGCAGATTACCCCTAAGAATCGTCCGTCGCCTAGTA
GTTGCCGCAACCGACGATACTTCTCCAACATAATCTAGCGTATTTATCAAAGCGTCGTCG
TATCTAGCCTTACGGACGTAATACGAATACCCCCTGCTCAGTGGGCATGTAATACGCCAA
CCAAAAACACGCCAGTTACGAGGAGTGGCACTGCTATAAACCTAGATGAGATCGCTGATG
CCACGAGGAACCTTAGTTGAGTCCGCTGAACCCGCCAGTTGGCTTTGCAGGTCCGCGTTG
TTACTATGACTAAAATATATGATGGATACGCGGACCACTCCTACAGATGCTAAAAGTCAA
ACCGGCACCTATTAGATTTTTAACGGTGCACTTCTAACCGACATAGCCCGCGACCAGGGG
TGAAATTGCATTACATACGATATGATCGCTCCCAGGTCAATGACCACTTGACCTGTGAGT
TTGCTTATTAAGGTGGCTTTAGGCAGCGTAAGC
>Rosalind_9350
ATGAATTTTTAGCGCAAATGAACCGCCTGCTTCCATTAAGTCCCCGCTGCAGAAACCTCG
TTTGTATTCAGAAAGTTCACCTGACAACGGGGCATAGGGTAAATAGATGCTATGTAAATC
TTAGGGCTTACGCGGCGACTTTGACTTTTTCAGCGAACAGAGGCGAAGGCGACCAGCGTC
ATAGGTCTTCATACCGAAACAACAGGGGAGCATGGCCAATCACTGTCACTAACTCACGGG
ACTCCGCCTTGCTCGCCGGTGCCATATCGTACTGACGTAACTCATTGAATTCCATAGAAC
TTGGTTTAGGCCACCTCCGCCGAAACCCGTGGTGGTAAGTCAAGCGAGGACACCGGAAAT
TCCGACCCCGGTTCCCAACACAGGGCTATTCATCACATTTGGTGTACGTATTGATCCTTA
ATTGCCAGAGTCCTACTCGTTGATGTACGATCCACTTAAGTAAGGTCGGGCGTTCTACCG
CGCGGCGCATACCGGACATTATAGCTTAGGCCCCCCAGCTCTATTGTTATTACTATATCC
CTAATTCTAGAAGGGAAATTGTAAGATCAATTCCCGGCAGGTGGGCAGGAACAGACGTCG
AGCACCATTCGTAGTAAAGGTCTTTCTCGGTGTGTAGCGTTGACAAATCTGCAACCCAAC
CTTGTACTCTTCGCTGAACAATAGGTGCATTTCAAGACCGAGCTTGGCGCTGTTTCCTGA
CTGCAGCATGGGCAAAATTCTCGTAGGCAAGTGATCAATTAGCGGAACGCATTGGAAAAA
TTTGTTGGCACAATCCGGCACAGGTACTGATACCCCTCGATGTCGCAGTGCCGAGTCACC
CATCGCATGATCTGAGGTTGGTGCTGCCAGCGCTCTCCGAACAGGAGTCGTAGTTGCACT
CATGGCCGCTTTACGACGGGAGAAACTTACAGTAGCCTTGTAACAACTTTGTAAATCGTT
CATGGACTATCGTGAGGCAGACTTCTATTGTCC
>Rosalind_6074
CGAGGTAACAGTTGTCCGTTCTTTGTAGATTGCCTGGGGTGAAGGTACTAGTTAGCAATG
ATCAGAAGAAAATAGAGCCAGCCGGACTCTCGGGGCGGTACCAGGGTCGAGGAATCTGGG
TAAGTTTCCTATGTGATGAACAGGGTTTTCGATGGTAACGATGTGAACGACCCTGGGTCG
GGTTCAGCCCTCCTAACGAAACACGTGCTTCAGAAAAATAGTTGCAACCTGTTGTTGTCA
ACCTAGTCCTATAGAGTATGTTACTCGGCTATACTCAGGACCTATCCAGACCGCCACTCT
TTCTCTGTGTTAAAACCCCACCATATAAGATCCGTCCTCCCTTTTCACCGCCTTTACAGC
AGGGAGCCGTTGAGCAGGGCCAATGACGCCAAGACTTTACTAAAGTGACTGGTAGGTTCA
TTCTACCTATCCCTTTGCGTATTGATGTTTAGTCTGGTTTCAGGTACAGGTAAACCAGGT
GGCTGGTGCCATACTCGCTAAACAAATGTGGGGGCGCGAAAGATCTGGTGCAGGTTGACT
ACGATTTTATAGAGCAGTACACCGTGCTAGTCAGCATGAGTGGAGACACCTGAAATAAGT
GACGAGGTTGTCCAATGTATAGGACGACAGTTGCAGGGTGCACTGCAACAGAGTTATAAC
CATTACGTTGACTTAACACATGATTGTTAAAATGCTTCGACCCAAGACTCGGCGGGTCAA
AGTAAACCATTACGCGCGGGTGTCTGTAGCTACGGGTCAGCAGGGACCTAGCTATTACGA
GATAGGAAGGCCCACGTACCTAGGGGTCCCTTTTTCGGGTCTTTACCTGGTCAGCGAAGC
CCCGAAACGTGAACTCCAGTGATAACAGGTTAACGGCTTCTGGTGACGACTCTATCGAGT
TGTCAATGTAGCTTACAGGTACTATCGGGAATAATGTCGGGGGTGAACGTTGCGGTTTAA
AGTGGCTCAGCAAGCATATACACCTAGGTTGCG
Try using format strings:
f'{expression}'
str.join()
dict.items()
Code:
d = {'A': [5, 3, 3, 3, 1, 4, 2, 1, 2, 3], 'C': [2, 1, 3, 2, 1, 2, 2, 1, 3, 3], 'G': [1, 3, 2, 4, 3, 2, 1, 3, 3, 0], 'T': [2, 3, 2, 1, 5, 2, 5, 5, 1, 3]}
for k, v in d.items(): #loop over your output
g = " ".join(str(v) for v in v) #join list values
print(f'{k:}: {g:2}') #format text
Result:
A: 5 3 3 3 1 4 2 1 2 3
C: 2 1 3 2 1 2 2 1 3 3
G: 1 3 2 4 3 2 1 3 3 0
T: 2 3 2 1 5 2 5 5 1 3

Converting a heatmap image in the JPEG format to categories in python

I have a massive heatmap photo in the JPEG format and I'd like to convert the entries to 5 different categories based on the color of each one, and store the final result in three 2D-numpy arrays (One for section A, one for section B, one for section C).
The categories are as follows:
Red: 5,
Dark brown: 4,
Light brown: 3,
Light orange: 2,
White: 1
You can see the cropped photo that shows only a few top rows in the JPEG format in the following image:
How can I do this in python?
I would recommend you install and use OpenCV to help with the processing. This also provides a Python library to work with.
The following works for the heatmap you have given. You will need to modify it accordingly for your massive heatmap.
It first crops the image into 3, one for A, B and C respectively. Next it extracts the pixels for each category colour, converts them first to white and then resizes each to a 16 x 16 greyscale image. Next it converts non zero values to the current category value and adds this to your array.
import numpy as np
import cv2
def parse_image(img):
# Start with all zeros in a 16x16 array
data = np.zeros((16, 16), dtype=int)
# List holding min and max BGR values for each category
categories = [
(5, (0, 0, 250), (5, 5, 255)), # Red
(4, (5, 60, 152), (12, 78, 160)), # Dark brown
(3, (9, 105, 225), (18, 120, 240)), # Light brown
(2, (220, 235, 250), (230, 245, 255)), # Light orange
(1, (250, 250, 250), (255, 255, 255)), # White
]
for category, bgr_min, bgr_max in categories:
# Extract pixels in the required range and convert them to 255
mask = cv2.inRange(img, bgr_min, bgr_max)
image_cat = cv2.bitwise_or(img, np.full(img.shape, 255, dtype=np.uint8), mask=mask)
# Convert the image into greyscale
image_grey = cv2.cvtColor(image_cat, cv2.COLOR_BGR2GRAY)
# Resize the image to 16x16
values = cv2.resize(image_grey, (16, 16))
# Convert non black values into the current category value
values[values > 0] = category
# Add the values to the data array
data = data + values
return data
# Load the heatmap
image_src = cv2.imread("heatmap.jpg")
cv2.imshow("Source", image_src)
# Crop into 3 sub images
starty = 28
cropx = 234
cropy = 184
images = []
for number, startx in enumerate([30, 303, 572], start=1):
images.append(image_src[starty:starty+cropy,startx:startx+cropx])
# Parse A, B and C
abc = [parse_image(img) for img in images]
print abc
So for the heatmap you provided you would get the following output (which could be reshaped into a single array):
[array([[2, 2, 5, 3, 5, 3, 5, 4, 2, 5, 2, 2, 5, 2, 2, 2],
[2, 2, 2, 3, 2, 2, 4, 3, 2, 4, 2, 2, 5, 2, 2, 2],
[2, 2, 2, 2, 2, 2, 3, 2, 3, 3, 2, 2, 5, 2, 2, 2],
[4, 3, 2, 5, 4, 2, 5, 5, 5, 5, 2, 2, 5, 2, 2, 2],
[2, 3, 2, 4, 2, 2,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, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 2]])]
Note, working with JPG images means there will be artefacts in image, so colours will not have exact values. This is why there are min and max values. Also normally you will see RGB values. But here you will be working with BGR values instead. You will need to tweak the min max values to ensure you do not get any 0 values.

Create histogram from dict of lists

I am trying to graph the frequency of the numbers 1, 2, and 3 that occur for certain keys in a dictionary (titled 'hat1' through 'hat10') and am having trouble converting my data (shown below) into a format that I might be able to graph.
data = {'hat9': [[1, 2, 3, 1, 2]], 'hat8': [[1, 2, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]], 'hat1': [[1, 2, 3]], 'hat3': [[1, 2, 3, 1, 2, 2, 2, 1, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1]], 'hat2': [[1, 2, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], 'hat5': [[1, 2, 3, 2, 3, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 3, 3, 3, 3, 3, 3, 1, 3, 2, 3, 2, 3, 2, 3, 3, 3, 3, 2, 3, 1, 3, 3, 3, 3]], 'hat4': [[1, 2, 3, 1, 2, 1, 1, 1, 2, 1, 1, 1, 1, 3, 1, 1, 1, 2, 1, 1, 2, 1, 1, 2, 3, 1, 2, 1, 3, 2, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1]], 'hat7': [[1, 2, 3, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2]], 'hat6': [[1, 2, 3, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 3, 3, 3, 3, 1, 1, 3]], 'hat10': [[1, 2, 3, 3, 3, 3, 3, 3, 1, 2, 2, 1, 2, 3, 3, 2, 3, 3, 3, 3, 3, 2, 1, 1, 3, 3, 1, 2, 2, 3, 3, 1, 3, 3, 3, 3, 3, 2, 3, 1, 3, 1, 3, 1, 3, 3, 3, 3, 3, 3, 3, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 3, 3, 3, 3, 2, 1, 3, 2, 1, 3, 2, 3, 3, 1, 2, 1, 2, 3, 3, 1, 3, 2, 2, 1, 2, 3, 3, 1, 2, 3, 2, 3, 3, 1, 3, 3, 3, 3]]}
When I ran DataFrame.from_dict(data) I received output that looked like this:
In [100]: DataFrame.from_dict(data)
Out[100]:
hat1 hat10 \
0 [1, 2, 3] [1, 2, 3, 3, 3, 3, 3, 3, 1, 2, 2, 1, 2, 3, 3, ...
hat2 \
0 [1, 2, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ...
hat3 \
0 [1, 2, 3, 1, 2, 2, 2, 1, 2, 2, 2, 2, 1, 1, 1, ...
hat4 \
0 [1, 2, 3, 1, 2, 1, 1, 1, 2, 1, 1, 1, 1, 3, 1, ...
hat5 \
0 [1, 2, 3, 2, 3, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, ...
hat6 \
0 [1, 2, 3, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, ...
hat7 \
0 [1, 2, 3, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2]
hat8 hat9
0 [1, 2, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, ... [1, 2, 3, 1, 2]
I was hoping someone might be able to help me get the data into a more workable format that can be converted into a graph relatively easily. Thanks for your help.
If you want to create you histogram with Matplotlib, you don't really need to do much more than call its hist method with each hat you want to show. For example,
import pylab
pylab.hist(data['hat4'][0], bins=(1,2,3,4), align='left')
(You need to index at [0] because for some reason each of your dictionary values is a list of length 1, the single item itself being a list of data values).
If you need to aggregrate the hats in some way, you need to say how.
You can do the same with a pandas DataFrame if you prefer:
import pandas as pd
df = pd.DataFrame(data)
pylab.hist(df['hat4'], bins=(1,2,3,4), align='left')
Try this out:
data = {'hat9': [[1, 2, 3, 1, 2]], 'hat8': [[1, 2, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]], 'hat1': [[1, 2, 3]], 'hat3': [[1, 2, 3, 1, 2, 2, 2, 1, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1]], 'hat2': [[1, 2, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], 'hat5': [[1, 2, 3, 2, 3, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 3, 3, 3, 3, 3, 3, 1, 3, 2, 3, 2, 3, 2, 3, 3, 3, 3, 2, 3, 1, 3, 3, 3, 3]], 'hat4': [[1, 2, 3, 1, 2, 1, 1, 1, 2, 1, 1, 1, 1, 3, 1, 1, 1, 2, 1, 1, 2, 1, 1, 2, 3, 1, 2, 1, 3, 2, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1]], 'hat7': [[1, 2, 3, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2]], 'hat6': [[1, 2, 3, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 3, 3, 3, 3, 1, 1, 3]], 'hat10': [[1, 2, 3, 3, 3, 3, 3, 3, 1, 2, 2, 1, 2, 3, 3, 2, 3, 3, 3, 3, 3, 2, 1, 1, 3, 3, 1, 2, 2, 3, 3, 1, 3, 3, 3, 3, 3, 2, 3, 1, 3, 1, 3, 1, 3, 3, 3, 3, 3, 3, 3, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 3, 3, 3, 3, 2, 1, 3, 2, 1, 3, 2, 3, 3, 1, 2, 1, 2, 3, 3, 1, 3, 2, 2, 1, 2, 3, 3, 1, 2, 3, 2, 3, 3, 1, 3, 3, 3, 3]]}
keys = []
values = []
for key,value in data.iteritems():
keys.append(key)
a = 0
b = 0
c = 0
for x in value[0]:
if x==1: a+=1;
elif x ==2: b+=1;
elif x==3: c+=1;
values.append([a,b,c])
print keys
print values
Hopefully that helps. Keys is ['hat9', 'hat8', etc.,..] and values = [[freq of 1 in 'hats9', freq of 2 in 'hats9', freq of 3 in 'hats9'], [freq of 1 in 'hats8', freq of 2 in 'hats8', freq of 3 in 'hats8'],..] (a list of 3 item lists)

Permutations of a list of input numbers in Python

I'm supposed to write a program in Python that will get a list of numbers from the user until the user inputs 0, then print all the permutations of this list of numbers.
I was working on the code, which seemed right at the begining, but for some reason I'm getting weird output.
It seems like the list is static, and every time the function returns it adds objects to the latest list. This doesn't happen the way it was before the function was called recursively.
Here is what I have so far:
def permutation(numberList,array,place):
if (place==len(numberList)):
print array
else:
x=0
while (x < len(numberList)):
array.append(numberList[x])
permutation(numberList,array,place+1)
x+=1
def scanList():
numberList=[];
number=input()
#keep scanning for numbers for the list
while(number!=0):
numberList.append(number)
number=input()
return numberList
permutation(scanList(),[],0)
This is the output for the input 1 2 3 0, for example:
[1, 1, 1]
[1, 1, 1, 2]
[1, 1, 1, 2, 3]
[1, 1, 1, 2, 3, 2, 1]
[1, 1, 1, 2, 3, 2, 1, 2]
[1, 1, 1, 2, 3, 2, 1, 2, 3]
[1, 1, 1, 2, 3, 2, 1, 2, 3, 3, 1]
[1, 1, 1, 2, 3, 2, 1, 2, 3, 3, 1, 2]
[1, 1, 1, 2, 3, 2, 1, 2, 3, 3, 1, 2, 3]
[1, 1, 1, 2, 3, 2, 1, 2, 3, 3, 1, 2, 3, 2, 1, 1]
[1, 1, 1, 2, 3, 2, 1, 2, 3, 3, 1, 2, 3, 2, 1, 1, 2]
[1, 1, 1, 2, 3, 2, 1, 2, 3, 3, 1, 2, 3, 2, 1, 1, 2, 3]
[1, 1, 1, 2, 3, 2, 1, 2, 3, 3, 1, 2, 3, 2, 1, 1, 2, 3, 2, 1]
[1, 1, 1, 2, 3, 2, 1, 2, 3, 3, 1, 2, 3, 2, 1, 1, 2, 3, 2, 1, 2]
[1, 1, 1, 2,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, 2, 3, 2, 1, 2, 3, 3, 1, 2, 3, 3, 1, 1, 2, 3, 2, 1, 2, 3, 3, 1, 2]
[1, 1, 1, 2, 3, 2, 1, 2, 3, 3, 1, 2, 3, 2, 1, 1, 2, 3, 2, 1, 2, 3, 3, 1, 2, 3, 3, 1, 1, 2, 3, 2, 1, 2, 3, 3, 1, 2, 3]
I would appreciate any help.
The thing is, the list [] in Python is dynamic. So when you add elements to it with array.append(numberList[x]), they stay there forever. Just remove the added element after your recursive call:
def permutation(numberList,array,place):
if (place==len(numberList)):
print(array)
else:
x=0
while (x < len(numberList)):
array.append(numberList[x])
permutation(numberList,array,place+1)
array.pop()
x+=1
That's actually the common way to write depth-first search algorithms: modify your structure, make recursive call, undo modifications. The result of your program doesn't seem to be permutations of the input though.
The Python way is to use itertools.
from itertools import permutations
for permutation in permutations([1,2,3]):
print(permutation)
Now whats wrong with your algorithm. As you noticed, the list is static (well not really, but your using the same list every time)
A simple fix would be to copy the list each time.
def permutation(numberList,array,place):
if (place==len(numberList)):
print array
else:
x=0
while (x < len(numberList)):
array2 = array[:] // here happens the copy
array2.append(numberList[x])
permutation(numberList,array2,place+1)
x+=1

Python numpy array strange error [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have a rather large numpy array for a game I am making for python. Numpy returns an error on a comma. This is my array.
import numpy as np
111 x = np.array([[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 2, 2, 2, 2, 2, 2],
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4,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, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 2, 2, 2, 2, 2, 2],
^
SyntaxError: invalid syntax
I've already tried just deleting the comma and replacing it again, and its the same error
You need another bracket like this 2]],n13int43. Also Try to find out your mistakes using a minimal case. Always use Python shell for these basic errors!

Categories