473,503 Members | 756 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

grouping array

hi if I have an array

say x = [[2,2,0,0,1,1],
[1,1,0,0,1,1],
[1,1,0,0,1,1]]
I basically want to group regions that are non zero like I want to get
the coordinates of non zero regions..as (x1,y1,x2,y2)
[(0,0,2,1),(0,4,2,5)] which show the top left(x1,y1) and bottom
right(x2,y2) corners of each group.hope i am clear.

Thanks

Sep 29 '05 #1
8 1577
On 29 Sep 2005 10:01:40 -0700, pk******@gmail.com <pk******@gmail.com> wrote:
hi if I have an array

say x = [[2,2,0,0,1,1],
[1,1,0,0,1,1],
[1,1,0,0,1,1]]
I basically want to group regions that are non zero like I want to get
the coordinates of non zero regions..as (x1,y1,x2,y2)
[(0,0,2,1),(0,4,2,5)] which show the top left(x1,y1) and bottom
right(x2,y2) corners of each group.hope i am clear.


I don't understand. Could you give some inputs with expected outputs
and some explanation?

Peace
Bill Mill
bill.mill at gmail.com
Sep 29 '05 #2
sure:
basically I am looking for clustering of non zero groups in that 2D
list...so in the above array the first non zero cluster is 2,2 in row
0, 1,1 in row 1 and 1,1 in row 1 so if we think of this as one group we
have the first element of the group is at (0,0) in the list and last is
at (2,1) in the list so we have that group represented as (0,0,2,1)
similarly the second non group...and finally we get the result list
with the location of that group in the whole list as
[(0,0,2,1),(0,4,2,5)...]

hope I am clear.

Sep 29 '05 #3
pk******@gmail.com wrote:
hi if I have an array

say x = [[2,2,0,0,1,1],
[1,1,0,0,1,1],
[1,1,0,0,1,1]]
I basically want to group regions that are non zero like I want to get
the coordinates of non zero regions..as (x1,y1,x2,y2)
[(0,0,2,1),(0,4,2,5)] which show the top left(x1,y1) and bottom
right(x2,y2) corners of each group.hope i am clear.


given your definitions, neither (0, 0, 2, 1) nor (0, 4, 2, 5) are clusters
in your data. assuming that your description is wrong but your data is
correct, and your clusters are always this simple, here's a snippet that
does what I think you want:

x = [[2,2,0,0,1,1],
[1,1,0,0,1,1],
[1,1,0,0,1,1]]

# http://www.pythonware.com/products/pil/
import Image

h = len(x)
w = len(x[0])

data = []
for row in x:
data.extend(row)

im = Image.new("L", (w, h), None)
im.putdata(data)

def runlength(x):
out = []
u = 0
for i, v in enumerate(x):
if v:
if not u:
lo = i
elif u:
out.append((lo, i))
u = v
if u: out.append((lo, i+1))
return out

xx, yy = im.getprojection()

for y in runlength(yy):
y0, y1 = y
for x in runlength(xx):
x0, x1 = x
print (y0, x0, y1-1, x1-1)

</F>

Sep 29 '05 #4
1. why are you creating an Image object here? cant this be done by
handling lists?
2. What exactly is getprojection doing?

Sep 29 '05 #5
<pk******@gmail.com> wrote:
hi if I have an array

say x = [[2,2,0,0,1,1],
[1,1,0,0,1,1],
[1,1,0,0,1,1]]
I basically want to group regions that are non zero like I want to get
the coordinates of non zero regions..as (x1,y1,x2,y2)
[(0,0,2,1),(0,4,2,5)] which show the top left(x1,y1) and bottom
right(x2,y2) corners of each group.hope i am clear.

Thanks


I guess you imply rectangular regions only ? If not, what's the output supposed to be for

[[2,2,0,0,1,1],
[1,1,3,0,0,1],
[1,1,3,0,1,1]]

or

[[2,2,2,2],
[1,0,3,3],
[1,1,3,0]]

George
Sep 29 '05 #6
pk******@gmail.com wrote:
hi if I have an array

say x = [[2,2,0,0,1,1],
[1,1,0,0,1,1],
[1,1,0,0,1,1]]
I basically want to group regions that are non zero like I want to get
the coordinates of non zero regions..as (x1,y1,x2,y2)
[(0,0,2,1),(0,4,2,5)] which show the top left(x1,y1) and bottom
right(x2,y2) corners of each group.hope i am clear.

