Building a summary statistic over edges connected between 2 vertices and store it an existing edge with the corresponding vertices.

26 views
Skip to first unread message

Olav Laudy

unread,
Feb 17, 2018, 2:16:25 PM2/17/18
to Gremlin-users


Hi,



See the attached image for a simplified version of my network. The red edges contain a count of the green edges that connect the same nodes. This was achieved by a very smart query (designed by Daniel Kuppitz in an earlier post):


g.V().hasLabel('object1').as('a').
  map(out('obs1').groupCount()).
  unfold().as('x').select(keys).
  addE('summary').
    from('a').
  property('count', select('x').select(values))


Next step is to collect some other summary measures of the green edges and add those to the existing red edge. For example, in the green edges there's a field 'relativeStrength'  which I would like to average and add to the red edge for each pair. 
My approach: I start at the red edge, look at the from and two side and then identify the corresponding green edges and summarize them. 

submit("g.E().hasLabel('red').as('x') 
         .project('a','b')  \
           .by(outV().outE('green'))  \
           .by(inV()) \
         .select('a').inV().inE('red').as('e').where('e',eq('x')).by()  

This results in an empty selection.

or even when I try to summarize the green edges, while keeping track the connected sides, I fail:

g.E().hasLabel('green').has('relativeStrength')
         .group() 
            .by(values('relativeStrength').mean())  
            .by(__.inV())  
            .by(__.outV())  
          
this query runs with the mean and either one of the connected vertices, but not both.



Appreciate your thoughts!


Olav






Daniel Kuppitz

unread,
Feb 17, 2018, 2:51:21 PM2/17/18
to gremli...@googlegroups.com
Untested, but I'm quite confident that this is going to work:

g.V().as('o').
  outE('red').as('re').inV().
  map(inE('green').
        where(outV().as('o')).
      values('relativeStrength').mean()).as('rs').
  select('re').
    property('avgRelStrength', select('rs'))

Cheers,
Daniel


--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/d21543e2-8122-468d-a498-562287a00aba%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Olav Laudy

unread,
Feb 20, 2018, 10:35:07 AM2/20/18
to gremli...@googlegroups.com
Daniel, thank you so much!

what I don't get from this query is the following:

g.V().as('o').
  outE('red').as('re').inV().
  map(inE('green').
        where(outV().as('o')).
      values('relativeStrength').mean()).as('rs').
  select('re').
    property('avgRelStrength', select('rs'))


In my attempts to get this working, I've been trying to use outV().hasId(select('o').id()) or similar.

How can this work with 'just' the as statement?


Thanks!


Olav



To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/CA%2Bf9seV%2Bf71v%3DJgVX5C8U1%3D2agq1%3DYUa5kUjGska_3mCUZuLeg%40mail.gmail.com.

Olav Laudy

unread,
Feb 20, 2018, 10:35:07 AM2/20/18
to gremli...@googlegroups.com
btw, this is the demonstration that is indeed does work as advertised:

4 objects:

g.addV('o').property('name','o1')
g.addV('o').property('name','o2')
g.addV('o').property('name','o3')
g.addV('o').property('name','o4')

4 relationships between the objects:

g.V().has('o','name','o1').as('o').V().has('o','name','o2').addE('red').from('o')
g.V().has('o','name','o1').as('o').V().has('o','name','o4').addE('red').from('o')
g.V().has('o','name','o3').as('o').V().has('o','name','o2').addE('red').from('o')
g.V().has('o','name','o3').as('o').V().has('o','name','o4').addE('red').from('o')

for every relationship, 2 links with easy verifiable means:

g.V().has('o','name','o1').as('o').V().has('o','name','o2').addE('green').from('o').property('relS',1)
g.V().has('o','name','o1').as('o').V().has('o','name','o2').addE('green').from('o').property('relS',2)

g.V().has('o','name','o1').as('o').V().has('o','name','o4').addE('green').from('o').property('relS',10)
g.V().has('o','name','o1').as('o').V().has('o','name','o4').addE('green').from('o').property('relS',20)

g.V().has('o','name','o3').as('o').V().has('o','name','o2').addE('green').from('o').property('relS',100)
g.V().has('o','name','o3').as('o').V().has('o','name','o2').addE('green').from('o').property('relS',200)

g.V().has('o','name','o3').as('o').V().has('o','name','o4').addE('green').from('o').property('relS',1000)
g.V().has('o','name','o3').as('o').V().has('o','name','o4').addE('green').from('o').property('relS',2000)

the query:

g.V().hasLabel('o').as('o')  
        .outE('red').as('re').inV()  
        .map(inE('green')  
            .where(outV().as('o'))  
        .values('relS').mean()).as('rs')  
        .select('re').property('avgRelStrength', select('rs'))

The result: 

['Edge:',
 {'id': '8cb0d378-8bd6-adf8-9889-cb17cd0d5f2f',
  'inV': '1eb0d378-7bad-21ac-a8d0-bef91e9caaab',
  'inVLabel': 'o',
  'label': 'red',
  'outV': 'b0b0d378-7b57-77a4-a5b3-784b16fe7126',
  'outVLabel': 'o',
  'properties': {'avgRelStrength': 1500.0}},
 'Edge:',
 {'id': '08b0d378-8b5e-eb2b-4883-3e7ae188f0e8',
  'inV': 'a2b0d378-7afd-17db-30a6-c0f1ac0845c2',
  'inVLabel': 'o',
  'label': 'red',
  'outV': 'b0b0d378-7b57-77a4-a5b3-784b16fe7126',
  'outVLabel': 'o',
  'properties': {'avgRelStrength': 150.0}},
 'Edge:',
 {'id': '80b0d378-8aec-de78-b0ea-c1e59a322e00',
  'inV': '1eb0d378-7bad-21ac-a8d0-bef91e9caaab',
  'inVLabel': 'o',
  'label': 'red',
  'outV': '36b0d378-7aa4-2ac1-a4a4-9c1e17ba8384',
  'outVLabel': 'o',
  'properties': {'avgRelStrength': 15.0}},
 'Edge:',
 {'id': '4eb0d378-8a53-2a5e-a7aa-007163f3ec7c',
  'inV': 'a2b0d378-7afd-17db-30a6-c0f1ac0845c2',
  'inVLabel': 'o',
  'label': 'red',
  'outV': '36b0d378-7aa4-2ac1-a4a4-9c1e17ba8384',
  'outVLabel': 'o',
  'properties': {'avgRelStrength': 1.5}}]
In [ ]:









Daniel Kuppitz

unread,
Feb 20, 2018, 10:46:19 AM2/20/18
to gremli...@googlegroups.com
Hmm, I see how this can easily be overlooked in the docs. There's actually just one example that shows that (number 2 in http://tinkerpop.apache.org/docs/3.3.1/reference/#using-where-with-match).
If the inner traversal of where() starts or ends with .as(stepLabel), then the label will be matched against existing labels in the surrounding traversal.

.where(bla().as('x'))         == .filter(bla().where(eq('x')))
.where(as('x').bla())         == .where(eq('x')).filter(bla())
.where(as('x').bla().as('y')) == .where(eq('x')).filter(bla().where(eq('y')))

Cheers,
Daniel


To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-users+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-users+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/CAO%3DOAKL2_Q-OCjkR857Y%3D2FqZa2ph8oBvZDKa-3KibdzJMejZg%40mail.gmail.com.

Olav Laudy

unread,
Feb 20, 2018, 11:15:01 AM2/20/18
to gremli...@googlegroups.com
Super cool!!!

To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/CA%2Bf9seWVSjwPTdXF%2BEB7uHvWNS1ZmW3qE64sBa5t3nzT2NmDjQ%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages