cancel
Showing results forΒ 
Search instead forΒ 
Did you mean:Β 

Correct syntax for GDS call in Python

andy_hegedus
Graph Voyager

Hi,

I'm trying to replicate some of the working browser GDS code into Python script for automation purposes.

I am working on basic graph create functionality.
Within browser this works:

CALL gds.graph.create.cypher(
    "self_cite",
    "MATCH (n:patent)-[:Assigned_to]-(a:company) WHERE a.name='applied materials' RETURN id(n) AS id, labels(n) AS labels",
    "MATCH (a:patent)-[:Cites]-(b:patent) return id(a) AS source, id(b) AS target",
    {validateRelationships: FALSE}
);

I am using the Neo4J python driver and working with a Jupyter notebook.

Here is what I have:

def make_self_cite(tx):
    result = tx.run("CALL gds.graph.create.cypher('self_cite', "
                    "'MATCH (n:patent)-[:Assigned_to]-(a:company) WHERE a.name='applied materials' RETURN id(n) AS id, labels(n) AS labels', "
                    "'MATCH (a:patent)-[:Cites]-(b:patent) return id(a) AS source, id(b) AS target', {validateRelationships: FALSE});")

with driver.session() as session:
    selftest = session.write_transaction(make_self_cite)
driver.close()    

And I am getting
CypherSyntaxError: {code: Neo.ClientError.Statement.SyntaxError} {message: Invalid input 'p': expected 'n/N' (line 1, column 103 (offset: 102))
And trying to figure out the correct syntax.
I think that puts it at the where clause where I define 'applied materials' . What should be the correct syntax. And as extra credit how would I parameterize the 'applied materials' part to further allow automation?
Andy

1 REPLY 1

andy_hegedus
Graph Voyager

Ok
Got the first part figured out by backslash quoting.

def make_self_cite(tx):
    result = tx.run("CALL gds.graph.create.cypher('self_cite', "
                    "\"MATCH (n:patent)-[:Assigned_to]-(a:company) WHERE a.name=\'applied materials\' RETURN id(n) AS id, labels(n) AS labels\", "
                    "'MATCH (a:patent)-[:Cites]-(b:patent) return id(a) AS source, id(b) AS target', {validateRelationships: FALSE});")

with driver.session() as session:
    selftest = session.write_transaction(make_self_cite)
driver.close()    


Andy