row1a = Canvas(firstFrame,width=54,height=54,bg="gray")
row1a.pack(side=LEFT)
row1b = Canvas(firstFrame,width=54,height=54,bg="gray40")
row1b.pack(side=LEFT)
row1c = Canvas(firstFrame,width=54,height=54,bg="gray")
row1c.pack(side=LEFT)
row1d = Canvas(firstFrame,width=54,height=54,bg="gray40")
row1d.pack(side=LEFT)
row1e = Canvas(firstFrame,width=54,height=54,bg="gray")
row1e.pack(side=LEFT)
row1f = Canvas(firstFrame,width=54,height=54,bg="gray40")
row1f.pack(side=LEFT)
row1g = Canvas(firstFrame,width=54,height=54,bg="gray")
row1g.pack(side=LEFT)
row = 1
column = b
Is there a way i can update the "row1b" canvas' colour using something like canvas.config(bg="blue").
There are a lot more of these canvases so I cant really use row1b.config(bg="blue") for each canvas.
Thank you!
Like with any widget, you can change its options after it's been created.
Since you are creating many canvases, the best solution is to store them in a list or dictionary. Since you seem to want to reference them with numbers and letters, a dictionary might be the best solution.
For example, to create the canvas objects in a loop you could do something like this:
canvases = {}
row = 1
for column in ('a', 'b', 'c', 'd', 'e', 'f', 'g'):
index = (row, column)
canvases[index] = Canvas(...)
Later, you can change the color of any single canvas via the canvases dictionary and the configure command:
canvases[(1,'c')].configure(background="blue")
Related
Let's say we have
df = pd.Dataframe(columns = ['item', 'item1', 'item2'])
and I want to create a list of the columns without one element. I would like to do something like
df.columns.remove('item2') but such code simply return 'None". Meanwhile,
l = list(df.columns)
l.remove('item2')
works fine. Is there a way to distill this code into one line?
You can use .difference:
print(df.columns.difference(["item2"]))
Prints:
Index(['item', 'item1'], dtype='object')
Or a list output:
print(df.columns.difference(["item2"]).tolist())
Prints:
['item', 'item1']
I have this assignment in which I have a file that contains alot of chromosed that I need to calculate for each one of them the mutation level.
The problem is that each chromosome can appear several times and I need to find the mean for all the mutation levels of this chromosome. and on top of that i need that the mutation will be in same nucleotides (T-->C or G-->A).
The mutation level is calculate by DP4 under INFO which contains four numbers that represented as [ref+,ref-,alt+,alt-]
Example of the file:
#CHROM POS ID REF ALT QUAL FILTER INFO FORMAT Aligned.sortedByCoord.out.bam
chr1 143755378 . T C 62 . DP=550;VDB=0;SGB=-0.693147;RPB=1.63509e-10;MQB=1;BQB=0.861856;MQ0F=0;AC=2;AN=2;DP4=0,108,0,440;MQ=20 GT:PL:DP 1/1:89,179,0:548
chr3 57644487 . T C 16.4448 . DP=300;VDB=0;SGB=-0.693147;RPB=0.993846;MQB=1;BQB=0.316525;MQ0F=0;ICB=1;HOB=0.5;AC=1;AN=2;DP4=0,166,0,134;MQ=20 GT:PL:DP 0/1:49,0,63:300
chr3 80706912 . T C 212 . DP=298;VDB=0;SGB=-0.693147;RPB=0.635135;MQB=1;MQSB=1;BQB=0.609797;MQ0F=0;AC=2;AN=2;DP4=1,1,256,40;MQ=20 GT:PL:DP 1/1:239,255,0:298
So this what I did until now and Im kinda stuck not really knowing how to continue from that point:
def vcf(file):
with open(file, "r+") as my_file:
"""First I wanted to clear the headline"""
for columns in my_file:
if columns.startswith("#"):
continue
"""Then I split the file into columns"""
for columns in my_file:
columns=columns.rstrip('\n').split('\t')
"""This is the info column"""
for row in columns[7]:
row = columns[7].split(";")
"""Using slicing I extracted the DP4 part and removed the str DP4"""
DP4 = [row[-2]]
new_DP4 = [x.replace("DP4=","") for x in DP4]
"""Then I took all the int outs and put them under the categories"""
for x in new_DP4:
xyz = x.split(",")
ref_plus = int(xyz[0])
ref_minus = int(xyz[1])
alt_plus = int(xyz[2])
alt_minus = int(xyz[3])
"""calculated the mean for each one"""
formula = ((alt_minus+alt_plus)/(alt_minus+alt_plus+ref_minus+ref_plus))
"""made a list of the chromosomes and their means"""
chr_form = [columns[0] , columns[3], columns[4], (formula)]
so basically I thought that now that I have all the data in list I can sort out somehow the same chr and do the means but I cant figure out how to do it.. I tried to use regex as well but im not that familiar with that
this is my current output for chr_form:
['chr3', 'T', 'C', 0.44666666666666666]
['chr3', 'T', 'C', 0.9932885906040269]
['chr5', 'A', 'G', 0.42073170731707316]
['chr5', 'A', 'G', 0.5772870662460567]
['chr6', 'A', 'G', 0.5153061224489796]
['chr6', 'A', 'G', 0.8934010152284264]
and so on..
but the output I want to get in the end is this:
{1: {‘T->C’: 0.802}, 3: {‘T->C’:0.446}}
I'll be happy to get an idea or example how to calculate the mean for each chr,
You have lots of unnecessary for loops. The only loop you need is for the lines in the file, you don't need to loop over the characters in fields when you're splitting them or removing something from the whole field.
At the end, you should be adding the result of the calculation to a dictionary.
def vcf(file):
chromosomes = {}
with open(file, "r+") as my_file:
# First I wanted to clear the headline
for line in my_file:
if line.startswith("#"): # skip comment lines.
continue
line=line.rstrip('\n').split('\t')
# This is the info column
info = line[7].split(";")
# Using slicing I extracted the DP4 part and removed the str DP4
DP4 = info[-2].replace("DP4=","")
# Then I took all the int outs and put them under the categories
ref_plus, ref_minus, alt_plus, alt_minus = map(int, DP4.split(','))
# calculated the mean for each one
formula = ((alt_minus+alt_plus)/(alt_minus+alt_plus+ref_minus+ref_plus))
# Get chromosome number from first field
chr_num = int(line[0].replace('chr', ''))
chromosomes[chr_num] = {f'{line[3]}->{line[4]}': formula}
return chromosomes
I have a dictionary that I want to use to describe data that changes with time. I want to then create an outer dictionary that has time increments as the keys and the previously mentioned dictionary as the value.
However, when I try to change one element in the nested dictionary in a given time step all of the time steps with the same dictionary key are changed as well.
I would like to have the same keys for the inner dictionaries but be able to insert unique values for the different time steps.
Any ideas why this is occurring?
time = 3
inner_dict = dict([(i,[]) for i in ['a','b','c']])
outer_dict= dict([(time_step,inner_dict) for time_step in range(time)])
test[1]['a'] = 20
print(test[2]['a'])
test[2]['a'] should still be an empty list. Or at least that's what I want to achieve.
This is happening because you are passing reference to the same mutable inner_dict to each time step in the outer dictionary. You need to create a separate instance of inner dict for each time step. Try doing something like this:
outer = dict([(time_step, {i:[] for i in ['a', 'b', 'c']}) for time_step in range(time)])
Or simply in dict comprehension
outer = {timestep: {i:[] for i in ['a', 'b', 'c']} for timestep in range(time)}
All, I need to find an effective way to input my data into my script to end up with a dictionary that looks like so:
CSSes_2_add = {
'Test CSS 1': ['Test CSS 1',['Staging','On Cluster','Test9','Test8', 'Test7', 'Test6', 'Test5',]],
'Test CSS 2': ['Test CSS 2',['On Cluster','Staging','Test1','Test2', 'Test3', 'Test4', 'Test5']],
'Auto Registered Devices': ['For Auto Registered Device Templates' ,['Block Toll Fraud']]
}
Please note that the number of elements of: `CSSes_2_add[x][1] will be different for each key of the dictionary. Instead of hardcoding my dictionary into my script, I'd like to know of a more efficient way to input my data. Please help
P.S.
If the suggestion is CSV, how do I handle the varying number of elements of the nested list.
Just using the data you have listed above an easy way of doing what you want is to loop through all the keys and data.
keys = ['Test CSS 1', 'Test CSS 2', 'Auto Registered Devices']
data_keys = ['Test CSS 1','Test CSS 2','For Auto Registered Device Templates']
d1 = ['Staging','On Cluster','Test9','Test8', 'Test7', 'Test6', 'Test5']
d2 = ['On Cluster','Staging','Test1','Test2', 'Test3', 'Test4', 'Test5']
d3 = ['Block Toll Fraud']
allData = [d1,d2,d3]
d = {}
for key, data_key in zip(keys, data_keys):
d[key] = [data_key]
for key, data in zip(list(d.keys()), allData):
d[key].append(data)
This will get you what you asked for, but if you are planning on accessing data from this dictionary it might get a little messy having to run through the nested list indexes.
I solved my problem with DictReader:
I have a CSV input file named "addCss.csv" with the following contents:
CssName,CssDesc,Partition1,Partition2,Partition3,Partition4,Partition5,Partition6,Partition7,Partition8,Partition9,Partition10,Partition11,Partition12,Partition13,Partition14,Partition15
CSS1,Description for CSS1,On Net,Staging,,,,,,,,,,,,,
CSS2,Description for CSS2, Test1, Test3, Test5,,,,,,,,,,,,
CSS3,Description for CSS3, Partition1, Test9, Test6,,,,,,,,,,,,
I then loop through the file and add the data to my dictionary as follows:
with open('addCss.csv', newline='') as css_csv_file:
css_csv_reader = csv.DictReader(css_csv_file)
CSSes_2_add = {}
for css_row in css_csv_reader:
css_input_outer_list = []
css_input_nested_list = []
css_input_outer_list.append(css_row['CssDesc'])
CSSes_2_add[css_row['CssName']] = css_input_outer_list
for N in range(1,15):
n = str(N)
if css_row['Partition'+n] != '':
css_input_nested_list.append(css_row['Partition'+n])
css_input_outer_list.append(css_input_nested_list)
print(CSSes_2_add)
I have two environments, A and B, in a CROSS TABLE. Each environment has stores with the amount of units next to them. Additionally, there is a column that shows the percentage of total units for each store in each environment.
The code for percentage of total is as follows:
Sum([UNITS]) THEN [Value] / Sum([Value]) OVER (All([Axis.Rows])) as [% Units]
Let's say store 1 has a different percentage of total for each environment. I want to create a separate custom expression that shows the difference between these two percentages.
Right now, I have a variation of this that is not desirable. It simply shows the percent change in units for store 1, rather than the change in the percentage of total. This code looks like:
(Sum([UNITS]) - Sum([UNITS]) OVER (PreviousPeriod([Axis.Columns]))) / Sum([UNITS]) OVER (PreviousPeriod([Axis.Columns])) as [Unit Difference]
I have tried unsuccessfully to embed the first piece of code within the second piece. Any help will be greatly appreciated!
I believe what you're looking for is something along the lines of
SUM(If([Environment] = 'A', [Units], 0))/(SUM(If([Environment] = 'A', [Units], 0)) OVER (Parent([Axis.Rows])) - SUM(If([Environment] = 'B', [Units], 0))/(SUM(If([Environment] = 'B', [Units], 0)) OVER (Parent([Axis.Rows])) as [% Difference]
This would require removing the A/B differentiation from the Horizontal access and replacing it with (Column Names) and making two different columns of custom expressions, composed of
SUM(If([Environment] = 'A', [Units], 0))/(SUM(If([Environment] = 'A', [Units], 0)) OVER (Parent([Axis.Rows])) as [A %]
and
SUM(If([Environment] = 'B', [Units], 0))/(SUM(If([Environment] = 'B', [Units], 0)) OVER (Parent([Axis.Rows])) as [B%]
If this is not what you are looking for, I suggest you clarify with an example with example numbers showing what you want the output to look like.