-
Notifications
You must be signed in to change notification settings - Fork 2
/
models.py
96 lines (75 loc) · 3.33 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"""
Database schema.
References:
- https://docs.djangoproject.com/en/2.1/ref/models/fields/
- https://docs.djangoproject.com/en/2.1/ref/models/options/
## Export schematic
Run the following script in the environment to generate figures in model-schema:
```shell
bash media/models-schema.sh
```
"""
from django.contrib.postgres.fields import JSONField
from django.db import models
class Metanode(models.Model):
identifier = models.CharField(primary_key=True, max_length=50)
abbreviation = models.CharField(max_length=10)
n_nodes = models.PositiveIntegerField()
class Node(models.Model):
id = models.IntegerField(primary_key=True)
metanode = models.ForeignKey(to='Metanode', on_delete=models.PROTECT)
identifier = models.CharField(max_length=50)
identifier_type = models.CharField(max_length=50, choices=[
('str', 'string'),
('int', 'integer'),
])
name = models.CharField(max_length=200)
properties = JSONField(db_column='data') # Migrate database field in future
class Meta:
# unique_together implies index_together in postgres
# https://stackoverflow.com/a/42676612/4651668
unique_together = ('metanode', 'identifier')
def get_cast_identifier(self):
import builtins
caster = getattr(builtins, self.identifier_type)
return caster(self.identifier)
class Metapath(models.Model):
abbreviation = models.CharField(primary_key=True, max_length=20)
name = models.CharField(max_length=200)
source = models.ForeignKey(to='Metanode', on_delete=models.PROTECT, related_name='metapath_source')
target = models.ForeignKey(to='Metanode', on_delete=models.PROTECT, related_name='metapath_target')
length = models.PositiveSmallIntegerField()
path_count_density = models.FloatField()
path_count_mean = models.FloatField()
path_count_max = models.PositiveIntegerField()
dwpc_raw_mean = models.FloatField()
n_similar = models.PositiveIntegerField()
p_threshold = models.FloatField()
class DegreeGroupedPermutation(models.Model):
metapath = models.ForeignKey(to='Metapath', on_delete=models.PROTECT)
source_degree = models.PositiveIntegerField()
target_degree = models.PositiveIntegerField()
n_dwpcs = models.BigIntegerField()
n_nonzero_dwpcs = models.BigIntegerField()
nonzero_mean = models.FloatField()
nonzero_sd = models.FloatField()
class Meta:
unique_together = ('metapath', 'source_degree', 'target_degree')
class PathCount(models.Model):
metapath = models.ForeignKey(to='Metapath', on_delete=models.PROTECT)
source = models.ForeignKey(to='Node', on_delete=models.PROTECT, related_name='path_source')
target = models.ForeignKey(to='Node', on_delete=models.PROTECT, related_name='path_target')
dgp = models.ForeignKey(to='DegreeGroupedPermutation', on_delete=models.PROTECT)
path_count = models.PositiveIntegerField()
dwpc = models.FloatField(
verbose_name='degree-weighted path count with damping exponent of 0.5'
)
p_value = models.FloatField(null=True)
class Meta:
unique_together = ('metapath', 'source', 'target')
indexes = [
models.Index(fields=['source', 'target']),
]
def get_adjusted_p_value(self):
"""Return Bonferroni adjusted p-value."""
return min(1.0, self.p_value * self.metapath.n_similar)