Search code examples
pythonpyglet

Visualizing large amount of data using Pyglet


I have some larger csv files containing data recorded by a radar at sea. The radar shows the received echo for 2048 different angles. Each angle can show data for 512 different ranges. For each range the intensity of the echo is stored as a value between 0 and 255.

In order to visualize the data I try to use Pyglet with batch rendering. Once the first rotation (2048 angles) have been displayed, the program should continue through the data, showing the newest recordings. Therefore, every time I display an angle, I try to delete the next angle from the vertexlist, that it won't draw on top of the previous rotation.

As there are a potential of 2048*512 readings per rotation, I have made a 2D list 0f 2048 by 512 to store the vertexlists, such that I'm able to delete the specific types. See the following code snippet.

import pyglet

win = pyglet.window.Window(1000, 1000)
batch = pyglet.graphics.Batch()

def update(dt):
    stepSize = 10000 # Save the first 10000 measurements in the batch before drawing it
    for i in range(Counter.cnt, Counter.cnt + stepSize):

        (* Calculate the pixel positions of each measurements from the angle and range *)

        # Add to batch and save in 2D list
        arr[angle][currentRange] = batch.add(2, pyglet.gl.GL_LINES, None,
                  ('v2f', (pts[0][0], pts[0][1], pts[1][0], pts[1][1])))

        # Delete everything from the next angle
        for n in range(0, len(arr[nextAngle])):
            try:
                arr[nextAngle][n].delete()
            except:
                pass

@win.event
def on_draw():
    win.clear()
    batch.draw()

if __name__ == '__main__':
    pyglet.clock.schedule_interval(update, 1/120.0)
    pyglet.app.run()

The measurements should maybe have been drawn as polygons or triangles, but since the angles are so close I have used lines.

This gives me a pretty good result for the first couple of rotations:

Radar_pyglet1

The blobs represent the surrounding islands at the sea. However after some rotations the program becomes slower and the display looks as such:

Radar_pyglet2

At this image the boat has rotated and therefore the position of the islands in the picture have also changed. However, as can be seen on the picture, after rotations a lot of smaller dots are drawn on the display, which aren't supposed to be there. Furthermore, parts of the measurements are partly erased almost at random.

What could the problem be here, and what could be a better way to display the data?


Solution

  • I'm still not completely sure what causes the problem, but i have found a workaround.

    Instead of using VertexList.delete(), I change the color of the vertexList to black:

    # Change color if vertixList exists, otherwise create new 
    if arr[angle][currentRange != 0:
        arr[angle][currentRange].colors = [255, 255, 255, 255, 255, 255]
    else:
        arr[angle[currentRange] = batch.add(2, pyglet.gl.GL_LINES, None,
            ('v2f', (pts[0][0], pts[0][1], pts[1][0], pts[1][1])),
            ('c3B', (255, 255, 255, 255, 255, 255)))
    
    # Change color of the next angle
    for n in range(0, len(arr[nextAngle])):
        try:
            arr[nextAngle][n].colors = [0, 0, 0, 0, 0, 0]
        except:
            pass
    

    Using this I don't get the same problem.