0

I'm looking for a solution that would help me do the following.

Suppose I have to generate HTML code using hand-drawn design. I got element type, x, y coordination width, and height as NumPy array. I need to sort these arrays according to the y coordination if the two elements are in the same value then sort according to the y value. Then I need to group the elements in the same type.

I created array like this :

import numpy
s = numpy.array([
          #element type, x,y,width,height
          ["hyperlink", 5, 150, 25, 10],
          ["paragraph", 20, 60, 10, 10],
          ["image", 85, 150, 25, 10],
          ["radio", 20, 60, 10, 10],
          ["radio", 85, 150, 25, 10],
          ["button", 20, 60, 10, 10],
          ["text_field", 20, 40, 25, 10],  
          ["label", 10, 10, 20, 10]])

print(s)
lists = s.tolist()
print([{"element_type ": x[0] ,  "x": x[1], "y": x[2], "width":x[3], "height" :x[4]} for i, x in enumerate(lists)])

I'd like to sort it such that my points are ordered by y-coordinate, and then by x in cases where their coordinate is the same.

Then I need to group the elements such as two radio buttons as in the above example. I expected output as follows.

{
'element_type ': 'hyperlink',
 'group_id' :"1",
 'x': '5', 
 'y': '150', 
 'width': '25', 
 'height': '10'
},
    
{
'element_type ': 'image',
'group_id' :"3", 
'x': '85', 
'y': '150', 
'width': '25', 
'height': '10'
}, 

{'element_type ': 'radio', 
'group_id' :"4",
'x': '20', 
'y': '60', 
'width': '10', 
'height': '10'
}, 

{
'element_type ': 'radio',
'group_id' :"4", 
'x': '85', 
'y': '150', 
'width': '25', 
'height': '10'
}, 

 {
'element_type ': 'text_field', 
'group_id' :"5",
'x': '20', 
'y': '40', 
'width': '25', 
'height': '10'
}, 

{'element_type ': 'label', 
**group_id :"5",** 
'x': '10', 
'y': '10', 
'width': '20', 
'height': '10'
}]

Can I get an idea for this? I used python.

mathfux
  • 5,759
  • 1
  • 14
  • 34

1 Answers1

0

numpy is not designed for groupby operations. Except the case you force it. pandas is a better option here.

import pandas as pd
df = pd.DataFrame(s, columns = ['element_type', 'x', 'y', 'width', 'height'])
df['group_id'] = df.groupby(['x', 'y', 'width', 'height']).ngroup()
>>> df.to_dict('records')

[{'element type': 'hyperlink',
  'x': '5',
  'y': '150',
  'width': '25',
  'height': '10',
  'group_id': 3},
 ...,
 {'element type': 'label',
  'x': '10',
  'y': '10',
  'width': '20',
  'height': '10',
  'group_id': 0}]
mathfux
  • 5,759
  • 1
  • 14
  • 34
  • Thank you. Can you give an idea as to if this group is according to following rules only? If it is a radio button, the check box needs to be grouped, and the radio and check box needs to be in the same x values. because if not there can be grouped radio buttons in the two places. and another rule is needed to group labels with relevant radio buttons, check boxes, and text fields. when I grouped it as this I saw it is incorrectly grouped. hyperlink also grouped. It is not I expected. – Nadeeshani Welgama Nov 28 '21 at 16:21