Commit 9cf629cd authored by delanoe's avatar delanoe

[GRAPH EXPLORER] Better types to check parameters and give user some usefull...

[GRAPH EXPLORER] Better types to check parameters and give user some usefull informations. I extensively use the Either type of Haskell that is needed to easyly understand the logic of the process.
parent a79375ae
...@@ -27,6 +27,15 @@ def get_graph( request=None , corpus=None ...@@ -27,6 +27,15 @@ def get_graph( request=None , corpus=None
): ):
''' '''
Get_graph : main steps: Get_graph : main steps:
0) Check the parameters
get_graph :: GraphParameters -> Either (Dic Nodes Links) (Dic State Length)
where type Length = Int
get_graph first checks the parameters and return either graph data or a dic with
state "type" with an integer to indicate the size of the parameter
(maybe we could add a String in that step to factor and give here the error message)
1) count Cooccurrences (function countCooccurrences) 1) count Cooccurrences (function countCooccurrences)
main parameters: threshold main parameters: threshold
...@@ -54,11 +63,11 @@ def get_graph( request=None , corpus=None ...@@ -54,11 +63,11 @@ def get_graph( request=None , corpus=None
if mapList_id is None: if mapList_id is None:
mapList_id = session.query(Node.id).filter(Node.typename == "MAPLIST").first()[0] mapList_id = session.query(Node.id).filter(Node.typename == "MAPLIST").first()[0]
mapList_size = session.query(NodeNgram).filter(NodeNgram.node_id == mapList_id) mapList_size_query = session.query(NodeNgram).filter(NodeNgram.node_id == mapList_id)
mapList_size = mapList_size_query.count()
if mapList_size.count() < graph_constraints['mapList']: if mapList_size < graph_constraints['mapList']:
# Do not compute the graph if mapList is not big enough # Do not compute the graph if mapList is not big enough
return {'nodes':[mapList_size.count()], 'links':[0,0,0]} return {'state': "mapListError", "length" : mapList_size}
# case of corpus not big enough # case of corpus not big enough
...@@ -103,7 +112,8 @@ def get_graph( request=None , corpus=None ...@@ -103,7 +112,8 @@ def get_graph( request=None , corpus=None
# Finally test if the size of the corpora is big enough # Finally test if the size of the corpora is big enough
# -------------------------------- # --------------------------------
if corpus_size_query.count() > graph_constraints['corpusMax']: corpus_size = corpus_size_query.count()
if corpus_size > graph_constraints['corpusMax']:
# Then compute cooc asynchronously with celery # Then compute cooc asynchronously with celery
scheduled(countCooccurrences)( corpus_id=corpus.id scheduled(countCooccurrences)( corpus_id=corpus.id
#, field1="ngrams", field2="ngrams" #, field1="ngrams", field2="ngrams"
...@@ -113,13 +123,13 @@ def get_graph( request=None , corpus=None ...@@ -113,13 +123,13 @@ def get_graph( request=None , corpus=None
, save_on_db = True , save_on_db = True
#, limit=size #, limit=size
) )
# Dic hack to inform user that graph is computed asynchronously # Dic to inform user that corpus maximum is reached then
# (Impossible graph: no nodes with one link) # graph is computed asynchronously
return {'nodes':[], 'links':[0]} return {"state" : "corpusMax", "length" : corpus_size}
elif corpus_size_query.count() <= graph_constraints['corpusMin']: elif corpus_size <= graph_constraints['corpusMin']:
# Do not compute the graph if corpus is not big enough # Do not compute the graph if corpus is not big enough
return {'nodes':[corpus_size_query.count()], 'links':[0,0]} return {"state" : "corpusMin", "length" : corpus_size}
else: else:
# If graph_constraints are ok then compute the graph in live # If graph_constraints are ok then compute the graph in live
......
...@@ -142,10 +142,12 @@ class Graph(APIView): ...@@ -142,10 +142,12 @@ class Graph(APIView):
) )
# Test data length # data :: Either (Dic Nodes Links) (Dic State Length)
# A graph needs more than 3 nodes and 3 links
# Others graphs are used to check the errors # data_test :: Either String Bool
if len(data['nodes']) > 3 and len(data['links']) > 3 : data_test = data.get("state", True)
if data_test is True:
# normal case -------------------------------- # normal case --------------------------------
if format_ == 'json': if format_ == 'json':
return JsonHttpResponse( return JsonHttpResponse(
...@@ -154,7 +156,9 @@ class Graph(APIView): ...@@ -154,7 +156,9 @@ class Graph(APIView):
) )
# -------------------------------------------- # --------------------------------------------
elif len(data['nodes']) == 1 and len(data['links']) == 2 : else:
# All other cases (more probable are higher in the if list)
if data["state"] == "corpusMin":
# async data case # async data case
return JsonHttpResponse({ return JsonHttpResponse({
'msg': '''Problem: your corpus is too small (only %d documents). 'msg': '''Problem: your corpus is too small (only %d documents).
...@@ -164,14 +168,14 @@ class Graph(APIView): ...@@ -164,14 +168,14 @@ class Graph(APIView):
You can manage your corpus here: You can manage your corpus here:
http://%sgargantext.org/projects/%d/ http://%sgargantext.org/projects/%d/
''' % ( data['nodes'][0] ''' % ( data["length"]
, graph_constraints['corpusMin'] , graph_constraints['corpusMin']
, "dev." , "dev."
, corpus.parent_id , corpus.parent_id
), ),
}, status=400) }, status=400)
elif len(data['nodes']) == 1 and len(data['links']) == 3 : elif data["state"] == "mapListError":
# async data case # async data case
return JsonHttpResponse({ return JsonHttpResponse({
'msg': '''Problem: your map list is too small (currently %d terms). 'msg': '''Problem: your map list is too small (currently %d terms).
...@@ -181,7 +185,7 @@ class Graph(APIView): ...@@ -181,7 +185,7 @@ class Graph(APIView):
You can manage your map terms here: You can manage your map terms here:
http://%sgargantext.org/projects/%d/corpora/%d/terms http://%sgargantext.org/projects/%d/corpora/%d/terms
''' % ( data['nodes'][0] ''' % ( data["length"]
, graph_constraints['mapList'] , graph_constraints['mapList']
, "dev." , "dev."
, corpus.parent_id , corpus.parent_id
...@@ -190,21 +194,27 @@ class Graph(APIView): ...@@ -190,21 +194,27 @@ class Graph(APIView):
}, status=400) }, status=400)
elif len(data['nodes']) == 0 and len(data['links']) == 0 : elif data["state"] == "corpusMax":
# async data case # async data case
return JsonHttpResponse({ return JsonHttpResponse({
'msg': '''Warning: Async graph generation. 'msg': '''Warning: Async graph generation since your corpus is
big (about %d documents).
Wait a while and discover your graph very soon. Wait a while and discover your graph very soon.
Click on the link and see your current graph Click on the link below and see your current graph
processing on top of the list: processing on top of the list:
http://%sgargantext.org/projects/%d/corpora/%d/myGraph http://%sgargantext.org/projects/%d/corpora/%d/myGraphs
''' % ("dev.", corpus.parent_id, corpus.id), ''' % (data["length"], "dev.", corpus.parent_id, corpus.id),
}, status=400)
else :
return JsonHttpResponse({
'msg': '''Programming error.''',
}, status=400) }, status=400)
else:
elif len(data["nodes"]) < 2 and len(data["links"]) < 2:
# empty data case # empty data case
return JsonHttpResponse({ return JsonHttpResponse({
'msg': '''Empty graph warning 'msg': '''Empty graph warning
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment