Commit 8cc92549 authored by delanoe's avatar delanoe

[FEAT] Temporal Graph implemented. Ok.

parent 7814f5b8
...@@ -5,7 +5,7 @@ from .nodes import Node ...@@ -5,7 +5,7 @@ from .nodes import Node
import datetime import datetime
__all__ = ['NodeHyperdata'] __all__ = ['NodeHyperdata', 'HyperdataKey']
class classproperty(object): class classproperty(object):
......
from gargantext.models import Node, Ngram, NodeNgram, NodeNgramNgram, \ from gargantext.models import Node, Ngram, NodeNgram, NodeNgramNgram, \
NodeHyperdata NodeHyperdata, HyperdataKey
from gargantext.util.db import session, aliased, bulk_insert, func from gargantext.util.db import session, aliased, bulk_insert, func
from gargantext.util.lists import WeightedMatrix, UnweightedList, Translations from gargantext.util.lists import WeightedMatrix, UnweightedList, Translations
...@@ -21,7 +21,7 @@ def countCooccurrences( corpus=None ...@@ -21,7 +21,7 @@ def countCooccurrences( corpus=None
For the moment list of paramters are not supported because, lists need to For the moment list of paramters are not supported because, lists need to
be merged before. be merged before.
corpus :: Corpus corpus :: Corpus
mapList_id :: Int mapList_id :: Int
groupList_id :: Int groupList_id :: Int
...@@ -32,10 +32,10 @@ def countCooccurrences( corpus=None ...@@ -32,10 +32,10 @@ def countCooccurrences( corpus=None
''' '''
# TODO : add hyperdata here # TODO : add hyperdata here
# Security test # Security test
field1,field2 = str(field1), str(field2) field1,field2 = str(field1), str(field2)
# Get node # Get node
if not coocNode_id: if not coocNode_id:
coocNode_id0 = ( session.query( Node.id ) coocNode_id0 = ( session.query( Node.id )
...@@ -56,20 +56,20 @@ def countCooccurrences( corpus=None ...@@ -56,20 +56,20 @@ def countCooccurrences( corpus=None
coocNode_id = coocNode.id coocNode_id = coocNode.id
else : else :
coocNode_id = coocNode_id[0] coocNode_id = coocNode_id[0]
if reset == True : if reset == True :
session.query( NodeNgramNgram ).filter( NodeNgramNgram.node_id == coocNode_id ).delete() session.query( NodeNgramNgram ).filter( NodeNgramNgram.node_id == coocNode_id ).delete()
session.commit() session.commit()
NodeNgramX = aliased(NodeNgram) NodeNgramX = aliased(NodeNgram)
# Simple Cooccurrences # Simple Cooccurrences
cooc_score = func.count(NodeNgramX.node_id).label('cooc_score') cooc_score = func.count(NodeNgramX.node_id).label('cooc_score')
# A kind of Euclidean distance cooccurrences # A kind of Euclidean distance cooccurrences
#cooc_score = func.sqrt(func.sum(NodeNgramX.weight * NodeNgramY.weight)).label('cooc_score') #cooc_score = func.sqrt(func.sum(NodeNgramX.weight * NodeNgramY.weight)).label('cooc_score')
if isMonopartite : if isMonopartite :
NodeNgramY = aliased(NodeNgram) NodeNgramY = aliased(NodeNgram)
...@@ -89,7 +89,7 @@ def countCooccurrences( corpus=None ...@@ -89,7 +89,7 @@ def countCooccurrences( corpus=None
) )
else : else :
NodeNgramY = aliased(NodeNgram) NodeNgramY = aliased(NodeNgram)
cooc_query = (session.query( NodeHyperdataNgram.ngram_id cooc_query = (session.query( NodeHyperdataNgram.ngram_id
, NodeNgramY.ngram_id , NodeNgramY.ngram_id
, cooc_score , cooc_score
...@@ -142,17 +142,13 @@ def countCooccurrences( corpus=None ...@@ -142,17 +142,13 @@ def countCooccurrences( corpus=None
# TODO : more complexe date format here. # TODO : more complexe date format here.
date_start = datetime.datetime.strptime (str(start), "%Y-%m-%d") date_start = datetime.datetime.strptime (str(start), "%Y-%m-%d")
date_start_utc = date_start.strftime("%Y-%m-%d %H:%M:%S") date_start_utc = date_start.strftime("%Y-%m-%d %H:%M:%S")
Start=aliased(NodeHyperdata) Start=aliased(NodeHyperdata)
StartFormat = aliased(Hyperdata)
cooc_query = (cooc_query.join( Start cooc_query = (cooc_query.join( Start
, Start.node_id == Node.id , Start.node_id == Node.id
) )
.join( StartFormat .filter( Start.key == 'publication_date')
, StartFormat.id == Start.hyperdata_id .filter( Start.value_utc >= date_start_utc)
)
.filter( StartFormat.name == 'publication_date')
.filter( Start.value_datetime >= date_start_utc)
) )
...@@ -160,26 +156,23 @@ def countCooccurrences( corpus=None ...@@ -160,26 +156,23 @@ def countCooccurrences( corpus=None
# TODO : more complexe date format here. # TODO : more complexe date format here.
date_end = datetime.datetime.strptime (str(end), "%Y-%m-%d") date_end = datetime.datetime.strptime (str(end), "%Y-%m-%d")
date_end_utc = date_end.strftime("%Y-%m-%d %H:%M:%S") date_end_utc = date_end.strftime("%Y-%m-%d %H:%M:%S")
End=aliased(NodeHyperdata) End=aliased(NodeHyperdata)
EndFormat = aliased(Hyperdata)
cooc_query = (cooc_query.join( End cooc_query = (cooc_query.join( End
, End.node_id == Node.id , End.node_id == Node.id
) )
.join( EndFormat .filter( End.key == 'publication_date')
, EndFormat.id == End.hyperdata_id .filter( End.value_utc <= date_end_utc )
)
.filter( EndFormat.name == 'publication_date' )
.filter( End.value_datetime <= date_end_utc )
) )
if isMonopartite: if isMonopartite:
# Cooc is symetric, take only the main cooccurrences and cut at the limit # Cooc is symetric, take only the main cooccurrences and cut at the limit
cooc_query = cooc_query.filter(NodeNgramX.ngram_id < NodeNgramY.ngram_id) cooc_query = cooc_query.filter(NodeNgramX.ngram_id < NodeNgramY.ngram_id)
cooc_query = cooc_query.having(cooc_score > threshold) cooc_query = cooc_query.having(cooc_score > threshold)
if isMonopartite: if isMonopartite:
cooc_query = cooc_query.group_by(NodeNgramX.ngram_id, NodeNgramY.ngram_id) cooc_query = cooc_query.group_by(NodeNgramX.ngram_id, NodeNgramY.ngram_id)
else: else:
...@@ -192,6 +185,6 @@ def countCooccurrences( corpus=None ...@@ -192,6 +185,6 @@ def countCooccurrences( corpus=None
mapList = UnweightedList( mapList_id ) mapList = UnweightedList( mapList_id )
group_list = Translations ( groupList_id ) group_list = Translations ( groupList_id )
cooc = matrix & (mapList * group_list) cooc = matrix & (mapList * group_list)
cooc.save(coocNode_id) cooc.save(coocNode_id)
return(coocNode_id) return(coocNode_id)
...@@ -94,10 +94,10 @@ ...@@ -94,10 +94,10 @@
<!-- FIXME a pop up for advanced mode of graphs --!> <!-- FIXME a pop up for advanced mode of graphs --!>
<a type="button" class="btn btn-default <a type="button" class="btn btn-default
{% if view == "conditional" %}active{%endif%}" {% if view == "conditional" %}active{%endif%}"
href="/projects/{{project.id}}/corpora/{{ corpus.id }}/explorer?field1=ngrams&amp;field2=ngrams&amp;distance=conditional&amp;bridgeness=5">Graphs (Conditional)</a> data-url="/projects/{{project.id}}/corpora/{{ corpus.id }}/explorer?field1=ngrams&amp;field2=ngrams&amp;distance=conditional&amp;bridgeness=5" onclick='gotoexplorer(this)' >Graphs (Conditional)</a>
<a type="button" class="btn btn-default <a type="button" class="btn btn-default
{% if view == "distributional" %}active{%endif%}" {% if view == "distributional" %}active{%endif%}"
href="/projects/{{project.id}}/corpora/{{ corpus.id }}/explorer?field1=ngrams&amp;field2=ngrams&amp;distance=distributional&amp;bridgeness=5">Graphs (Distributional)</a> data-url="/projects/{{project.id}}/corpora/{{ corpus.id }}/explorer?field1=ngrams&amp;field2=ngrams&amp;distance=distributional&amp;bridgeness=5" onclick='gotoexplorer(this)' >Graphs (Distributional)</a>
<!-- <!--
<a type="button" class="btn btn-default <a type="button" class="btn btn-default
{% if view == "journalTerms" %}active{%endif%}" {% if view == "journalTerms" %}active{%endif%}"
...@@ -198,6 +198,37 @@ ...@@ -198,6 +198,37 @@
});}); });});
</script> </script>
<script type="text/javascript">
function gotoexplorer(elem) {
var url_ = $(elem).data("url")
if (TheBuffer==false)
return window.open(url_,'_blank');
var current_timerange = TheBuffer
var time_limits = [new Date(oldest[0],oldest[1]-1,oldest[2]), new Date(latest[0],latest[1]-1,latest[2] ) ];
time_limits[0] = new Date(time_limits[0].setDate(time_limits[0].getDate()-1) );
time_limits[1] = new Date(time_limits[1].setDate(time_limits[1].getDate()+1) );
if( ( +current_timerange[0]===+time_limits[0] ) && ( +current_timerange[1]===+time_limits[1] ) ) {
url_ = url_ // rien
} else {
var start__ = new Date(current_timerange[0].setDate(current_timerange[0].getDate()+1) );
var end__ = new Date(current_timerange[1].setDate(current_timerange[1].getDate()-1) );
var start_ = start__.getFullYear()+"-"+(start__.getMonth()+1)+"-"+(start__.getDay()+1)
var end_ = end__.getFullYear()+"-"+(end__.getMonth()+1)+"-"+(end__.getDay()+1)
url_ += "&start=" + start_ + "&end="+end_;
// url_ += "&start=" + start__.getFullYear() + "&end="+end__.getFullYear();
}
return window.open(url_,'_blank');
}
</script>
{% if debug == False %} {% if debug == False %}
<!-- Piwik --> <!-- Piwik -->
<script type="text/javascript"> <script type="text/javascript">
...@@ -222,3 +253,7 @@ ...@@ -222,3 +253,7 @@
</body> </body>
</html> </html>
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