Commit 1759a477 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 595e8682
...@@ -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,64 +156,72 @@ class Graph(APIView): ...@@ -154,64 +156,72 @@ class Graph(APIView):
) )
# -------------------------------------------- # --------------------------------------------
elif len(data['nodes']) == 1 and len(data['links']) == 2 :
# async data case
return JsonHttpResponse({
'msg': '''Problem: your corpus is too small (only %d documents).
Solution: Add more documents (more than %d documents)
in order to get a graph.
You can manage your corpus here:
http://%sgargantext.org/projects/%d/
''' % ( data['nodes'][0]
, graph_constraints['corpusMin']
, "dev."
, corpus.parent_id
),
}, status=400)
elif len(data['nodes']) == 1 and len(data['links']) == 3 :
# async data case
return JsonHttpResponse({
'msg': '''Problem: your map list is too small (currently %d terms).
Solution: Add some terms (more than %d terms)
in order to get a graph.
You can manage your map terms here:
http://%sgargantext.org/projects/%d/corpora/%d/terms
''' % ( data['nodes'][0]
, graph_constraints['mapList']
, "dev."
, corpus.parent_id
, corpus.id
),
}, status=400)
elif len(data['nodes']) == 0 and len(data['links']) == 0 :
# async data case
return JsonHttpResponse({
'msg': '''Warning: Async graph generation.
Wait a while and discover your graph very soon.
Click on the link and see your current graph
processing on top of the list:
http://%sgargantext.org/projects/%d/corpora/%d/myGraph
''' % ("dev.", corpus.parent_id, corpus.id),
}, status=400)
else: else:
# empty data case # All other cases (more probable are higher in the if list)
return JsonHttpResponse({ if data["state"] == "corpusMin":
'msg': '''Empty graph warning # async data case
No cooccurences found in this corpus for the words of this maplist return JsonHttpResponse({
(maybe add more terms to the maplist or increase the size of your 'msg': '''Problem: your corpus is too small (only %d documents).
corpus ?)''',
}, status=400) Solution: Add more documents (more than %d documents)
in order to get a graph.
You can manage your corpus here:
http://%sgargantext.org/projects/%d/
''' % ( data["length"]
, graph_constraints['corpusMin']
, "dev."
, corpus.parent_id
),
}, status=400)
elif data["state"] == "mapListError":
# async data case
return JsonHttpResponse({
'msg': '''Problem: your map list is too small (currently %d terms).
Solution: Add some terms (more than %d terms)
in order to get a graph.
You can manage your map terms here:
http://%sgargantext.org/projects/%d/corpora/%d/terms
''' % ( data["length"]
, graph_constraints['mapList']
, "dev."
, corpus.parent_id
, corpus.id
),
}, status=400)
elif data["state"] == "corpusMax":
# async data case
return JsonHttpResponse({
'msg': '''Warning: Async graph generation since your corpus is
big (about %d documents).
Wait a while and discover your graph very soon.
Click on the link below and see your current graph
processing on top of the list:
http://%sgargantext.org/projects/%d/corpora/%d/myGraphs
''' % (data["length"], "dev.", corpus.parent_id, corpus.id),
}, status=400)
else :
return JsonHttpResponse({
'msg': '''Programming error.''',
}, status=400)
elif len(data["nodes"]) < 2 and len(data["links"]) < 2:
# empty data case
return JsonHttpResponse({
'msg': '''Empty graph warning
No cooccurences found in this corpus for the words of this maplist
(maybe add more terms to the maplist or increase the size of your
corpus ?)''',
}, status=400)
else: else:
# parameters error case # parameters error case
......
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