-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmodels.py
224 lines (168 loc) · 7.65 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
from django.contrib.auth.models import AbstractBaseUser
from django.contrib.auth.models import BaseUserManager
from django.contrib.auth.models import PermissionsMixin
from django.db import models
from django.utils.translation import gettext_lazy as _
class UserManager(BaseUserManager):
"""
A custom user manager to deal with emails as unique identifiers for auth
instead of usernames. The default that's used is "UserManager"
"""
def _create_user(self, email, password, **extra_fields):
"""
Creates and saves a User with the given email and password.
"""
if not email:
raise ValueError('The Email must be set')
email = self.normalize_email(email)
user = self.model(email=email, **extra_fields)
user.set_password(password)
user.save()
return user
def create_superuser(self, email, password, **extra_fields):
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
extra_fields.setdefault('is_active', True)
if extra_fields.get('is_staff') is not True:
raise ValueError('Superuser must have is_staff=True.')
if extra_fields.get('is_superuser') is not True:
raise ValueError('Superuser must have is_superuser=True.')
return self._create_user(email, password, **extra_fields)
def get_by_natural_key(self, username):
# __iexact is a filter that gives a case-insensitive representation of a field.
# Permits mismatched casing for usernames.
case_insensitive_username_field = '{}__iexact'.format(self.model.USERNAME_FIELD)
return self.get(**{case_insensitive_username_field: username})
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(unique=True, null=True)
display_name = models.CharField(max_length=300, null=True)
is_staff = models.BooleanField(
_('staff status'),
default=False,
help_text=_('Designates whether the user is a staff member.'),
)
is_active = models.BooleanField(
_('active'),
default=False,
help_text=_(
'Designates whether this user should be treated as active.'
'Unselect this instead of deleting accounts.'
),
)
USERNAME_FIELD = 'email'
objects = UserManager()
def get_full_name(self):
return self.display_name
def get_short_name(self):
return self.display_name
def natural_key(self):
return self.email,
def __repr__(self):
return generic_repr(self)
class Meta:
db_table = 'brainscore_user'
def generic_repr(obj):
return obj.__class__.__name__ \
+ "[" + ",".join(f"{field}={value}" for field, value in vars(obj).items()) + "]"
class Reference(models.Model):
author = models.CharField(max_length=300)
year = models.IntegerField()
url = models.CharField(max_length=1000, unique=True)
bibtex = models.TextField()
def __repr__(self):
return generic_repr(self)
class ReferenceManager(models.Manager):
def get_by_natural_key(self, url):
return self.get(url=url)
objects = ReferenceManager()
class Meta:
db_table = 'brainscore_reference'
class BenchmarkType(models.Model):
identifier = models.CharField(max_length=200, primary_key=True)
domain = models.CharField(max_length=200, default="vision")
reference = models.ForeignKey(Reference, on_delete=models.PROTECT, null=True) # null for parents
order = models.IntegerField(default=999)
parent = models.ForeignKey("self", null=True, on_delete=models.PROTECT) # null: average benchmark has no parent
visible = models.BooleanField(default=False, null=False)
def __repr__(self):
return generic_repr(self)
class Meta:
db_table = 'brainscore_benchmarktype'
class BenchmarkMeta(models.Model):
number_of_stimuli = models.IntegerField(null=True)
number_of_recording_sites = models.IntegerField(null=True)
recording_sites = models.CharField(max_length=100, null=True)
behavioral_task = models.CharField(max_length=100, null=True)
class Meta:
db_table = 'brainscore_benchmarkmeta'
class BenchmarkInstance(models.Model):
benchmark_type = models.ForeignKey(BenchmarkType, on_delete=models.PROTECT)
version = models.IntegerField()
ceiling = models.FloatField(default=0, null=True)
ceiling_error = models.FloatField(null=True)
meta = models.ForeignKey(BenchmarkMeta, null=True, on_delete=models.PROTECT)
def __repr__(self):
return generic_repr(self)
def natural_key(self):
return self.benchmark_type.identifier, self.version
class BenchmarkInstanceManager(models.Manager):
def get_by_natural_key(self, identifier, version):
return self.get(benchmark_type__identifier=identifier, version=version)
objects = BenchmarkInstanceManager()
class Meta:
unique_together = (('benchmark_type', 'version'),)
db_table = 'brainscore_benchmarkinstance'
class Submission(models.Model):
class Status:
PENDING = 'running'
SUCCESS = 'successful'
FAILURE = 'failure'
submitter = models.ForeignKey(User, on_delete=models.PROTECT)
timestamp = models.DateTimeField(auto_now_add=True, blank=True)
model_type = models.CharField(max_length=30, default='BaseModel')
status = models.CharField(max_length=25, default='unknown')
# equivalent to ID until language changes were added: (ID 6756)
jenkins_id = models.IntegerField()
def __repr__(self):
return generic_repr(self)
class Meta:
db_table = 'brainscore_submission'
class Model(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=200)
owner = models.ForeignKey(User, on_delete=models.PROTECT)
reference = models.ForeignKey(Reference, on_delete=models.PROTECT, null=True) # null for models without publication
submission = models.ForeignKey(Submission, on_delete=models.PROTECT, null=True) # null for self-run models
visual_degrees = models.IntegerField(null=True) # null during model creation before querying objec
public = models.BooleanField(default=False)
competition = models.CharField(max_length=200, default=None, null=True)
domain = models.CharField(max_length=200, default="vision")
def __repr__(self):
return generic_repr(self)
def natural_key(self):
return self.name, self.owner.natural_key()
class ModelManager(models.Manager):
def get_by_natural_key(self, name, owner=None):
kwargs = dict(name=name)
if owner is not None: # if owner is passed, explicitly use it to link, otherwise try without
kwargs['owner__email'] = owner
return self.get(**kwargs)
objects = ModelManager()
class Meta:
db_table = 'brainscore_model'
class Score(models.Model):
benchmark = models.ForeignKey(BenchmarkInstance, on_delete=models.PROTECT)
model = models.ForeignKey(Model, on_delete=models.PROTECT)
score_raw = models.FloatField(default=0, null=True)
score_ceiled = models.FloatField(default=0, null=True)
error = models.FloatField(default=0, null=True)
start_timestamp = models.DateTimeField(blank=True)
end_timestamp = models.DateTimeField(auto_now_add=True, blank=True, null=True)
comment = models.CharField(max_length=1000, null=True)
def __repr__(self):
return generic_repr(self)
def natural_key(self):
return self.benchmark.natural_key(), self.model.natural_key()
natural_key.dependencies = ['benchmarks.BenchmarkInstance', 'benchmarks.model']
class Meta:
db_table = 'brainscore_score'