Related
I am trying to process a 3D image in chunks (non-overlapping windows). Once this is done I want to put the chunks back together in the right order.
I have been chunking the image as below:
tens = torch.tensor(range(64))
tens = tens.view((4,4,4))
print(tens)
>>>tensor([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]],
[[16, 17, 18, 19],
[20, 21, 22, 23],
[24, 25, 26, 27],
[28, 29, 30, 31]],
[[32, 33, 34, 35],
[36, 37, 38, 39],
[40, 41, 42, 43],
[44, 45, 46, 47]],
[[48, 49, 50, 51],
[52, 53, 54, 55],
[56, 57, 58, 59],
[60, 61, 62, 63]]])
tens = torch.chunk(tens,2, -1)
tens = torch.stack(tens)
tens = torch.chunk(tens,2, -2)
tens = torch.concat(tens)
tens = torch.chunk(tens,2, -3)
tens = torch.concat(tens)
print(torch.shape)
>>>torch.Size([8, 2, 2, 2])
Then I want to put it back together in the original order
tens = tens.view([4,4,2,2])
tens = tens.view([2,4,4,2])
tens = tens.view([4,4,4])
print(tens)
>>>tensor([[[ 0, 1, 4, 5],
[16, 17, 20, 21],
[ 2, 3, 6, 7],
[18, 19, 22, 23]],
[[ 8, 9, 12, 13],
[24, 25, 28, 29],
[10, 11, 14, 15],
[26, 27, 30, 31]],
[[32, 33, 36, 37],
[48, 49, 52, 53],
[34, 35, 38, 39],
[50, 51, 54, 55]],
[[40, 41, 44, 45],
[56, 57, 60, 61],
[42, 43, 46, 47],
[58, 59, 62, 63]]])
and I can't figure out how to get the elements in the right order. I realise I probably missed something in the docs or something else obvious but I can't find it. Any ideas?
Operator torch.chunk doesn't reduce dimensions so its inverse is torch.cat, not torch.stack.
Here are the transforms with the corresponding inverse operations:
Splitting the last dimension in two:
>>> chunks = tens.chunk(chunks=2, dim=-1)
>>> torch.cat(chunks, dim=-1)
Splitting the second dimension into two:
>>> chunks = tens.chunk(chunks=2, dim=-2)
>>> torch.cat(chunks, dim=-2)
Splitting the first dimension into two:
>>> chunks = tens.chunk(chunks=2, dim=-3)
>>> torch.cat(chunks, dim=-3)
If you want to invert the whole sequence, you just have to keep in mind that torch.cat is the reverse operation of torch.chunk:
>>> tens = torch.cat(tens.chunk(2), dim=-3)
>>> tens = torch.cat(tens.chunk(2), dim=-2)
>>> tens = torch.cat(tens.chunk(2), dim=-1)
given a 3D array:
a = np.arange(3*4*5).reshape(3,4,5)
array([[[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]],
[[20, 21, 22, 23, 24],
[25, 26, 27, 28, 29],
[30, 31, 32, 33, 34],
[35, 36, 37, 38, 39]],
[[40, 41, 42, 43, 44],
[45, 46, 47, 48, 49],
[50, 51, 52, 53, 54],
[55, 56, 57, 58, 59]]])
I would like to create the following matrix:
result =
array([[20, 21, 22, 23, 24],
[ 5, 6, 7, 8, 9],
[50, 51, 52, 53, 54],
[55, 56, 57, 58, 59]])
Using the indices idx = [1,0,2,2]
I.e, I would like to "take" per matrix, the row specified in idx, where len(idx)==a.shape[1] and np.max(idx)<a.shape[0] as idx choose from dimension 1.
Given that your array has three dimensions (x,y,z), since you want to take one value for each row in the yth direction, you can do this:
a[idx, range(a.shape[1])]
Output:
array([[20, 21, 22, 23, 24],
[ 5, 6, 7, 8, 9],
[50, 51, 52, 53, 54],
[55, 56, 57, 58, 59]])
My goal is to read info from a csv file and save that to a dictionary(Thanks to comments). Dictionary will contain name as the key and numbers as the value. Dictionary should be dynamic i.e. should work for n number of row characters. What ı need: be able to change the key values and create only one dictionary not multiple dicts.
name,AGATC,AATG,TATC
Alice,2,8,3
Bob,4,1,5
Charlie,3,2,5
Alice = [2,8,3]
Bob = [4,1,5]
etc.
The code should work for this too with same logic:
name,AGATC,TTTTTTCT,AATG,TCTAG,GATA,TATC,GAAA,TCTG
Albus,15,49,38,5,14,44,14,12
Cedric,31,21,41,28,30,9,36,44
Draco,9,13,8,26,15,25,41,39
Fred,37,40,10,6,5,10,28,8
Ginny,37,47,10,23,5,48,28,23
Hagrid,25,38,45,49,39,18,42,30
Harry,46,49,48,29,15,5,28,40
Hermione,43,31,18,25,26,47,31,36
James,46,41,38,29,15,5,48,22
Kingsley,7,11,18,33,39,31,23,14
Lavender,22,33,43,12,26,18,47,41
Lily,42,47,48,18,35,46,48,50
Lucius,9,13,33,26,45,11,36,39
Luna,18,23,35,13,11,19,14,24
Minerva,17,49,18,7,6,18,17,30
Neville,14,44,28,27,19,7,25,20
Petunia,29,29,40,31,45,20,40,35
Remus,6,18,5,42,39,28,44,22
Ron,37,47,13,25,17,6,13,35
Severus,29,27,32,41,6,27,8,34
Sirius,31,11,28,26,35,19,33,6
Vernon,26,45,34,50,44,30,32,28
Zacharias,29,50,18,23,38,24,22,9
Here is my try:
def readcsv(n):
with open(f'{n}','r') as f:
readed = csv.reader(f)
for row in readed:
key = row[0]
value = row[1:]
#print(f"{key} and {value}")
dic = dict(key = value)
print(dic)
OUTPUT :
{'key': ['AGATC', 'AATG', 'TATC']}
{'key': ['2', '8', '3']}
{'key': ['4', '1', '5']}
{'key': ['3', '2', '5']}
You can read in .csv files easily using the pandas library. What you are looking for is better served with a dictionary where the names are the keys and the lists are the values.
If you have a dictionary and want to change the value of a certain key (say, Albus in the dict, d below), it is very straight-forward.
# To change the value associated with key="Albus"
d["Albus"] = [1,2,3,4,5,6,7,8]
# To access the value of key="Albus"
d["Albus"]
Code
import pandas as pd
import os # for handling file-paths
from io import StringIO # for reading dummy data
## Reading from a file "input.csv"
# df = pd.read_csv("input.csv", sep=",").set_index('name')
## Reading from the dummy data as a string
df = pd.read_csv(StringIO(s.strip()), sep=",").set_index('name')
## Subsequently process the data to get a
# dict of structure (key=name, value=list).
df = pd.DataFrame(df.to_numpy().T, columns=df.index)
d = df.to_dict(orient='list) # returns a dictionary ==> data-structure
print(d)
Output:
{'Albus': [15, 49, 38, 5, 14, 44, 14, 12],
'Cedric': [31, 21, 41, 28, 30, 9, 36, 44],
'Draco': [9, 13, 8, 26, 15, 25, 41, 39],
'Fred': [37, 40, 10, 6, 5, 10, 28, 8],
'Ginny': [37, 47, 10, 23, 5, 48, 28, 23],
'Hagrid': [25, 38, 45, 49, 39, 18, 42, 30],
'Harry': [46, 49, 48, 29, 15, 5, 28, 40],
'Hermione': [43, 31, 18, 25, 26, 47, 31, 36],
'James': [46, 41, 38, 29, 15, 5, 48, 22],
'Kingsley': [7, 11, 18, 33, 39, 31, 23, 14],
'Lavender': [22, 33, 43, 12, 26, 18, 47, 41],
'Lily': [42, 47, 48, 18, 35, 46, 48, 50],
'Lucius': [9, 13, 33, 26, 45, 11, 36, 39],
'Luna': [18, 23, 35, 13, 11, 19, 14, 24],
'Minerva': [17, 49, 18, 7, 6, 18, 17, 30],
'Neville': [14, 44, 28, 27, 19, 7, 25, 20],
'Petunia': [29, 29, 40, 31, 45, 20, 40, 35],
'Remus': [6, 18, 5, 42, 39, 28, 44, 22],
'Ron': [37, 47, 13, 25, 17, 6, 13, 35],
'Severus': [29, 27, 32, 41, 6, 27, 8, 34],
'Sirius': [31, 11, 28, 26, 35, 19, 33, 6],
'Vernon': [26, 45, 34, 50, 44, 30, 32, 28],
'Zacharias': [29, 50, 18, 23, 38, 24, 22, 9]}
Dummy Data
s = """
name,AGATC,TTTTTTCT,AATG,TCTAG,GATA,TATC,GAAA,TCTG
Albus,15,49,38,5,14,44,14,12
Cedric,31,21,41,28,30,9,36,44
Draco,9,13,8,26,15,25,41,39
Fred,37,40,10,6,5,10,28,8
Ginny,37,47,10,23,5,48,28,23
Hagrid,25,38,45,49,39,18,42,30
Harry,46,49,48,29,15,5,28,40
Hermione,43,31,18,25,26,47,31,36
James,46,41,38,29,15,5,48,22
Kingsley,7,11,18,33,39,31,23,14
Lavender,22,33,43,12,26,18,47,41
Lily,42,47,48,18,35,46,48,50
Lucius,9,13,33,26,45,11,36,39
Luna,18,23,35,13,11,19,14,24
Minerva,17,49,18,7,6,18,17,30
Neville,14,44,28,27,19,7,25,20
Petunia,29,29,40,31,45,20,40,35
Remus,6,18,5,42,39,28,44,22
Ron,37,47,13,25,17,6,13,35
Severus,29,27,32,41,6,27,8,34
Sirius,31,11,28,26,35,19,33,6
Vernon,26,45,34,50,44,30,32,28
Zacharias,29,50,18,23,38,24,22,9
"""
Do you mean the variable name by "name"? I suppose it would be possible do something like this
globals()[row[0]] = list(row[1:])
However it would be highly unconventional to set a variable name during runtime. A better solution for a namespace would be using a dictionary:
rows = {row[0]:row[1:]} for row in readed}
You could the access the relevant element by using rows["name of row"]
I seems found a bug when I'm using python 2.7 with numpy module:
import numpy as np
x=np.arange(3*4*5).reshape(3,4,5)
x
Here I got the full 'x' array as follows:
array([[[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]],
[[20, 21, 22, 23, 24],
[25, 26, 27, 28, 29],
[30, 31, 32, 33, 34],
[35, 36, 37, 38, 39]],
[[40, 41, 42, 43, 44],
[45, 46, 47, 48, 49],
[50, 51, 52, 53, 54],
[55, 56, 57, 58, 59]]])
Then I try to indexing single row values in sheet [1]:
x[1][0][:]
Result:
array([20, 21, 22, 23, 24])
But something wrong while I was try to indexing single column in sheet [1]:
x[1][:][0]
Result still be the same as previous:
array([20, 21, 22, 23, 24])
Should it be array([20, 25, 30, 35])??
It seems something wrong while indexing the middle index with range?
No, it's not a bug.
When you use [:] you are using slicing notation and it takes all the list:
l = ["a", "b", "c"]
l[:]
#output:
["a", "b", "c"]
and in your case:
x[1][:]
#output:
array([[20, 21, 22, 23, 24],
[25, 26, 27, 28, 29],
[30, 31, 32, 33, 34],
[35, 36, 37, 38, 39]])
What you realy wish is using numpy indexing notation:
x[1, : ,0]
#output:
array([20, 25, 30, 35])
This is not a bug. x[1][:][0] is not a multiple index ("give me the elements where first dimension is 1, second is any, third is 0"). Instead, you are indexing three times, three objects.
x1 = x[1] # x1 is the first 4x5 subarray
x2 = x1[:] # x2 is same as x1
x3 = x2[0] # x3 is the first row of x2
To use multiple index, you want to do it in a single slice:
x[1, :, 0]
import random
orig=list(range(1,65))
temp_orig= [[0, 1, 2, 3, 4, 5, 6, 7],
[8, 9, 10, 11, 12, 13, 14, 15],
[16, 17, 18, 19, 20, 21, 22, 23],
[24, 25, 26, 27, 28, 29, 30, 31],
[32, 33, 34, 35, 36, 37, 38, 39],
[40, 41, 42, 43, 44, 45, 46, 47],
[48, 49, 50, 51, 52, 53, 54, 55],
[56, 57, 58, 59, 60, 61, 62, 63]]
ip= [[40, 24, 9, 41, 42, 56, 43, 45],
[4, 23, 21, 60, 35, 6, 59, 0],
[36, 53, 32, 16, 7, 37, 17, 18],
[10, 62, 61, 38, 29, 34, 31, 25],
[54, 57, 51, 49, 39, 3, 50, 30],
[11, 46, 33, 27, 44, 15, 13, 48],
[12, 58, 1, 26, 47, 20, 28, 52],
[19, 55, 2, 63, 22, 8, 14, 5]]
ipinv=[[15, 50, 58, 37, 8, 63, 13, 20],
[61, 2, 24, 40, 48, 46, 62, 45],
[19, 22, 23, 56, 53, 10, 60, 9],
[1, 31, 51, 43, 54, 28, 39, 30],
[18, 42, 29, 12, 16, 21, 27, 36],
[0, 3, 4, 6, 44, 7, 41, 52],
[47, 35, 38, 34, 55, 17, 32, 57],
[5, 33, 49, 14, 11, 26, 25, 59]]
print "\n\nOriginal position of Bits:"
for i in range(len(ipinv)):
print "\t",temp_orig[i]
print "\n\nInitial Permutation Table"
for i in range(len(ipinv)):
print "\t",ip[i]
print "\n\nInverse Initail permutation Table"
for i in range(len(ipinv)):
print "\t",ipinv[i]
print "\nRound 1:Initial Permutation"
plaintext=list(raw_input("\n\tEnter not more than 8 char:"))
l=len(plaintext)
for i in range(8,len(plaintext)):
del plaintext[l-1+8-i]
print "\n\tPlain text:",plaintext,"\n"
for i in range(8):
plaintext[i]=list(bin(ord(plaintext[i])).zfill(8))
print "\t",plaintext[i]
ip1=temp_orig
for i in range(8):
for j in range(8):
ip1[i][j]=plaintext[(ip[i][j])/8][(ip[i][j])%8]
print "\nEnciphered list:"
for i in range(8):
print "\t",ip1[i]
print "\n73\n",ip1[7][3],"\n73"
ip1_d=temp_orig
for i in range(8):
for j in range(8):
ip1_d[i][j]=ip1[(ipinv[i][j])/8][(ipinv[i][j])%8]
print ip1[7][3],"ip1_d",[i],[j],"=ip1[",(ipinv[i][j])/8,"][",(ipinv[i][j])%8,"]"
#print "ip1_d",[i],[j],"=",ip1[(ipinv[i][j])/8][(ipinv[i][j])%8]
print "\nDeciphered list:"
for i in range(8):
print "\t",ip1_d[i]
The de sunstitution becomes wrong, just for example i tried printing ip1[7][3] and all of a sudden it changes value when i=7 and j=3 without any assignment to ip1
You do ip1=temp_orig a few times. That tells me that you probably think this copies the temp_orig list - it does not. You have to explicitly copy the list here:
ip1=[list(row) for row in temp_orig]
and so on. Your code just gives new names to temp_orig but still modifies the temp_orig list.
I think this is the most chaotic code I've seen in a while. You should start thinking more in terms of transformations of your data instead of juggling indices like this. I still have no idea what your code does or how it's supposed to work.