/
database_models.py
150 lines (114 loc) · 4.65 KB
/
database_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
from peewee import Proxy, Model as PeeweeModel, CharField, ForeignKeyField, IntegerField, BooleanField, DateTimeField, \
FloatField, TextField, PrimaryKeyField
database_proxy = Proxy()
class PeeweeBase(PeeweeModel):
@classmethod
def set_schema(cls, schema_name):
for model in cls.__subclasses__():
model._meta.schema = schema_name
class Meta:
database = database_proxy
class User(PeeweeBase):
id = PrimaryKeyField()
email = CharField(index=True, null=True)
is_active = BooleanField()
is_staff = BooleanField()
is_superuser = BooleanField()
last_login = DateTimeField(null=True)
password = CharField()
class Meta:
table_name = 'brainscore_user'
class Reference(PeeweeBase):
author = CharField()
bibtex = TextField()
url = CharField()
year = IntegerField()
class Meta:
table_name = 'brainscore_reference'
class BenchmarkType(PeeweeBase):
identifier = CharField(primary_key=True)
reference = ForeignKeyField(column_name='reference_id', field='id', model=Reference, null=True)
order = IntegerField()
parent = ForeignKeyField(column_name='parent_id', field='identifier', model='self', null=True)
visible = BooleanField(default=False, null=False)
domain = CharField(max_length=200, default=None)
owner = ForeignKeyField(column_name='owner_id', field='id', model=User, null=False)
class Meta:
table_name = 'brainscore_benchmarktype'
class BenchmarkMeta(PeeweeBase):
number_of_stimuli = IntegerField(null=True)
number_of_recording_sites = IntegerField(null=True)
recording_sites = CharField(max_length=100, null=True)
behavioral_task = CharField(max_length=100, null=True)
class Meta:
table_name = 'brainscore_benchmarkmeta'
class BenchmarkInstance(PeeweeBase):
benchmark = ForeignKeyField(column_name='benchmark_type_id', field='identifier', model=BenchmarkType)
ceiling = FloatField(null=True)
ceiling_error = FloatField(null=True)
version = IntegerField(null=True)
meta = ForeignKeyField(model=BenchmarkMeta, null=True)
class Meta:
table_name = 'brainscore_benchmarkinstance'
class Submission(PeeweeBase):
jenkins_id = IntegerField()
submitter = ForeignKeyField(column_name='submitter_id', field='id', model=User)
timestamp = DateTimeField(null=True)
model_type = CharField()
status = CharField()
class Meta:
table_name = 'brainscore_submission'
class Model(PeeweeBase):
name = CharField()
owner = ForeignKeyField(column_name='owner_id', field='id', model=User)
reference = ForeignKeyField(column_name='reference_id', field='id', model=Reference, null=True)
submission = ForeignKeyField(column_name='submission_id', field='id', model=Submission)
domain = CharField(max_length=200, default=None)
visual_degrees = IntegerField(null=True) # null during creation of new model without having model object loaded
public = BooleanField()
competition = CharField(max_length=200, default=None, null=True)
class Meta:
table_name = 'brainscore_model'
class Score(PeeweeBase):
benchmark = ForeignKeyField(column_name='benchmark_id', field='id', model=BenchmarkInstance)
end_timestamp = DateTimeField(null=True)
error = FloatField(null=True)
model = ForeignKeyField(column_name='model_id', field='id', model=Model)
score_ceiled = FloatField(null=True)
score_raw = FloatField(null=True)
start_timestamp = DateTimeField(null=True)
comment = CharField(null=True, max_length=1_000)
class Meta:
table_name = 'brainscore_score'
def clear_schema():
"""
Delete the contents of all tables.
This function is meant for testing only, use with caution.
"""
Score.delete().execute()
Model.delete().execute()
Submission.delete().execute()
BenchmarkInstance.delete().execute()
BenchmarkType.delete().execute()
Reference.delete().execute()
User.delete().execute()
def create_schema(schema_name):
"""
Creates an isolated schema for testing purposes.
"""
try:
database_proxy.execute_sql(f'CREATE SCHEMA IF NOT EXISTS {schema_name}')
print(f"Schema {schema_name} created successfully.")
except Exception as e:
print(f"Error creating schema {schema_name}: {e}")
def drop_schema(schema_name):
"""
Drops a schema that was used for testing purposes.
"""
database_proxy.execute_sql(f'DROP SCHEMA IF EXISTS {schema_name} CASCADE')
def create_tables():
"""
Creates the necessary tables for a test database schema.
"""
with database_proxy:
database_proxy.create_tables(PeeweeBase.__subclasses__(), safe=True)