Skip to content
This repository has been archived by the owner on Mar 29, 2024. It is now read-only.

Python: a small bug? + best practice in counting number of elements equal to specific value #40

Closed
renoust opened this issue May 22, 2018 · 1 comment

Comments

@renoust
Copy link

renoust commented May 22, 2018

Hi,

I just wondered, is there a way cleaner to quickly count the number of elements equal to a certain value of properties than the following?
Ideally, the API would propose something like "numberOfNodes/EdgesEqualTo" in the similar manner as "getNodes/EdgesEqualTo".

I made a small benchmarking on a small graph (default web import on labri.fr + degrees, 159 nodes / 287 edges), here is the code (I'd like to count all nodes of degree 2):

from tulip import tlp
import time

def timeit(method):
    def timed(*args, **kw):
        ts = time.time()
        result = method(*args, **kw)
        te = time.time()
        if 'log_time' in kw:
            name = kw.get('log_name', method.__name__.upper())
            kw['log_time'][name] = int((te - ts) * 1000)
        else:
            print '%r  %2.2f ms' % \
                  (method.__name__, (te - ts) * 1000)
        return result
    return timed

@timeit
def count_equal_to_with_enumerate(graph):
  nodes = graph['viewMetric'].getNodesEqualTo(2)
  for i, n in enumerate(nodes):
    continue
  print i+1
  
@timeit
def count_equal_to_with_iteration(graph):
  nodes = graph['viewMetric'].getNodesEqualTo(2)
  i = 0
  for n in nodes:
    i += 1    
  print i
  
@timeit
def count_equal_to_with_default_value(graph):
  viewMetric = graph['viewMetric']
  default = viewMetric.getNodeDefaultValue()
  viewMetric.setNodeDefaultValue(2)
  i = graph.numberOfNodes()-viewMetric.numberOfNonDefaultValuatedNodes()
  viewMetric.setNodeDefaultValue(default)
  print i


def main(graph): 
  count_equal_to_with_enumerate(graph)
  count_equal_to_with_iteration(graph)
  count_equal_to_with_default_value(graph)

The output is:

42
'count_equal_to_with_enumerate'  6.22 ms
42
'count_equal_to_with_iteration'  0.76 ms
0
'count_equal_to_with_default_value'  0.21 ms

The last method is by far the fastest, but unfortunately does not work, or I made a mistake maybe.
Cheers,

@p-mary
Copy link
Contributor

p-mary commented May 24, 2018

Thanks for your input.
The result for the last method pointed out that there was a bug in PropertyInterface::set[Node|Edge]DefaultValue(val).
It has been fixed in commit 1b77ef1.
However this method is certainly the worse to count the number of elements because these are enumerated twice and this involves numerous updates of the property storage structures.
So, the second method is certainly the best.

@p-mary p-mary closed this as completed May 24, 2018
p-mary referenced this issue in anlambert/talipot Jan 3, 2020
QOpenGL module is marked as deprecated since a while now so it is time
to remove its use from the Talipot codebase and promote the use of
QOpenGL* classes directly integrated in the QtGui module.

The big difference between QOpenGL and QtOpenGL from Qt5 is that all
rendering is performed in framebuffer objects, there is no more direct
rendering in the underlying os windows with its own OpenGL context.

Talipot OpenGL rendering also follows that idiom, all renderings are performed
offscreen using a shared OpenGL context. This also means that there is no
more QGLWidget as viewport for QGraphicsView. Talipot OpenGL scene are
now converted to QImage in order to display them using the default Qt raster
rendering engine. This should fixes the numerous rendering glitches observed
on MacOS.

First thing observed after the migration is a consequent performance boost
in OpenGL rendering when using an Intel GPU on a Linux host machine (especially
when selecting elements, it is now 10 times faster on debian stable).
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants