Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
gargantext
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
humanities
gargantext
Commits
05f9197a
Commit
05f9197a
authored
Mar 26, 2016
by
delanoe
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[FEAT] A graph happens.
parent
f64dd8ce
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
82 additions
and
58 deletions
+82
-58
urls.py
gargantext/urls.py
+3
-1
distance.py
graphExplorer/distance.py
+16
-21
functions.py
graphExplorer/functions.py
+54
-29
rest.py
graphExplorer/rest.py
+9
-7
No files found.
gargantext/urls.py
View file @
05f9197a
...
...
@@ -38,7 +38,9 @@ urlpatterns = [
url
(
r'^projects/(\d+)/corpora/(\d+)/documents/(\d+)/$'
,
annotations_main_view
),
# Module "Graph Explorer"
url
(
r'^projects/(\d+)/corpora/(\d+)/explorer$'
,
explorer
)
url
(
r'^projects/(\d+)/corpora/(\d+)/explorer$'
,
explorer
),
url
(
r'^projects/(\d+)/corpora/(\d+)/graph$'
,
Graph
.
as_view
()),
url
(
r'^projects/(\d+)/corpora/(\d+)/node_link.json$'
,
Graph
.
as_view
())
#url(r'^projects/(\d+)/corpora/(\d+)/explorer$', include(graphExplorer.urls))
#url(r'^projects/(\d+)/corpora/(\d+)/explorer$', include(graphExplorer_urls))
]
graphExplorer/distance.py
View file @
05f9197a
from
gargantext.util.db
import
session
from
gargantext.models
import
Node
,
NodeNgram
,
NodeNgramNgram
,
\
NodeHyperdata
from
gargantext.util.db
import
session
,
aliased
from
graphExplorer.louvain
import
best_partition
from
copy
import
copy
from
collections
import
defaultdict
from
operator
import
itemgetter
from
django.db
import
connection
,
transaction
from
math
import
log
,
sqrt
#from operator import itemgetter
import
math
from
math
import
log
,
sqrt
import
numpy
as
np
import
pandas
as
pd
from
copy
import
copy
import
numpy
as
np
import
pandas
as
pd
import
networkx
as
nx
from
networkx.readwrite
import
json_graph
from
graphExplorer.louvain
import
best_partition
,
generate_dendogram
,
partition_at_level
from
sqlalchemy.orm
import
aliased
def
do_distance
(
cooc_id
,
field1
=
None
,
field2
=
None
,
isMonopartite
=
True
,
distance
=
'conditional'
):
'''
...
...
@@ -40,14 +35,14 @@ def do_distance(cooc_id, field1=None, field2=None, isMonopartite=True, distance=
query
=
session
.
query
(
Cooc
)
.
filter
(
Cooc
.
node_id
==
cooc_id
)
.
all
()
for
cooc
in
query
:
matrix
[
cooc
.
ngram
x_id
][
cooc
.
ngramy_id
]
=
cooc
.
score
matrix
[
cooc
.
ngram
y_id
][
cooc
.
ngramx_id
]
=
cooc
.
score
matrix
[
cooc
.
ngram
1_id
][
cooc
.
ngram2_id
]
=
cooc
.
weight
matrix
[
cooc
.
ngram
2_id
][
cooc
.
ngram1_id
]
=
cooc
.
weight
ids
[
cooc
.
ngram
x_id
]
=
(
field1
,
cooc
.
ngramx
_id
)
ids
[
cooc
.
ngram
y_id
]
=
(
field2
,
cooc
.
ngramy
_id
)
ids
[
cooc
.
ngram
1_id
]
=
(
field1
,
cooc
.
ngram1
_id
)
ids
[
cooc
.
ngram
2_id
]
=
(
field2
,
cooc
.
ngram2
_id
)
weight
[
cooc
.
ngram
x_id
]
=
weight
.
get
(
cooc
.
ngramx_id
,
0
)
+
cooc
.
score
weight
[
cooc
.
ngram
y_id
]
=
weight
.
get
(
cooc
.
ngramy_id
,
0
)
+
cooc
.
score
weight
[
cooc
.
ngram
1_id
]
=
weight
.
get
(
cooc
.
ngram1_id
,
0
)
+
cooc
.
weight
weight
[
cooc
.
ngram
2_id
]
=
weight
.
get
(
cooc
.
ngram2_id
,
0
)
+
cooc
.
weight
x
=
pd
.
DataFrame
(
matrix
)
.
fillna
(
0
)
...
...
graphExplorer/functions.py
View file @
05f9197a
# Gargantext lib
from
gargantext.util.db
import
session
from
gargantext.util.http
import
JsonHttpResponse
from
gargantext.models
import
Node
,
NodeNgram
,
NodeNgramNgram
#from gargantext.util.toolchain.ngram_coocs import compute_coocs
from
graphExplorer.distance
import
do_distance
from
graphExplorer.cooccurrences
import
do_cooc
# Prelude lib
from
copy
import
copy
,
deepcopy
from
collections
import
defaultdict
from
sqlalchemy.orm
import
aliased
from
copy
import
copy
,
deepcopy
from
collections
import
defaultdict
from
sqlalchemy.orm
import
aliased
# Math/Graph lib
import
math
import
pandas
as
pd
import
numpy
as
np
import
networkx
as
nx
from
math
import
log
from
networkx.readwrite
import
json_graph
# Gargantext lib
from
gargantext.util.http
import
JsonHttpResponse
from
gargantext.util.toolchain.ngram_coocs
import
compute_coocs
from
graphExplorer.distance
import
do_distance
import
networkx
as
nx
from
networkx.readwrite
import
json_graph
def
get_cooc
(
request
=
None
,
corpus
=
None
,
field1
=
'ngrams'
,
field2
=
'ngrams'
,
cooc_id
=
None
,
type
=
'node_link'
,
start
=
None
,
end
=
None
,
hapax
=
1
,
threshold
=
1
,
distance
=
'conditional'
,
size
=
1000
,
bridgeness
=
5
,
mainList_id
=
None
,
stopList_id
=
None
):
'''
get_ccoc : to compute the graph.
'''
# implicit global session
data
=
{}
#if session.query(Node).filter(Node.type_id==type_cooc_id, Node.parent_id==corpus_id).first() is None:
print
(
"Cooccurrences do not exist yet, creating it."
)
miam_id
=
1
stop_id
=
2
group_id
=
3
if
stopList_id
==
None
:
stopList_id
=
(
session
.
query
(
Node
.
id
)
.
filter
(
Node
.
typename
==
"STOPLIST"
,
Node
.
parent_id
==
corpus
.
id
)
.
first
())
if
stopList_id
==
None
:
raise
ValueError
(
"STOPLIST node needed for mainlist creation"
)
if
mainList_id
==
None
:
stopList_id
=
(
session
.
query
(
Node
.
id
)
.
filter
(
Node
.
typename
==
"STOPLIST"
,
Node
.
parent_id
==
corpus
.
id
)
.
first
())
if
not
mainList_id
==
None
:
raise
ValueError
(
"STOPLIST node needed for mainlist creation"
)
# compute_cooc needs group, fields etc.
# group_id = 3
SamuelFlag
=
False
# if field1 == field2 == 'ngrams' :
...
...
@@ -49,9 +68,17 @@ def get_cooc( request=None, corpus=None
# data deleted each time
#cooc_id = get_or_create_node(nodetype='Cooccurrence', corpus=corpus).id
cooc_id
=
compute_cooc
(
corpus
,
field1
=
"ngrams"
,
field2
=
"ngrams"
,
miam_id
=
miam_id
,
group_id
=
group_id
,
stop_id
=
stop_id
,
limit
=
size
,
isMonopartite
=
True
,
start
=
start
,
end
=
end
,
hapax
=
hapax
)
if
corpus
is
None
:
corpus
=
session
.
query
(
Node
)
.
filter
(
Node
.
id
==
corpus_id
)
.
first
()
cooc_id
=
do_cooc
(
corpus
=
corpus
#, field1="ngrams", field2="ngrams"
,
mainList_id
=
mainList_id
,
stopList_id
=
stopList_id
#, group_id=group_id
#, isMonopartite=True
,
start
=
start
,
end
=
end
,
threshold
=
threshold
#, limit=size
)
G
,
partition
,
ids
,
weight
=
do_distance
(
cooc_id
,
field1
=
"ngrams"
,
field2
=
"ngrams"
,
isMonopartite
=
True
,
distance
=
distance
)
...
...
@@ -95,21 +122,19 @@ def get_cooc( request=None, corpus=None
weight
=
G
[
ids
[
s
][
1
]][
ids
[
t
][
1
]][
"weight"
]
if
bridgeness
<
0
:
info
=
{
"s"
:
ids
[
s
][
1
]
,
"t"
:
ids
[
t
][
1
]
,
"w"
:
weight
}
info
=
{
"s"
:
ids
[
s
][
1
]
,
"t"
:
ids
[
t
][
1
]
,
"w"
:
weight
}
links
.
append
(
info
)
else
:
if
partition
[
s
]
==
partition
[
t
]:
info
=
{
"s"
:
ids
[
s
][
1
]
,
"t"
:
ids
[
t
][
1
]
,
"w"
:
weight
}
info
=
{
"s"
:
ids
[
s
][
1
]
,
"t"
:
ids
[
t
][
1
]
,
"w"
:
weight
}
links
.
append
(
info
)
if
bridgeness
>
0
:
...
...
graphExplorer/rest.py
View file @
05f9197a
...
...
@@ -2,13 +2,15 @@ from gargantext.util.http import APIView, APIException, JsonHttpResponse
#from rest_framework.authentication import SessionAuthentication, BasicAuthentication
from
gargantext.util.db
import
session
from
gargantext.models.nodes
import
Node
from
graphExplorer.functions
import
get_cooc
# TODO check authentication
class
Graph
(
APIView
):
#authentication_classes = (SessionAuthentication, BasicAuthentication)
def
get
(
self
,
request
,
corpus_id
):
def
get
(
self
,
request
,
project_id
,
corpus_id
):
'''
Graph.get :: Get graph data as REST api.
Get all the parameters first
...
...
@@ -39,18 +41,18 @@ class Graph(APIView):
if
field1
in
accepted_field1
:
if
field2
in
accepted_field2
:
if
start
is
not
None
and
end
is
not
None
:
data
=
compute_cooc
(
corpus
data
=
get_cooc
(
corpus
=
corpus
#, field1=field1 , field2=field2
,
start
=
start
,
end
=
end
,
threshold
=
threshold
,
distance
=
distance
,
start
=
start
,
end
=
end
,
threshold
=
threshold
,
distance
=
distance
)
else
:
data
=
compute_cooc
(
corpus
data
=
get_cooc
(
corpus
=
corpus
#, field1=field1, field2=field2
,
threshold
=
threshold
,
distance
=
distance
,
bridgeness
=
bridgeness
)
,
bridgeness
=
bridgeness
)
if
format_
==
'json'
:
return
JsonHttpResponse
(
data
)
else
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment