sbmf 0.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
sbmf-0.0.1/LICENCE.txt ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2018 The Python Packaging Authority
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
sbmf-0.0.1/PKG-INFO ADDED
@@ -0,0 +1,14 @@
1
+ Metadata-Version: 2.4
2
+ Name: sbmf
3
+ Version: 0.0.1
4
+ Summary: Utilities for sparse and semi-binary matrix factorization
5
+ Author-Email: Matteo Alleman <mmallemmann@gmail.com>
6
+ License-Expression: MIT
7
+ License-File: LICENCE.txt
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: Operating System :: OS Independent
10
+ Requires-Python: >=3.8
11
+ Requires-Dist: numpy
12
+ Description-Content-Type: text/markdown
13
+
14
+ Code for binary matrix factorization
sbmf-0.0.1/README.md ADDED
@@ -0,0 +1 @@
1
+ Code for binary matrix factorization
sbmf-0.0.1/meson.build ADDED
@@ -0,0 +1,58 @@
1
+ project('sbmf', 'c', 'cython',
2
+ version: '0.0.1',
3
+ default_options: ['warning_level=2'])
4
+
5
+ # Find Python and required dependencies
6
+ py = import('python').find_installation()
7
+ py_dep = py.dependency()
8
+
9
+ ## Find numpy (taken from sklearn's build file)
10
+ incdir_numpy = meson.get_external_property('numpy-include-dir', 'not-given')
11
+ if incdir_numpy == 'not-given'
12
+ incdir_numpy = run_command(py,
13
+ [
14
+ '-c',
15
+ '''
16
+ import os
17
+ import numpy as np
18
+ try:
19
+ incdir = os.path.relpath(np.get_include())
20
+ except Exception:
21
+ incdir = np.get_include()
22
+ print(incdir)
23
+ '''
24
+ ],
25
+ check: true
26
+ ).stdout().strip()
27
+ endif
28
+
29
+ inc_np = include_directories(incdir_numpy)
30
+ # Don't use the deprecated NumPy C API. Define this to a fixed version instead of
31
+ # NPY_API_VERSION in order not to break compilation for released SciPy versions
32
+ # when NumPy introduces a new deprecation.
33
+ numpy_no_deprecated_api = ['-DNPY_NO_DEPRECATED_API=NPY_1_9_API_VERSION']
34
+ np_dep = declare_dependency(include_directories: inc_np, compile_args: numpy_no_deprecated_api)
35
+
36
+
37
+ # Find Cython
38
+ cython = find_program('cython', required: true)
39
+
40
+ # Generate C file from Cython sourceme
41
+ cython_gen = generator(cython,
42
+ output: '@BASENAME@.c',
43
+ arguments: ['@INPUT@', '-o', '@OUTPUT@',
44
+ '--directive', 'language_level=3',
45
+ '--directive', 'boundscheck=False',
46
+ '--directive', 'wraparound=False',
47
+ '--directive', 'cdivision=True'])
48
+
49
+ # Generate the C source
50
+ sparse_c = cython_gen.process('src/sbmf/spsearch.pyx')
51
+
52
+ # Build the extension module
53
+ py.extension_module('sparse',
54
+ sparse_c,
55
+ dependencies: [py_dep, np_dep],
56
+ include_directories: [inc_np],
57
+ install: true,
58
+ subdir: 'sbmf')
@@ -0,0 +1,20 @@
1
+ [build-system]
2
+ requires = ["meson-python", "cython", "numpy"]
3
+ build-backend = "mesonpy"
4
+
5
+ [project]
6
+ name = "sbmf"
7
+ version = "0.0.1"
8
+ authors = [
9
+ { name="Matteo Alleman", email="mmallemmann@gmail.com" },
10
+ ]
11
+ description = "Utilities for sparse and semi-binary matrix factorization"
12
+ readme = "README.md"
13
+ requires-python = ">=3.8"
14
+ classifiers = [
15
+ "Programming Language :: Python :: 3",
16
+ "Operating System :: OS Independent",
17
+ ]
18
+ license = "MIT"
19
+ license-files = ["LICEN[CS]E*"]
20
+ dependencies = ["numpy"]
File without changes
@@ -0,0 +1 @@
1
+ numpy
File without changes
@@ -0,0 +1,833 @@
1
+ cimport numpy as np
2
+ import numpy as np
3
+ from libc.math cimport exp, ceil, lround
4
+ from libc.stdlib cimport malloc, free
5
+ from cython.parallel import prange
6
+
7
+ ctypedef np.int32_t int32
8
+ ctypedef np.npy_uint32 UINT32_t
9
+ cdef inline UINT32_t DEFAULT_SEED = 1
10
+ cdef enum:
11
+ # Max value for our rand_r replacement (near the bottom).
12
+ # We don't use RAND_MAX because it's different across platforms and
13
+ # particularly tiny on Windows/MSVC.
14
+ # It corresponds to the maximum representable value for
15
+ # 32-bit signed integers (i.e. 2^31 - 1).
16
+ RAND_R_MAX = 2147483647
17
+
18
+ np.import_array()
19
+
20
+ ######################################################################
21
+ #### Utils, because it's really annoying to import across files ######
22
+ ######################################################################
23
+
24
+ cdef class FiniteSet:
25
+ """
26
+ Custom unordered set with O(1) add/remove methods
27
+ which takes advantage of the fixed maximum set size
28
+
29
+ Many thanks to Jerome Richard on stackoverflow for the suggestion
30
+ """
31
+ cdef:
32
+ int* index # Index of element in value array
33
+ int* value # Array of contained element
34
+ int size # Current number of elements
35
+
36
+ def __cinit__(self, int[:] indices, int maximum):
37
+ """C-level initialization"""
38
+ self.size = len(indices)
39
+
40
+ # Allocate arrays
41
+ self.index = <int*>malloc(maximum * sizeof(int))
42
+ self.value = <int*>malloc(maximum * sizeof(int))
43
+
44
+ if not self.index or not self.value:
45
+ raise MemoryError("Could not allocate memory")
46
+
47
+ # Initialize index array to -1 (invalid)
48
+ cdef int i
49
+ for i in range(maximum):
50
+ self.index[i] = -1
51
+
52
+ # Set up initial values
53
+ for i in range(self.size):
54
+ self.value[i] = indices[i]
55
+ self.index[indices[i]] = i
56
+
57
+ def __dealloc__(self):
58
+ """Cleanup C memory"""
59
+ if self.index:
60
+ free(self.index)
61
+ if self.value:
62
+ free(self.value)
63
+
64
+ cdef inline bint contains(self, int i) nogil:
65
+ return self.index[i] >= 0
66
+
67
+ cdef inline void add(self, int i) nogil:
68
+ """
69
+ Add element to set
70
+ """
71
+ # if not self.contains(i):
72
+ if self.index[i] < 0:
73
+ self.value[self.size] = i
74
+ self.index[i] = self.size
75
+ ## Increase
76
+ self.size += 1
77
+
78
+ cdef inline void remove(self, int i) nogil:
79
+ """
80
+ Remove element from set
81
+ """
82
+ # if self.contains(i):
83
+ if self.index[i] >= 0:
84
+ self.value[self.index[i]] = self.value[self.size - 1]
85
+ self.index[self.value[self.size - 1]] = self.index[i]
86
+ self.index[i] = -1
87
+ self.size -= 1
88
+
89
+ cdef class BiMat:
90
+ """
91
+ A format for sparse binary matrices, initialized from csr format,
92
+ which you could call an "uncompressed sparse row" format
93
+
94
+ Designed for O(1) bit flipping and O(nnz) iteration over rows, but
95
+ uses the same memory (more, actually) as a dense array. Just a 2d
96
+ extension of the FiniteSet format suggested by Jerome.
97
+ """
98
+ ## Examples:
99
+ ## To get a list of non-zero columns for row i, do
100
+ ## elements = []
101
+ ## for j in range(size[i]):
102
+ ## elements.append(value[i][j])
103
+ ##
104
+ ## To check if element j is in set i, do
105
+ ## isin = (index[i][j] > 0)
106
+ cdef:
107
+ int **index # Index of element in value array
108
+ int **value # Array of contained elements
109
+ int *size # Current number of elements per row
110
+ int nrow
111
+ int ncol
112
+
113
+ def __cinit__(self, int[:] indices, int[:] indptr, int dimension):
114
+ """C-level initialization"""
115
+
116
+ self.nrow = len(indptr) - 1
117
+ self.ncol = dimension
118
+ cdef int i, j
119
+
120
+ self.index = <int **>malloc(self.nrow * sizeof(int *))
121
+ self.value = <int **>malloc(self.nrow * sizeof(int *))
122
+ self.size = <int *>malloc(self.nrow * sizeof(int))
123
+
124
+ # print('mallocd rows')
125
+ if not self.index or not self.value or not self.size:
126
+ raise MemoryError("Could not allocate memory")
127
+
128
+ for i in range(self.nrow): # each row's pointer
129
+ self.index[i] = <int *>malloc(dimension * sizeof(int))
130
+ self.value[i] = <int *>malloc(dimension * sizeof(int))
131
+
132
+ if not self.index[i] or not self.value[i]:
133
+ raise MemoryError("Could not allocate memory")
134
+
135
+ # print('malloced cols')
136
+ # Initialize index array to -1 (invalid) and size to 0
137
+ for i in range(self.nrow):
138
+ self.size[i] = 0
139
+ for j in range(dimension):
140
+ self.index[i][j] = -1
141
+
142
+ # Set up initial values
143
+ for i in range(self.nrow):
144
+ for j in range(indptr[i], indptr[i+1]):
145
+ self.value[i][self.size[i]] = indices[j]
146
+ self.index[i][indices[j]] = self.size[i]
147
+ self.size[i] += 1
148
+
149
+ def __dealloc__(self):
150
+ """Cleanup C memory"""
151
+ cdef int i
152
+ if self.index:
153
+ for i in range(self.nrow):
154
+ if self.index[i]:
155
+ free(self.index[i])
156
+ free(self.index)
157
+ if self.value:
158
+ for i in range(self.nrow):
159
+ if self.value[i]:
160
+ free(self.value[i])
161
+ free(self.value)
162
+ if self.size:
163
+ free(self.size)
164
+
165
+ cdef inline int add(self, int i, int j) nogil:
166
+ """
167
+ Add element to set
168
+ """
169
+ if i < 0 or i >= self.nrow or j < 0 or j >= self.ncol:
170
+ return -1
171
+
172
+ if self.index[i][j] < 0:
173
+ self.value[i][self.size[i]] = j
174
+ self.index[i][j] = self.size[i]
175
+ self.size[i] += 1
176
+
177
+ return 1
178
+
179
+ cdef inline int remove(self, int i, int j) nogil:
180
+ """
181
+ Remove element from set
182
+ """
183
+
184
+ if i < 0 or i >= self.nrow or j < 0 or j >= self.ncol:
185
+ return -1
186
+
187
+ cdef int remove_idx, last_idx
188
+
189
+ if self.index[i][j] >= 0:
190
+ remove_idx = self.index[i][j]
191
+ last_idx = self.size[i] - 1
192
+
193
+ if remove_idx < last_idx: # don't swap if last element
194
+ self.value[i][remove_idx] = self.value[i][last_idx]
195
+ self.index[i][self.value[i][last_idx]] = remove_idx
196
+
197
+ self.index[i][j] = -1
198
+ self.size[i] -= 1
199
+
200
+ return 1
201
+
202
+ cdef inline double[:,:] _dot(self, double[:,:] X, double[:,:] Z) nogil:
203
+ """
204
+ Matrix multiplication on the left, Z = SX
205
+ Must provide the output array Z of appropriate shape
206
+ """
207
+ cdef int nout = X.shape[1]
208
+ cdef int i,j,k
209
+
210
+ for i in prange(self.nrow, nogil=True):
211
+ for j in range(nout):
212
+ Z[i][j] = 0
213
+ for k in range(self.size[i]):
214
+ Z[i][j] += X[self.value[i][k]][j]
215
+ return Z
216
+
217
+ cdef inline double[:,:] kernel(self, double[:,:] K) nogil:
218
+ """
219
+ Return the kernel matrix K = SS'
220
+ """
221
+ cdef int i,j,k
222
+
223
+ for i in prange(self.nrow, nogil=True):
224
+ for j in range(self.nrow):
225
+ K[i][j] = 0
226
+ if self.size[i] <= self.size[j]: # only need to check the smallest
227
+ for k in range(self.size[i]):
228
+ if self.index[j][k] > 0: # k in set j
229
+ K[i][j] += 1.0
230
+ else:
231
+ for k in range(self.size[j]):
232
+ if self.index[i][k] > 0: # k in set i
233
+ K[i][j] += 1.0
234
+ return K
235
+
236
+ def pyadd(self, int i, int j):
237
+ return self.add(i,j)
238
+
239
+ def pyremove(self, int i, int j):
240
+ return self.remove(i,j)
241
+
242
+ def dot(self, double[:,:] X):
243
+ cdef int M = X.shape[1]
244
+ cdef np.ndarray[double, ndim=2] out = np.zeros((self.nrow, M))
245
+ return self._dot(X, out)
246
+
247
+ def ker(self):
248
+ cdef np.ndarray[double, ndim=2] K = np.zeros((self.nrow, self.nrow))
249
+ return self.kernel(K)
250
+
251
+ def to_csr(self):
252
+ """
253
+ Output indices and indptr arrays
254
+ """
255
+ cdef np.ndarray[int, ndim=1] indptr = np.zeros(self.nrow+1, dtype=int)
256
+ cdef np.ndarray[int, ndim=1] indices
257
+ cdef list ind_list = []
258
+ cdef int i, j, k
259
+
260
+ for i in range(self.nrow):
261
+ indptr[i+1] = self.size[i] + indptr[i]
262
+ for j in range(self.size[i]):
263
+ ind_list.append(self.value[i][j])
264
+ indices = np.array(ind_list, dtype=int)
265
+
266
+ return indices, indptr
267
+
268
+
269
+ cdef class XORRNG:
270
+ """
271
+ Custom XORRNG sampler that I copied from the scikit-learn source code
272
+ """
273
+
274
+ cdef UINT32_t state
275
+
276
+ def __cinit__(self, UINT32_t seed=DEFAULT_SEED):
277
+ if (seed == 0):
278
+ seed = DEFAULT_SEED
279
+ self.state = seed
280
+
281
+ cdef inline double sample(self) nogil:
282
+ """Generate a pseudo-random np.uint32 from a np.uint32 seed"""
283
+ self.state ^= <UINT32_t>(self.state << 13)
284
+ self.state ^= <UINT32_t>(self.state >> 17)
285
+ self.state ^= <UINT32_t>(self.state << 5)
286
+
287
+ # Use the modulo to make sure that we don't return a values greater than the
288
+ # maximum representable value for signed 32bit integers (i.e. 2^31 - 1).
289
+ # Note that the parenthesis are needed to avoid overflow: here
290
+ # RAND_R_MAX is cast to UINT32_t before 1 is added.
291
+ return <double>(self.state % ((<UINT32_t>RAND_R_MAX) + 1))/RAND_R_MAX
292
+
293
+ cdef inline double cymin(double a, double b, double c) nogil:
294
+ cdef double out = a
295
+ if b < out:
296
+ out = b
297
+ if c < out:
298
+ out = c
299
+ return out
300
+
301
+ cdef inline double sigmoid(double curr) nogil:
302
+ if curr < -100:
303
+ return 0.0
304
+ elif curr > 100:
305
+ return 1.0
306
+ else:
307
+ return 1.0 / (1.0 + exp(-curr))
308
+
309
+
310
+ ######################################################################
311
+ ############# Classes for sampling from p(s|x) #######################
312
+ ######################################################################
313
+
314
+ cdef XORRNG prng = XORRNG()
315
+
316
+ cdef class KernelSampler:
317
+ """
318
+ This stores the statistics of S in order to sample from
319
+ the p(s|x) under the kernel MSE likelihood:
320
+ p(x|s) ~ exp(s'Cov(s,s)s - 2 s'Cov(s,x)x) / Z
321
+ """
322
+
323
+ cdef double **J # recurrent weights (Cov s,s)
324
+ cdef double **W # input weights (Cov s,x)
325
+ cdef double **dJ # accumulated change in J
326
+ cdef double **dW # ditto for W
327
+ cdef double *h # offset/intercept
328
+ cdef double *r # inhibition offset
329
+ cdef double lr # learning rate
330
+ cdef double th # inhib threshold
331
+ cdef int dimx # input dimension
332
+ cdef int dims # latent dimension
333
+ cdef int bsz # batch size
334
+
335
+ def __cinit__(self, double[:,:] Jinit, double[:,:] Winit,
336
+ int batch_size, double lr):
337
+
338
+ cdef int i, j
339
+
340
+ self.dims = Winit.shape[0]
341
+ self.dimx = Winit.shape[1]
342
+ self.bsz = batch_size
343
+ self.lr = lr
344
+ self.th = lr / batch_size
345
+
346
+ # Allocate arrays
347
+ self.J = <double **>malloc(self.dims * sizeof(double *))
348
+ self.W = <double **>malloc(self.dims * sizeof(double *))
349
+ self.dJ = <double **>malloc(self.dims * sizeof(double *))
350
+ self.dW = <double **>malloc(self.dims * sizeof(double *))
351
+ self.h = <double *>malloc(self.dims * sizeof(double))
352
+ self.r = <double *>malloc(self.dims * sizeof(double))
353
+
354
+ if not self.J or not self.W or not self.h or not self.r:
355
+ raise MemoryError("Could not allocate memory")
356
+
357
+ for i in range(self.dims):
358
+ self.J[i] = <double *>malloc(self.dims * sizeof(double))
359
+ self.W[i] = <double *>malloc(self.dimx * sizeof(double))
360
+ self.dJ[i] = <double *>malloc(self.dims * sizeof(double))
361
+ self.dW[i] = <double *>malloc(self.dimx * sizeof(double))
362
+
363
+ if not self.J[i] or not self.W[i] or not self.dJ[i] or not self.dW[i]:
364
+ raise MemoryError("Could not allocate memory")
365
+
366
+ ## Initialize arrays
367
+ for i in range(self.dims):
368
+ self.h[i] = 0.0
369
+ self.r[i] = 0.0
370
+ for j in range(self.dims):
371
+ self.J[i][j] = Jinit[i][j]
372
+ self.h[i] += (Jinit[i][j] - Jinit[i][i] * Jinit[j][j]) * Jinit[j][j]
373
+ self.r[i] += self.R0(i,j)
374
+ self.dJ[i][j] = 0.0
375
+
376
+ for j in range(self.dimx):
377
+ self.W[i][j] = Winit[i][j]
378
+ self.dW[i][j] = 0.0
379
+
380
+ def __dealloc__(self):
381
+ """Cleanup C memory"""
382
+ cdef int i
383
+ if self.J:
384
+ for i in range(self.dims):
385
+ if self.J[i]:
386
+ free(self.J[i])
387
+ free(self.J)
388
+ if self.W:
389
+ for i in range(self.dims):
390
+ if self.W[i]:
391
+ free(self.W[i])
392
+ free(self.W)
393
+ if self.dJ:
394
+ for i in range(self.dims):
395
+ if self.dJ[i]:
396
+ free(self.dJ[i])
397
+ free(self.dJ)
398
+ if self.dW:
399
+ for i in range(self.dims):
400
+ if self.dW[i]:
401
+ free(self.dW[i])
402
+ free(self.dW)
403
+ if self.h:
404
+ free(self.h)
405
+ if self.r:
406
+ free(self.r)
407
+
408
+ cdef inline void learn(self) nogil:
409
+ """
410
+ Update shared parameters
411
+ """
412
+ cdef int i, j
413
+
414
+ for i in prange(self.dims, nogil=True):
415
+ self.h[i] = 0.0
416
+ self.r[i] = 0.0
417
+ for j in range(self.dims):
418
+ self.J[i][j] += self.lr*(self.dJ[i][j] - self.J[i][j])
419
+ self.h[i] += (self.J[i][j] - self.J[i][i] * self.J[j][j]) * self.J[j][j]
420
+ self.r[i] += self.R0(i,j)
421
+ self.dJ[i][j] = 0.0
422
+
423
+ for j in range(self.dimx):
424
+ self.W[i][j] += self.lr*(self.dW[i][j] - self.W[i][j])
425
+ self.dW[i][j] = 0.0
426
+
427
+ cdef inline double R(self, int i, int j) nogil:
428
+ """
429
+ Compute value of R matrix at index (i,j)
430
+ """
431
+
432
+ cdef double A, B, C, D
433
+
434
+ A = (1 - self.th)*self.J[i][j]
435
+ B = (1 - self.th)*self.J[i][i] - A
436
+ C = self.J[j][j] - A
437
+ D = 1 - A - B - C
438
+
439
+ if A - cymin(B,C-self.th,D) < self.th:
440
+ return 1.0
441
+ if B - cymin(A,C,D-self.th) < self.th:
442
+ return -1.0
443
+ if C - cymin(A,B,D) < self.th:
444
+ return -1.0
445
+ if D - cymin(A,B,C) < self.th:
446
+ return 1.0
447
+ return 0.0
448
+
449
+ cdef inline double R0(self, int i, int j) nogil:
450
+ """
451
+ Compute constant value of R matrix at index (i,j)
452
+ """
453
+
454
+ cdef double A, B, C, D
455
+
456
+ A = (1 - self.th)*self.J[i][j]
457
+ B = (1 - self.th)*self.J[i][i] - A
458
+ C = self.J[j][j] - A
459
+ D = 1 - A - B - C
460
+
461
+ if B - cymin(A,C,D-self.th) < self.th:
462
+ return 1.0
463
+ if D - cymin(A,B,C) < self.th:
464
+ return -1.0
465
+ return 0.0
466
+
467
+ cdef inline int recur(self,
468
+ BiMat S,
469
+ double[:] x,
470
+ int i,
471
+ double scl,
472
+ double temp,
473
+ double beta) nogil:
474
+
475
+ cdef bint regularize = (beta > 1e-6)
476
+ cdef int check
477
+ cdef double inp, dot, inhib, curr, prob
478
+ cdef int j, k, l
479
+
480
+ for j in range(self.dims):
481
+
482
+ inp = 0.0
483
+ for k in range(self.dimx):
484
+ inp += 2 * self.W[j][k] * x[k]
485
+
486
+ check = S.remove(i,j)
487
+
488
+ dot = self.J[j][j] * (1 - self.J[j][j]) - 2 * self.h[j]
489
+ inhib = beta * self.r[j]
490
+ for l in range(S.size[i]):
491
+ k = S.value[i][l]
492
+ dot += 2 * (self.J[j][k] - self.J[j][j] * self.J[k][k])
493
+ if regularize:
494
+ inhib += beta * self.R(j, k)
495
+
496
+ curr = (inp - scl * dot - inhib) / temp
497
+ prob = sigmoid(curr)
498
+
499
+ if (prng.sample() < prob):
500
+ check = S.add(i,j)
501
+
502
+ return check
503
+
504
+ def sample(self, BiMat S,
505
+ double[:,:] X,
506
+ double scl,
507
+ double temp,
508
+ double beta):
509
+
510
+ cdef int n = S.nrow
511
+ cdef int n_batch = lround(ceil(n/self.bsz))
512
+
513
+ cdef int check
514
+ cdef bint regularize = (beta > 1e-6)
515
+
516
+ cdef int b, i0, i, ii, j, k
517
+
518
+ # cdef np.ndarray[double, ndim=2] curr = np.zeros((n, self.dims))
519
+
520
+ for b in range(n_batch):
521
+
522
+ i0 = b*self.bsz
523
+
524
+ for i in prange(self.bsz, nogil=True):
525
+ # for i in range(self.bsz):
526
+
527
+ ii = i0 + i
528
+
529
+ ## roughly O(nnz)
530
+ check = self.recur(S, X[ii], ii, scl, temp, beta)
531
+ # curr[ii] = self.recur(S, X[ii], ii, scl, temp, beta)
532
+
533
+ for j in range(S.size[ii]):
534
+ for k in range(S.size[ii]):
535
+ self.dJ[S.value[ii][j]][S.value[ii][k]] += 1.0/self.bsz
536
+ for k in range(self.dimx):
537
+ self.dW[S.value[ii][j]][k] += X[ii][k]/self.bsz
538
+
539
+ if check < 0:
540
+ print("index error")
541
+
542
+ self.learn()
543
+
544
+ return S
545
+ # return curr
546
+
547
+ def get_weights(self, str name):
548
+
549
+ cdef np.ndarray[double, ndim=2] weight
550
+
551
+ if name == "J":
552
+ weight = np.zeros((self.dims, self.dims))
553
+ for i in range(self.dims):
554
+ for j in range(self.dims):
555
+ weight[i][j] = self.J[i][j]
556
+
557
+ elif name == "W":
558
+ weight = np.zeros((self.dims, self.dimx))
559
+ for i in range(self.dims):
560
+ for j in range(self.dimx):
561
+ weight[i][j] = self.W[i][j]
562
+
563
+ elif name == "dJ":
564
+ weight = np.zeros((self.dims, self.dims))
565
+ for i in range(self.dims):
566
+ for j in range(self.dims):
567
+ weight[i][j] = self.dJ[i][j]
568
+
569
+ elif name == "dW":
570
+ weight = np.zeros((self.dims, self.dimx))
571
+ for i in range(self.dims):
572
+ for j in range(self.dimx):
573
+ weight[i][j] = self.dW[i][j]
574
+
575
+ elif name == "R":
576
+ weight = np.zeros((self.dims, self.dims))
577
+ for i in range(self.dims):
578
+ for j in range(self.dims):
579
+ weight[i][j] = self.R(i,j)
580
+
581
+ else:
582
+ raise ValueError("%s not a parameter"%name)
583
+
584
+ return weight
585
+
586
+ def get_offsets(self, str name):
587
+
588
+ cdef np.ndarray[double] offset
589
+
590
+ if name == 'h':
591
+ offset = np.zeros(self.dims)
592
+ for i in range(self.dims):
593
+ offset[i] = self.h[i]
594
+
595
+ elif name == 'r':
596
+ offset = np.zeros(self.dims)
597
+ for i in range(self.dims):
598
+ offset[i] = self.r[i]
599
+
600
+ else:
601
+ raise ValueError("%s not a parameter"%name)
602
+
603
+ return offset
604
+
605
+
606
+ ###############################################################
607
+
608
+ cdef class GaussianSampler:
609
+ """
610
+ This stores the statistics of S in order to sample from
611
+ the p(s|x,W) under the gaussian likelihood:
612
+ p(x|s,W) ~ exp(-|x - Ws|^2)
613
+ """
614
+
615
+ cdef double **J # Cov(s,s)
616
+ cdef double **dJ # accumulated update for J
617
+ cdef double *r # inhibition offset
618
+ cdef double lr # learning rate
619
+ cdef double th # inhib threshold
620
+ cdef int dims # latent dimension
621
+ cdef int bsz # batch size
622
+
623
+ def __cinit__(self, double[:,:] Jinit, int batch_size, double lr):
624
+
625
+ cdef int i, j
626
+
627
+ self.dims = Jinit.shape[0]
628
+ self.bsz = batch_size
629
+ self.lr = lr
630
+ self.th = lr / batch_size
631
+
632
+ # Allocate arrays
633
+ self.J = <double **>malloc(self.dims * sizeof(double *))
634
+ self.dJ = <double **>malloc(self.dims * sizeof(double *))
635
+ self.r = <double *>malloc(self.dims * sizeof(double))
636
+
637
+ if not self.J or not self.r or not self.dJ:
638
+ raise MemoryError("Could not allocate memory")
639
+
640
+ for i in range(self.dims):
641
+ self.J[i] = <double *>malloc(self.dims * sizeof(double))
642
+ self.dJ[i] = <double *>malloc(self.dims * sizeof(double))
643
+
644
+ if not self.J[i] or not self.dJ[i]:
645
+ raise MemoryError("Could not allocate memory")
646
+
647
+ ## Initialize arrays
648
+ for i in range(self.dims):
649
+ self.r[i] = 0.0
650
+ for j in range(self.dims):
651
+ self.J[i][j] = Jinit[i][j]
652
+ self.r[i] += self.R0(i,j)
653
+ self.dJ[i][j] = 0.0
654
+
655
+ def __dealloc__(self):
656
+ """Cleanup C memory"""
657
+ cdef int i
658
+ if self.J:
659
+ for i in range(self.dims):
660
+ if self.J[i]:
661
+ free(self.J[i])
662
+ free(self.J)
663
+ if self.dJ:
664
+ for i in range(self.dims):
665
+ if self.dJ[i]:
666
+ free(self.dJ[i])
667
+ free(self.dJ)
668
+ if self.r:
669
+ free(self.r)
670
+
671
+ cdef inline void learn(self) nogil:
672
+ """
673
+ Update shared parameters
674
+ """
675
+ cdef int i, j
676
+
677
+ for i in prange(self.dims, nogil=True):
678
+ self.r[i] = 0.0
679
+ for j in range(self.dims):
680
+ self.J[i][j] += self.lr*(self.dJ[i][j] - self.J[i][j])
681
+ self.r[i] += self.R0(i,j)
682
+ self.dJ[i][j] = 0.0
683
+
684
+ cdef inline double R(self, int i, int j) nogil:
685
+ """
686
+ Compute value of R matrix at index (i,j)
687
+ """
688
+
689
+ cdef double A, B, C, D
690
+
691
+ A = (1 - self.th)*self.J[i][j]
692
+ B = (1 - self.th)*self.J[i][i] - A
693
+ C = self.J[j][j] - A
694
+ D = 1 - A - B - C
695
+
696
+ if A - cymin(B,C-self.th,D) < self.th:
697
+ return 1.0
698
+ if B - cymin(A,C,D-self.th) < self.th:
699
+ return -1.0
700
+ if C - cymin(A,B,D) < self.th:
701
+ return -1.0
702
+ if D - cymin(A,B,C) < self.th:
703
+ return 1.0
704
+
705
+ cdef inline double R0(self, int i, int j) nogil:
706
+ """
707
+ Compute constant value of R matrix at index (i,j)
708
+ """
709
+
710
+ cdef double A, B, C, D
711
+
712
+ A = (1 - self.th)*self.J[i][j]
713
+ B = (1 - self.th)*self.J[i][i] - A
714
+ C = self.J[j][j] - A
715
+ D = 1 - A - B - C
716
+
717
+ if B - cymin(A,C,D-self.th) < self.th:
718
+ return 1.0
719
+ if D - cymin(A,B,C) < self.th:
720
+ return -1.0
721
+ return 0.0
722
+
723
+ cdef inline int recur(self,
724
+ BiMat S,
725
+ double[:] Wx,
726
+ double[:,:] WtW,
727
+ int i,
728
+ double temp,
729
+ double beta) nogil:
730
+
731
+ cdef bint regularize = (beta > 1e-6)
732
+ cdef int check
733
+ cdef double dot, inhib, curr, prob
734
+ cdef int j, k, l
735
+
736
+ for j in range(self.dims):
737
+
738
+ check = S.remove(i,j)
739
+
740
+ dot = 0.5*WtW[j][j]
741
+ inhib = beta * self.r[j]
742
+ for l in range(S.size[i]):
743
+ k = S.value[i][l]
744
+ dot += WtW[j][k]
745
+
746
+ if regularize:
747
+ inhib += beta * self.R(j, k)
748
+
749
+ curr = (Wx[j] - dot - inhib) / temp
750
+ prob = sigmoid(curr)
751
+
752
+ if (prng.sample() < prob):
753
+ check = S.add(i,j)
754
+
755
+ return check
756
+
757
+ def sample(self, BiMat S,
758
+ double[:,:] WX,
759
+ double[:,:] WtW,
760
+ double temp,
761
+ double beta):
762
+
763
+ cdef int n = S.nrow
764
+ cdef int n_batch = lround(ceil(n/self.bsz))
765
+
766
+ cdef int check
767
+ cdef bint regularize = (beta > 1e-6)
768
+
769
+ cdef int b, i0, i, ii, j, k
770
+
771
+ for b in range(n_batch):
772
+
773
+ i0 = b*self.bsz
774
+
775
+ for i in prange(self.bsz, nogil=True):
776
+ # for i in range(self.bsz):
777
+
778
+ ii = i0 + i
779
+
780
+ ## roughly O(nnz)
781
+ check = self.recur(S, WX[ii], WtW, ii, temp, beta)
782
+
783
+ for j in range(S.size[ii]):
784
+ for k in range(S.size[ii]):
785
+ self.dJ[S.value[ii][j]][S.value[ii][k]] += 1.0/self.bsz
786
+
787
+ if check < 0:
788
+ print("index error")
789
+
790
+ self.learn()
791
+
792
+ return S
793
+
794
+ def get_weights(self, str name):
795
+
796
+ cdef np.ndarray[double, ndim=2] weight
797
+
798
+ if name == "J":
799
+ weight = np.zeros((self.dims, self.dims))
800
+ for i in range(self.dims):
801
+ for j in range(self.dims):
802
+ weight[i][j] = self.J[i][j]
803
+
804
+ elif name == "dJ":
805
+ weight = np.zeros((self.dims, self.dims))
806
+ for i in range(self.dims):
807
+ for j in range(self.dims):
808
+ weight[i][j] = self.dJ[i][j]
809
+
810
+ elif name == "R":
811
+ weight = np.zeros((self.dims, self.dims))
812
+ for i in range(self.dims):
813
+ for j in range(self.dims):
814
+ weight[i][j] = self.R(i,j)
815
+
816
+ else:
817
+ raise ValueError("%s not a parameter"%name)
818
+
819
+ return weight
820
+
821
+ def get_offsets(self, str name):
822
+
823
+ cdef np.ndarray[double] offset
824
+
825
+ if name == 'r':
826
+ offset = np.zeros(self.dims)
827
+ for i in range(self.dims):
828
+ offset[i] = self.r[i]
829
+
830
+ else:
831
+ raise ValueError("%s not a parameter"%name)
832
+
833
+ return offset
@@ -0,0 +1,12 @@
1
+ Metadata-Version: 2.4
2
+ Name: sbmf
3
+ Version: 0.0.1
4
+ Summary: Utilities for sparse and semi-binary matrix factorization
5
+ Author-email: Matteo Alleman <mmallemmann@gmail.com>
6
+ License-Expression: MIT
7
+ Classifier: Programming Language :: Python :: 3
8
+ Classifier: Operating System :: OS Independent
9
+ Requires-Python: >=3.9
10
+ Description-Content-Type: text/markdown
11
+
12
+ Code for binary matrix factorization
@@ -0,0 +1,9 @@
1
+ README.md
2
+ pyproject.toml
3
+ src/sbmf/spsearch.pyx
4
+ src/sbmf/__init__.py
5
+ src/sbmf/spsearch.pyx
6
+ src/sbmf.egg-info/PKG-INFO
7
+ src/sbmf.egg-info/SOURCES.txt
8
+ src/sbmf.egg-info/dependency_links.txt
9
+ src/sbmf.egg-info/top_level.txt
@@ -0,0 +1 @@
1
+ sbmf