Thanks

How about this:
def getregions(grid):
"""Yield lists of adjancent points, not necessarily rectangular"""
adj = [(-1,0),(+1,0),(0,-1),(0,+1)] # horizontal and vertical adjacencies
# could add diagonals

points = set((y,x) for y, row in enumerate(grid)
for x, cell in enumerate(row)
if cell)

while points: # set of (y,x) non-zero points
region = [points.pop()] # start a new region with any remaining point
ptr = 0
while ptr < len(region):
y, x = region[ptr]
adjpoints = set((y + j, x + i) for j, i in adj)
adjpoints &= points # keep only the non-zero, unseen points
points -= adjpoints # remove these adjancencies from points
region.extend(adjpoints) # add them to the region
ptr += 1
yield region

def getregioncoords(grid):
"""Get top left and bottom right of *rectangular* regions"""
regions = getregions(grid)
return [(reg[0], reg[-1]) for reg in regions if reg.sort() or True]

x = [[2,2,0,0,1,1], ... [1,1,0,0,1,1],
... [1,1,0,0,1,1]]
...
... getregioncoords(x) [((0, 0), (2, 1)), ((0, 4), (2, 5))] x = [[1,0,1,0,1]]
getregioncoords(x) [((0, 0), (0, 0)), ((0, 2), (0, 2)), ((0, 4), (0, 4))] x = [[random.choice([0,1,2]) for x in range(6)] for y in range(6)]
pprint.pprint(x) [[1, 1, 2, 1, 2, 0],
[2, 0, 0, 2, 0, 1],
[1, 2, 2, 0, 2, 0],
[0, 1, 0, 0, 0, 0],
[2, 0, 0, 1, 1, 0],
[2, 2, 2, 0, 1, 0]] print "\n".join(str(reg) for reg in getregions(x)) [(0, 1), (0, 0), (0, 2), (1, 0), (0, 3), (2, 0), (1, 3), (0, 4), (2, 1), (3,
1), (2, 2)]
[(5, 4), (4, 4), (4, 3)]
[(5, 0), (5, 1), (4, 0), (5, 2)]
[(1, 5)]
[(2, 4)]


Unfortunately, it's rather slow. This one is much faster, using just one data
structure

def getregions2(grid):
"""Yield lists of adjancent points, not necessarily rectangular"""
adj = [(-1,0),(+1,0),(0,-1),(0,+1)] # horizontal and vertical adjacencies
# could add diagonals
rows = len(grid)
cols = len(grid[0])
griddata = []
for row in grid:
griddata.extend(row)
for y in xrange(rows):
ybase = y * cols
for x in xrange(cols):
if griddata[ybase + x]:
griddata[ybase + x] = 0
region = [(y, x)]
append = region.append
ptr = 0
while ptr < len(region):
y1, x1 = region[ptr]
for j, i in adj:
y2, x2 = y1 + j, x1 + i
if y2 < 0 or y2 == rows: continue
if x2 < 0 or x2 == cols: continue
if griddata[y2 * cols + x2]:
append((y2, x2))
griddata[y2 * cols + x2] = 0
ptr += 1
yield region

Michael

Sep 30 '05 #7
fredrick's solutions seems to be more closer to what I was looking
for.But I am still not sure if that could be done without the use of
Image module.
Also in your solution I cannot follow this
[[1, 1, 2, 1, 2, 0],
[2, 0, 0, 2, 0, 1],
[1, 2, 2, 0, 2, 0],
[0, 1, 0, 0, 0, 0],
[2, 0, 0, 1, 1, 0],
[2, 2, 2, 0, 1, 0]]
print "\n".join(str(reg) for reg in getregions(x))

[(0, 1), (0, 0), (0, 2), (1, 0), (0, 3), (2, 0), (1, 3), (0, 4), (2,
1), (3,
1), (2, 2)]
[(5, 4), (4, 4), (4, 3)]
[(5, 0), (5, 1), (4, 0), (5, 2)]
[(1, 5)]
[(2, 4)]
This is kind of confusing...could you please correlate the grid to the
result and explain

