forked from datajoint/datajoint-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema_simple.py
More file actions
294 lines (241 loc) · 6.36 KB
/
schema_simple.py
File metadata and controls
294 lines (241 loc) · 6.36 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
"""
A simple, abstract schema to test relational algebra
"""
import random
import datajoint as dj
import itertools
import hashlib
import uuid
import faker
import numpy as np
from datetime import date, timedelta
import inspect
class IJ(dj.Lookup):
definition = """ # tests restrictions
i : int
j : int
"""
contents = list(dict(i=i, j=j + 2) for i in range(3) for j in range(3))
class JI(dj.Lookup):
definition = """ # tests restrictions by relations when attributes are reordered
j : int
i : int
"""
contents = list(dict(i=i + 1, j=j) for i in range(3) for j in range(3))
class A(dj.Lookup):
definition = """
id_a :int
---
cond_in_a :tinyint
"""
contents = [(i, i % 4 > i % 3) for i in range(10)]
class B(dj.Computed):
definition = """
-> A
id_b :int
---
mu :float # mean value
sigma :float # standard deviation
n :smallint # number samples
"""
class C(dj.Part):
definition = """
-> B
id_c :int
---
value :float # normally distributed variables according to parameters in B
"""
def make(self, key):
random.seed(str(key))
sub = B.C()
for i in range(4):
key["id_b"] = i
mu = random.normalvariate(0, 10)
sigma = random.lognormvariate(0, 4)
n = random.randint(0, 10)
self.insert1(dict(key, mu=mu, sigma=sigma, n=n))
sub.insert(
dict(key, id_c=j, value=random.normalvariate(mu, sigma))
for j in range(n)
)
class L(dj.Lookup):
definition = """
id_l: int
---
cond_in_l :tinyint
"""
contents = [(i, i % 3 >= i % 5) for i in range(30)]
class D(dj.Computed):
definition = """
-> A
id_d :int
---
-> L
"""
def _make_tuples(self, key):
# make reference to a random tuple from L
random.seed(str(key))
lookup = list(L().fetch("KEY"))
self.insert(dict(key, id_d=i, **random.choice(lookup)) for i in range(4))
class E(dj.Computed):
definition = """
-> B
-> D
---
-> L
"""
class F(dj.Part):
definition = """
-> E
id_f :int
---
-> B.C
"""
class G(dj.Part):
definition = """ # test secondary fk reference
-> E
id_g :int
---
-> L
"""
class H(dj.Part):
definition = """ # test no additional fk reference
-> E
id_h :int
"""
def make(self, key):
random.seed(str(key))
l_contents = list(L().fetch("KEY"))
part_f, part_g, part_h = E.F(), E.G(), E.H()
bc_references = list((B.C() & key).fetch("KEY"))
random.shuffle(bc_references)
self.insert1(dict(key, **random.choice(l_contents)))
part_f.insert(
dict(key, id_f=i, **ref)
for i, ref in enumerate(bc_references)
if random.getrandbits(1)
)
g_inserts = [dict(key, id_g=i, **ref) for i, ref in enumerate(l_contents)]
part_g.insert(g_inserts)
part_h.insert(dict(key, id_h=i) for i in range(4))
class F(dj.Manual):
definition = """
id: int
----
date=null: date
"""
class G(dj.Computed):
definition = """ # test downstream of complex master/parts
-> E
"""
def make(self, key):
self.insert1(key)
class DataA(dj.Lookup):
definition = """
idx : int
---
a : int
"""
contents = list(zip(range(5), range(5)))
class DataB(dj.Lookup):
definition = """
idx : int
---
a : int
"""
contents = list(zip(range(5), range(5, 10)))
class Website(dj.Lookup):
definition = """
url_hash : uuid
---
url : varchar(1000)
"""
def insert1_url(self, url):
hashed = hashlib.sha1()
hashed.update(url.encode())
url_hash = uuid.UUID(bytes=hashed.digest()[:16])
self.insert1(dict(url=url, url_hash=url_hash), skip_duplicates=True)
return url_hash
class Profile(dj.Manual):
definition = """
ssn : char(11)
---
name : varchar(70)
residence : varchar(255)
blood_group : enum('A+', 'A-', 'AB+', 'AB-', 'B+', 'B-', 'O+', 'O-')
username : varchar(120)
birthdate : date
job : varchar(120)
sex : enum('M', 'F')
"""
class Website(dj.Part):
definition = """
-> master
-> Website
"""
def populate_random(self, n=10):
fake = faker.Faker()
faker.Faker.seed(0) # make test deterministic
for _ in range(n):
profile = fake.profile()
with self.connection.transaction:
self.insert1(profile, ignore_extra_fields=True)
for url in profile["website"]:
self.Website().insert1(
dict(ssn=profile["ssn"], url_hash=Website().insert1_url(url))
)
class TTestUpdate(dj.Lookup):
definition = """
primary_key : int
---
string_attr : varchar(255)
num_attr=null : float
blob_attr : longblob
"""
contents = [
(0, "my_string", 0.0, np.random.randn(10, 2)),
(1, "my_other_string", 1.0, np.random.randn(20, 1)),
]
class ArgmaxTest(dj.Lookup):
definition = """
primary_key : int
---
secondary_key : char(2)
val : float
"""
n = 10
@property
def contents(self):
n = self.n
yield from zip(
range(n**2),
itertools.chain(*itertools.repeat(tuple(map(chr, range(100, 100 + n))), n)),
np.random.rand(n**2),
)
class ReservedWord(dj.Manual):
definition = """
# Test of SQL reserved words
key : int
---
in : varchar(25)
from : varchar(25)
int : int
select : varchar(25)
"""
class OutfitLaunch(dj.Lookup):
definition = """
# Monthly released designer outfits
release_id: int
---
day: date
"""
contents = [(0, date.today() - timedelta(days=15))]
class OutfitPiece(dj.Part, dj.Lookup):
definition = """
# Outfit piece associated with outfit
-> OutfitLaunch
piece: varchar(20)
"""
contents = [(0, "jeans"), (0, "sneakers"), (0, "polo")]
LOCALS_SIMPLE = {k: v for k, v in locals().items() if inspect.isclass(v)}
__all__ = list(LOCALS_SIMPLE)