Skip to content

Instantly share code, notes, and snippets.

View chelseatroy's full-sized avatar

Chelsea Troy chelseatroy

View GitHub Profile
@chelseatroy
chelseatroy / workouts_controller.rb
Last active September 20, 2018 17:39
Example: Data Import as a Process
class WorkoutsController < ApplicationController
...
def index
@workouts = Workout.complete
end
def fix_incompletes
@workouts = Workout.needs_revision
@chelseatroy
chelseatroy / workout.rb
Created September 20, 2018 02:32
Example: Data Import as a Transaction
class Workout
property :description, type: String
property :date, type: DateTime
validates :date, presence: true
...
end
@chelseatroy
chelseatroy / compressed.py
Last active September 25, 2018 20:02
toarray implementation in _cs_matrix
class _cs_matrix(_data_matrix, _minmax_mixin, IndexMixin):
...
def toarray(self, order=None, out=None):
if out is None and order is None:
order = self._swap('cf')[0]
out = self._process_toarray_args(order, out)
if not (out.flags.c_contiguous or out.flags.f_contiguous):
raise ValueError('Output array must be C or F contiguous')
# align ideal order with output array order
@chelseatroy
chelseatroy / base.py
Last active September 25, 2018 20:08
_process_toarray_args in spmatrix class
class spmatrix(object):
...
def _process_toarray_args(self, order, out):
if out is not None:
if order is not None:
raise ValueError('order cannot be specified if out '
'is not None')
if out.shape != self.shape or out.dtype != self.dtype:
raise ValueError('out array must be same dtype and shape as '
@chelseatroy
chelseatroy / generate_sparsetools.py
Last active September 26, 2018 14:55
The C++ sparsetools generator
"""
python generate_sparsetools.py
Generate manual wrappers for C++ sparsetools code.
Type codes used:
'i': integer scalar
'I': integer array
'T': data array
@chelseatroy
chelseatroy / csr.h
Created September 25, 2018 21:37
implementation of csr_todense
/*
* Compute B += A for CSR matrix A, C-contiguous dense matrix B
*
* Input Arguments:
* I n_row - number of rows in A
* I n_col - number of columns in A
* I Ap[n_row+1] - row pointer
* I Aj[nnz(A)] - column indices
* T Ax[nnz(A)] - nonzero values
* T Bx[n_row*n_col] - dense matrix in row-major order
@chelseatroy
chelseatroy / base.py
Created September 26, 2018 03:12
spmatrix initializer
class spmatrix(object):
""" This class provides a base class for all sparse matrices. It
cannot be instantiated. Most of the work is provided by subclasses.
"""
__array_priority__ = 10.1
ndim = 2
def __init__(self, maxprint=MAXPRINT):
self._shape = None
@chelseatroy
chelseatroy / compressed.py
Last active September 26, 2018 03:24
cs_matrix initializer
class _cs_matrix(_data_matrix, _minmax_mixin, IndexMixin):
"""base matrix class for compressed row and column oriented matrices"""
def __init__(self, arg1, shape=None, dtype=None, copy=False):
_data_matrix.__init__(self)
if isspmatrix(arg1):
...
elif isinstance(arg1, tuple):
@chelseatroy
chelseatroy / sputils.py
Created September 26, 2018 03:19
sputils check_shape
def check_shape(args, current_shape=None):
"""Imitate numpy.matrix handling of shape arguments"""
@chelseatroy
chelseatroy / coo.py
Last active September 26, 2018 03:22
coo_matrix initializer
class coo_matrix(_data_matrix, _minmax_mixin):
...
format = 'coo'
def __init__(self, arg1, shape=None, dtype=None, copy=False):
_data_matrix.__init__(self)
if isinstance(arg1, tuple):
...