Sep 30 '05 #8
pk******@gmail.com wrote:
fredrick's solutions seems to be more closer to what I was looking
for.But I am still not sure if that could be done without the use of
Image module.
What do you mean by "closer to what I was looking
for"? For the single test case you provided:
say x = [[2,2,0,0,1,1],
[1,1,0,0,1,1],
[1,1,0,0,1,1]]
I basically want to group regions that are non zero like I want to get
the coordinates of non zero regions..as (x1,y1,x2,y2)
[(0,0,2,1),(0,4,2,5)] which show the top left(x1,y1) and bottom
right(x2,y2) corners of each group.hope i am clear.
my solution provides the correct output:
x = [[2,2,0,0,1,1], ... [1,1,0,0,1,1],
... [1,1,0,0,1,1]]
...
... getregioncoords(x) [((0, 0), (2, 1)), ((0, 4), (2, 5))]

* except that the points aren't flattened. If that's important to you, rewrite
getregioncoords as follows:

def getregioncoords(grid):
"""Get top left and bottom right of *rectangular* regions"""
regions = getregions(grid)
return [reg[0]+reg[-1] for reg in regions if reg.sort() or True]
getregioncoords(x) [(0, 0, 2, 1), (0, 4, 2, 5)]

Also in your solution I cannot follow this
I broke the solution into two parts:

1) the getregions generator yields a list of all the contiguous regions. The
output below is the lists of coordinates that are contiguous non-zero cells in
the grid.
[[1, 1, 2, 1, 2, 0],
[2, 0, 0, 2, 0, 1],
[1, 2, 2, 0, 2, 0],
[0, 1, 0, 0, 0, 0],
[2, 0, 0, 1, 1, 0],
[2, 2, 2, 0, 1, 0]] >>> print "\n".join(str(reg) for reg in getregions(x))

[(0, 1), (0, 0), (0, 2), (1, 0), (0, 3), (2, 0), (1, 3), (0, 4), (2,
1), (3,
1), (2, 2)]
[(5, 4), (4, 4), (4, 3)]
[(5, 0), (5, 1), (4, 0), (5, 2)]
[(1, 5)]
[(2, 4)]

2) If the regions are rectangular, the getregioncoords functions returns the
coordinates of the top-left and bottom-right points. You did not answer the
previous post which asked what to do if the regions were not rectangular.

HTH

Michael

Sep 30 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2854
by: amber | last post by:
Hello, I have a report in VB.NET/Crystal Reports. I have a criteria form that users select between 2 different types of grouping (group by category or group by year). Can I programmatically...
2
1686
by: Andreas Håkansson | last post by:
Seeing how my previous post seem to have fallen between the cracks, I thought I would have a second, more direct, go at it. So my question is "Is it possible to group (Muenchian method) over...
3
2712
by: ahaque38 | last post by:
Hello. Using A2K SP3, I am having the following problem with a report using "Sorting and Grouping". I have recently added a grouping in the reports for "Category2<>'CONTRACTS'". I have...
8
3486
by: Mike MacSween | last post by:
tblCourses one to many to tblEvents. A course may have an intro workshop (a type of event), a mid course workshop, a final exam. Or any combination. Or something different in the future. At...
7
2670
by: maffonso | last post by:
Hi guys, My table has a memo field. At the end of the month a run a report and I would like to concatenate the memo fields in a unique field by dept. Im thinking doing this through totals button...
4
1559
by: mantrid | last post by:
I have records from a database that are extracted with php and displayed in a table. Data in some of the fields is replicated eg something1,item1,somethingelse1 something1,item1,somethingelse2...
0
1212
by: Corey | last post by:
hello, I’m trying to run a query and I’m getting error messages Can anyone help me get though this problem? 1: Tried this and got this error message ORA-00923 , decode...
0
1523
by: Roman Bertle | last post by:
Hello, I try to format monetary values using the locale module, python2.5: Python 2.5.2a0 (r251:54863, Jan 3 2008, 17:59:56) on linux2 Type "help", "copyright", "credits" or "license" for...
1
5660
by: Sandeep Singh | last post by:
Hi, How to do group by in XSLT ? I tried on the following codes: <files> <file name="swablr.eps" size="4313" project="mars"/> <file name="batboy.wks" size="424" ...
1
1496
by: filippo nanni | last post by:
Hello, I have a new question about organizing multidimensional arrays. I have an array containing several others; my aim is to obtain a new array, which groups together those arrays from the first...
0
7203
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7334
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6993
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5579
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3156
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1514
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
737
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.