Split contiguous regions within numpy array into smaller contiguous regions - python
I am trying to identify the center of mass of each contiguous regions of a certain size (i.e. 30 total pixels) within a binary numpy array.
I've used scipy.ndimage.label to identify any contiguous regions of pixels within my array and it worked great. However, in some cases there are contiguous regions that are larger than the size I am looking for (i.e. 60 pixels, 75 pixels, 90 pixels, etc.) In these cases I need to split the large contiguous region into several contiguous regions of my desired size.
For example:
Imagine the following array and I need to find all contiguous areas within the array.
>>> x
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0],
[0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0],
[0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 4, 4, 4, 4, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 4, 4, 4, 4, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 4, 4, 4, 4, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 4, 4, 4, 4, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
Let's say I'm only interested in contiguous areas of 9 pixels. This is an example of the output I would be looking for.
>>> contiguous_regions
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 5, 5, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0, 0, 3, 3, 3, 8, 8, 8, 9, 9, 9, 19, 19, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 8, 8, 8, 9, 9, 9, 19, 19, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 8, 8, 8, 9, 9, 9, 19, 19, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 10, 11, 11, 11, 12, 12, 12, 19, 19, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 10, 11, 11, 11, 12, 12, 12, 19, 20, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 10, 11, 11, 11, 12, 12, 12, 20, 20, 0, 4, 4, 4, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 13, 13, 14, 14, 14, 15, 15, 15, 20, 20, 0, 4, 4, 4, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 13, 13, 14, 14, 14, 15, 15, 15, 20, 20, 0, 4, 4, 4, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 13, 13, 14, 14, 14, 15, 15, 15, 20, 20, 0, 22, 22, 22, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 16, 16, 17, 17, 17, 18, 18, 18, 0, 0, 0, 22, 22, 22, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 16, 16, 17, 17, 17, 18, 18, 18, 0, 0, 0, 22, 22, 22, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 16, 16, 17, 17, 17, 18, 18, 18, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 21, 21, 21, 21, 21, 21, 21, 21, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
It's ok if edges get cutoff and the contiguous regions do not all need to be the same shape necessarily, but the more square the better. Eventually, once I have the smaller regions of interest, I need to get the center of mass of each one using scipy.ndimage.measurements.center_of_mass. If there is a way I could reduce these contiguous areas into individual pixels x distance from one another, that would work too.
Any ideas on how to accomplish this with numpy and scipy?
Thanks in advance
If your labels are just 0 and 1 you could mess around with the following, though I think there are some cases where it might not work. To get you started, here's how you can create an index grid:
igrid = np.repeat(np.arange(4), 5)[..., None] + np.repeat(np.arange(4), 5)[None, ...]
array([[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3],
[1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4],
[1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4],
[1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4],
[1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4],
[1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4],
[2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5],
[2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5],
[2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5],
[2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5],
[2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5],
[3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6],
[3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6],
[3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6],
[3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6],
[3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6]])
So in this case, your maximum allowed size is 25 but you could change this by changing the second parameter of repeat. To get the right size to match your original array you could just generate a large enough igrid and slice out the region you want.
You can then do:
result = np.where(contiguous_regions, contiguous_regions, contiguous_regions + igrid)
Related
Filtering elements of a numpy array depending on their occurrences
i have the following 2D numpy array M M = np.array([[1,1,1,0,0,0,0,0,0,0,0], [1,1,1,0,0,0,0,0,0,1,1], [1,1,1,0,0,0,0,0,0,1,1], [0,0,0,0,0,1,1,1,0,0,0], [0,0,0,0,0,1,1,1,0,0,0], [1,1,1,0,1,1,1,1,0,0,0], [1,1,1,0,0,1,1,1,0,0,0], [1,1,1,0,0,1,1,1,0,0,0]]) which I want to identify its spots (Pixels with value==1 and connected to each other). Thanks to the function 'label' from scipy, I can identify all of my spots in the matrix. The output should seem like this: Output, Nbr= label(M) #Output= array([[1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0], # [1, 1, 1, 0, 0, 0, 0, 0, 0, 2, 2], # [1, 1, 1, 0, 0, 0, 0, 0, 0, 2, 2], # [0, 0, 0, 0, 0, 3, 3, 3, 0, 0, 0], # [0, 0, 0, 0, 0, 3, 3, 3, 0, 0, 0], # [4, 4, 4, 0, 3, 3, 3, 3, 0, 0, 0], # [4, 4, 4, 0, 0, 3, 3, 3, 0, 0, 0], # [4, 4, 4, 0, 0, 3, 3, 3, 0, 0, 0]]) I want only to have spots with 9 elements, that means the first and fourth spot. using a for loop like this works fine: for i in range(Nbr+1): Spot= np.argwhere(components[:,:]== i) if len(Spot)!=9: M[Spot[:, 0], Spot[:, 1]]=0 #M= array([[1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0], # [1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0], # [1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0], # [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # [1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0], # [1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0], # [1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]]) The porblem is when my Spots are more than 4, my code is slower. Is there any faster alternative that can do the job of the for loop? Thanks.
MultiLabelBinarizer() with inverse_transform()
I have multilabel labels. Elements in a label means voting. Here is how labels look: array([[ 4, 0, 0, 1, 3, 2, 0, 0], [ 6, 0, 1, 1, 0, 0, 0, 0], [ 5, 0, 0, 3, 1, 0, 0, 0], [ 4, 0, 0, 4, 1, 0, 0, 0], [ 9, 0, 0, 1, 0, 0, 0, 0], [ 6, 0, 0, 1, 0, 0, 1, 1], [ 2, 0, 0, 8, 0, 0, 0, 0], [ 0, 10, 0, 0, 0, 0, 0, 0], [ 0, 10, 0, 0, 0, 0, 0, 0], [ 0, 0, 6, 0, 0, 0, 4, 0]]) And here is what I tried: from sklearn.preprocessing import MultiLabelBinarizer mlb = MultiLabelBinarizer() nn = mlb.fit_transform(labels_train) nn[:10] Output: array([[1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0], [1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0], [1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0], [1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0]]) And when I tried inverse_transform(): zet = mlb.inverse_transform(nn) zet[:10] out: [(0, 1, 2, 3, 4), (0, 1, 6), (0, 1, 3, 5), (0, 1, 4), (0, 1, 9), (0, 1, 6), (0, 2, 8), (0, 10), (0, 10), (0, 4, 6)] What am I doing wrong? Why is it showing unique values in ascending order?
Is it possible to read the content of a Binary File as Dictionary in Python?
I have raw data files in binary format. While I have written the code earlier in MATLAB, I wanted to transform the same into PYTHON. I have attached a couple of binary files. My MATLAB code for reading each of these files based on the file format specification document is as below: f1 = fopen('p580.004','r') if (f1 ~= -1) must = fread(f1,1,'int16'); internaltrigger = fread(f1,1,'int8'); internaldwell = fread(f1,1,'int8'); dwellunits = fread(f1,1,'int8'); check_dwell(dwellunits==0)=2; check_dwell(dwellunits==1)=0.002; check_dwell(dwellunits==2)=0.000002; check_dwell(dwellunits==3)=200; check_dwell(dwellunits>3)=NaN; check_dwell(dwellunits<0)=NaN; acqmode = fread(f1,1,'int8'); formatdwell = fread(f1,1,'int32'); passlength = fread(f1,1,'int16'); passcount = fread(f1,1,'int32'); passpreset = fread(f1,1,'int32'); starttime = fread(f1,8,'*char'); startdate = fread(f1,8,'*char'); markerchannel = fread(f1,1,'int16'); mcsno = fread(f1,1,'int8'); calibtype = fread(f1,1,'int8'); calibunit = fread(f1,4,'*char'); calibcoef0 = fread(f1,4,'int8'); calibcoef1 = fread(f1,4,'int8'); externaldwelthrevolt = fread(f1,4,'int8'); reserv1 = fread(f1,1,'int32'); reserv2 = fread(f1,1,'int8'); replacethesum = fread(f1,1,'int8'); identbyte = fread(f1,1,'*char'); progdwethrevolt = fread(f1,1,'int8'); detdeslength = fread(f1,1,'int8'); detdes = fread(f1,63,'*char'); samplength = fread(f1,1,'int8'); sampledes = fread(f1,63,'*char'); lindisflag = fread(f1,1,'int8'); linscale = fread(f1,1,'int8'); disstarchannel = fread(f1,2,'int8'); disnumchannels = fread(f1,1,'int16'); reser = fread(f1,10,'int8'); discse = fread(f1,1,'int8'); discedg = fread(f1,1,'int8'); discvolt = fread(f1,4,'int8'); scaupvolt =fread(f1,1,'float32'); scalolvolt = fread(f1,1,'float32'); dwelltime = fread(f1,1,'float32'); setconsflag = fread(f1,1,'int8'); reserve = fread(f1,13,'int8'); calibcoef2 = fread(f1,1,'float32'); calibcoef3 = fread(f1,1,'float32'); mcsid = fread(f1,8,'*char'); if (dwelltime == check_dwell) && (length(startdate)>=2) x1= fread(f1,1024,'int32'); % Photon Count if (isempty(x1) ~= 1) && (length(x1) == 1024) && (length(find(x1>0))>100) photoncount(1:1024) = x1; else photoncount(1:1024) = NaN; end %%%%%%% converting char array to string %%%%% startdate=cell2mat(cellstr(startdate)'); starttime=cell2mat(cellstr(starttime)'); %%%%%%% checking for date format %%%%% test=find(startdate == '-'); if (isempty(test) ~=1) day2=startdate(1,4:5); month2=startdate(1,1:2); year2=pfiles1(i).date(8:11); else if (strcmp(startdate,'') ~=1) day2=startdate(1,3:4); month2=startdate(1,1:2); year2=startdate(1,5:8); else day2=''; month2=''; year2=''; end end end In PYTHON, I wrote the following code: from array import array fname = 'p580.004' with open(fname,'rb') as file: pdata = array( 'B', file.read() ) # buffer the file The pdata output results the following: pdata = Out[1]: array('B', [252, 255, 1, 0, 0, 1, 2, 0, 0, 0, 0, 4, 212, 48, 0, 0, 212, 48, 0, 0, 48, 48, 58, 49, 52, 58, 51, 54, 48, 57, 50, 57, 50, 48, 49, 56, 75, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 70, 179, 63, 0, 0, 0, 0, 0, 1, 170, 1, 7, 77, 67, 66, 32, 49, 50, 49, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 225, 183, 15, 191, 255, 245, 31, 65, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 77, 67, 83, 112, 45, 48, 48, 49, 75, 0, 0, 0, 183, 0, 0, 0, 210, 0, 0, 0, 201, 0, 0, 0, 178, 0, 0, 0, 169, 0, 0, 0, 130, 0, 0, 0, 103, 0, 0, 0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0, 0, 4, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 6, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 3, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 3, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 1, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 4, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 3, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 4, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 4, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 4, 0, 0, 0, 1, 0, 0, 0, 3, 0, 0, 0]) If I use the following Python code: with open(fname,'rb') as f: # b is important -> binary fileContent = f.read() Now, the fileContent output gives the output in hexadecimal format. The following are my queries: Is there a way I can read the binary file as a Dictionary in Python? As you see the MATLAB code, there are different formatted variables e.g., int8, int16, int32, char, float32. How to handle these different formats with the Python code I wrote? I have tried to search internet but could not understand how to identify the format of the data in the binary file (assuming you don't have format specification information) and how to assign the actual values to the variables. Can anyone help me convert the MATLAB script to Python code for reading the binary file? Based on the suggestions, I have revised the code as below: fname = 'p580.004' with open(fname, 'rb') as fd: binbuf = fd.read() data_names = ('must','internaltrigger','internaldwell','dwellunits', \ 'acqmode','formatdwell','passlength','passcount','passpreset', \ 'starttime','startdate','markerchannel','mcsno','calibtype', \ 'calibunit','calibcoef0','calibcoef1','externaldwelthrevolt', \ 'reserv1','reserv2','replacethesum','identbyte', \ 'progdwethrevolt','detdeslength','detdes','samplength', \ 'sampledes','lindisflag','linscale','disstarchannel', \ 'disnumchannels','reser','discse','discedg','discvolt', \ 'scaupvolt','scalolvolt','dwelltime','setconstflag', \ 'reserve','calibcoef2','calibcoef3','mcsid','Ncount') data_tuple = struct.unpack_from('hbbbbihii8s8schbb4s4b4b4bibbcbb63pb63cbb2bh10bbb4bfffh13hff8c1024i', binbuf) The main issue I am facing is defining the format of the binary file. There is an inconsistency in the len(binbuf) and the struct.calcsize('hbbbbihii8s8schbb4s4b4b4bibbcbb63pb63cbb2bh10bbb4bfffh13hff8c1024i'). Can anyone suggest me where I am going wrong in the formatting based on the MATLAB code indicated above?
To read binary data into variable in python3, there is the struct module. It allows to specify how to interpret the bytes (e.g. unsigned long, signed char, etc.) and in what order by a format string. E.g. The format string 'hbb' in the call to struct.unpack_from() in the examples below means: 'h' : read a 16-bit signed int 'b' : then a 8-bit int 'b' : then another 8-bit int Quick example to read the first 3 of your variables from a binary file: import struct fname = 'p580.004' with open(fname, 'rb') as fd: binbuf = fd.read() must, internaltrigger, internaldwell = struct.unpack('hbb', binbuf) print('must:', must) print('internaltrigger:', internaltrigger) print('internaldwell:', internaldwell) Or, to read the same into a dictionary as you asked: import struct fname = 'p580.004' with open(fname, 'rb') as fd: binbuf = fd.read() data_names = ('must', 'internaltrigger', 'internaldwell' ) data_tuple = struct.unpack('hbb', binbuf) dictionary = dict(zip(data_names, data_tuple)) print(dictionary) For more about this: https://docs.python.org/3/library/struct.html
I could write the PYTHON code using numpy as below: import numpy as np dt=np.dtype([('must',np.int16), ('nternaltrigger',np.int8), ('internaldwell',np.int8), ('dwellunits',np.int8), ('acqmode',np.int8), ('formatdwell',np.int32), ('passlength',np.int16), ('passcount',np.int32), ('passpreset',np.int32), ('starttime','|S8'), ('startdate','|S8'), ('markerchannel',np.int16), ('mcsno',np.int8), ('calibtype',np.int8), ('calibunit','|S4'), ('calibcoef0','|S4'), ('calibcoef1','|S4'), ('externaldwelthrevolt',np.int8,(4,)), ('reserv1',np.int32), ('reserv2',np.int8), ('replacethesum',np.int8), ('identbyte','|S1'), ('progdwethrevolt',np.int8), ('detdeslength',np.int8), ('detdes','|S63'), ('samplength',np.int8), ('sampledes','|S63'), ('lindisflag',np.int8), ('linscale',np.int8), ('disstarchannel',np.int8,(2,)), ('disnumchannels',np.int16), ('reser',np.int8,(10,)), ('discse',np.int8), ('discedg',np.int8), ('discvolt',np.int8,(4,)), ('scaupvolt',np.float32), ('scalolvolt',np.float32), ('dwelltime',np.float32), ('setconsflag',np.int8), ('reserve',np.int8,(13,)), ('calibcoef2',np.float32), ('calibcoef3',np.float32), ('mcsid','|S8'), ('photocount',np.int32,(1024,))]) f=np.fromfile('p580.004',dtype=dt) d=dict(zip(f.dtype.names,f[0])) This works fine as required. Thanks everyone for the suggestion and initial help.
can't multiply matrix and list which type 'float'
I want to multiply matrix (transpose_test_feature) with a list (log_train_probs_nonspam_words). Matrix has a size of (2500 * 260) and list has a size of 2500 items. I want to multiply them. But I have this type error: TypeError: can't multiply sequence by non-int of type 'float' Is there anybody who can quickly help me? multiply_nonspam_test = [] for i in range(0, len(transpose_test_feature)): x = log_train_probs_nonspam_words[i] *transpose_test_feature[i] multiply_nonspam_test.append(x) print multiply_nonspam_test Input: log_train_probs_nonspam_words (sample): [-5.259440519499674, -5.5014525551182665, -6.226860597896433, -4.000908730923304, -6.970438632083271, -6.082121752251521, -6.7823864005803305, -4.22184233658671, -5.805134968916488, -6.28594951426644, -5.396092039460441, -4.935057080197465, -6.321351441317356, -6.054147900209116, -7.698677132454486, -6.71339352909338, -5.403260528939053, -5.454932539483374, -5.837924791739479, -9.239122173401634, -6.405908829345418, -8.83365706529347, -6.9200077784563785, -5.406864136442351, -6.089239220020385, -5.794439679799741, -5.6556032349455245, -7.8528278122817445, -4.863365151741348, -7.3673199965000435, -5.588463932107897, -7.293212024346322, -6.509093065580649, -5.605931625148287, -4.93280736046345, -6.674172815940098, -7.663585812643216, -5.918893854273146, -6.7542155236136345, -7.534374081163209, -6.6241623953654365, -5.095987447010102, -8.140509884733525, -6.2350910970329485, -5.287878454820207] transpose_test_feature (originally it is a 2500 * 260 matrix, I gave the first row of a matrix): [0, 1, 0, 0, 0, 0, 3, 0, 0, 0, 0, 1, 0, 0, 2, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 6, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 3, 1, 0, 2, 3, 0, 0, 2, 2, 0, 5, 1, 3, 0, 0, 3, 3, 0, 0, 1, 0, 1, 1, 1, 3, 0, 0, 3, 4, 1, 8, 0, 0, 0, 1, 3, 2, 0, 2, 2, 0, 0, 0, 1, 2, 0, 3, 2, 1, 0, 0, 1, 0, 3, 0, 0, 3, 0, 0, 0, 5, 0, 0, 0, 1, 0, 0, 2, 1, 1, 0, 18, 0, 10, 13, 3, 0, 2, 1, 7, 7, 0, 11, 8, 1, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 9, 0, 4, 0, 0, 0, 2, 0, 0, 1, 0, 0, 0, 0, 0, 42, 11, 5, 0, 19, 3, 0, 0, 1, 0, 0, 3, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 5, 3, 1, 8, 0, 0, 2, 3, 14, 0, 2, 0, 13, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 20, 2, 29, 29, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0]
You are multiplying a list by a float. You should multiply each row of the matrix with words. Something like this will work. multiply_nonspam_test = [] for row in transpose_test_feature: multiply_nonspam_test.append([x*y for x,y in zip(row, log_train_probs_nonspam_words)]) print multiply_nonspam_test
You can use NumPy to speed things up: import numpy as np a1 = np.array(log_train_probs_nonspam_words) a1 = a1.reshape(a1.shape[0], 1) a2 = np.array(transpose_test_feature) multiply_nonspam_test = a1 * a2
Scipy label erosion
How can I keep a ring of pixels around labeled regions in a numpy array? In a simple case, I'd subtract the erosion. That approach doesn't work when the labels touch. How can I get get B from A? A = array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0], [0, 0, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0], [0, 0, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0], [0, 0, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0], [0, 0, 2, 2, 2, 1, 1, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]) B = array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0], [0, 0, 2, 0, 0, 0, 0, 0, 2, 0, 0, 0], [0, 0, 2, 0, 0, 0, 0, 0, 2, 0, 0, 0], [0, 0, 2, 0, 0, 2, 2, 2, 2, 0, 0, 0], [0, 0, 2, 2, 2, 1, 1, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]) I'm working with large arrays with many labels, so separate erosions on each label isn't an option.
New Answer Actually, I just thought of a better way: B = A * (np.abs(scipy.ndimage.laplace(A)) > 0) As a full example: import numpy as np import scipy.ndimage A = np.array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0], [0, 0, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0], [0, 0, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0], [0, 0, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0], [0, 0, 2, 2, 2, 1, 1, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]) B = A * (np.abs(scipy.ndimage.laplace(A)) > 0) I think this should work in all cases (of "labeled" arrays like A, at any rate...). If you're worried about performance, you can split this into a few pieces to reduce memory overhead: B = scipy.ndimage.laplace(A) B = np.abs(B, B) # Preform abs in-place B /= B # This will produce a divide by zero warning that you can safely ignore B *= A This version is a lot more verbose, but should use much less memory. Old Answer I can't think of a good way to do it in one step with the usual scipy.ndimage functions. (I feel like a tophat filter should do what you want, but I can't quite figure it out.) However, doing several separate erosions is an option, as you mentioned. You should get reasonable performance even on very large arrays if you use find_objects to extract the subregion of each label, and then just do the erosion on the subregion. For example: import numpy as np import scipy.ndimage A = np.array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0], [0, 0, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0], [0, 0, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0], [0, 0, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0], [0, 0, 2, 2, 2, 1, 1, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]) regions = scipy.ndimage.find_objects(A) mask = np.zeros_like(A).astype(np.bool) for val, region in enumerate(regions, start=1): if region is not None: subregion = A[region] mask[region] = scipy.ndimage.binary_erosion(subregion == val) B = A.copy() B[mask] = 0 This yields: array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0], [0, 0, 2, 0, 0, 0, 0, 0, 2, 0, 0, 0], [0, 0, 2, 0, 0, 0, 0, 0, 2, 0, 0, 0], [0, 0, 2, 0, 0, 2, 2, 2, 2, 0, 0, 0], [0, 0, 2, 2, 2, 1, 1, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]) The performance should be reasonable for large arrays, but it's going to depend strongly on how large of an area the different labeled objects span and the number of labeled objects that you have....