Skip to content

Instantly share code, notes, and snippets.

@espdev
espdev / trading_day.py
Last active January 24, 2021 15:39
Trading days offset
from typing import Union
import pandas as pd
from trading_calendars import TradingCalendar, get_calendar
class TradingDay:
"""Offset for trading days
The class is useful to find previous and next trading days from given date.
@espdev
espdev / use_matlab.py
Last active October 23, 2020 21:09
Using Matlab shared engine
# -*- coding: utf-8 -*-
import multiprocessing as mp
import matlab.engine as me
def start_matlab(ready_event, close_event):
print('Starting matlab...')
eng = me.start_matlab(option='-nodesktop')
@espdev
espdev / main.py
Created March 4, 2020 23:20
scipy/sprs random sparse matrices multiplication
import time
import numpy as np
from scipy.sparse import csr_matrix
def make_matrix(shape, nnz):
i = np.random.randint(0, shape[0] - 1, nnz)
j = np.random.randint(0, shape[1] - 1, nnz)
data = np.random.randn(nnz)
@espdev
espdev / main.py
Created March 4, 2020 10:59
Rust/Python sparse matrices multiplication
import time
import numpy as np
from scipy.sparse import diags
def make_matrix(pcount):
x = np.linspace(0., 10., pcount)
dx = np.diff(x)
odx = 1. / dx
@espdev
espdev / cls_is_none.py
Created January 17, 2020 12:43
cls is None
from typing import Optional
from pydantic import BaseModel, validator
class MyModel(BaseModel):
name: Optional[str]
class Config:
validate_assignment = True
@espdev
espdev / index_offset.py
Created January 17, 2020 11:32
numpy array index offset
import numpy as np
def index_offset(a):
if a.base is None:
ravel_index = 0
shape = a.shape
else:
byte_offset = np.byte_bounds(a)[0] - np.byte_bounds(a.base)[0]
ravel_index = byte_offset // a.dtype.itemsize
shape = a.base.shape
@espdev
espdev / output.txt
Created January 15, 2020 15:52
pydantic_algo
name='laplace' params=LaplaceAlgoParams(gradient_sigma=1.0, laplacian_sigma=2.0)
name='gauss' params=GaussAlgoParams(gaussian_sigma=1.5)
1 validation error for AlgoSettings
name
Cannot find algorithm 'lol'. Existing algorithms: ['laplace', 'gauss'] (type=value_error)
1 validation error for AlgoSettings
params
<class '__main__.GaussAlgoParams'> parameters type is invalid for 'laplace' algorithm. Parameters type must be a subclass of <class '__main__.LaplaceAlgoParams'> (type=type_error)
@espdev
espdev / plot_rectangular_parallelepiped.py
Last active September 27, 2019 12:25
Draw rectangular parallelepiped using the opposite vertices (diagonal)
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import matplotlib.pyplot as plt
# x y z
a = (1, 1, 1) # p1
b = (2, 2, 2) # p2
x, y, z = 0, 1, 2
@espdev
espdev / mirroring-point.ipynb
Created November 29, 2017 09:55
Отражение точки относительно заданного отрезка
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@espdev
espdev / slide.py
Last active October 26, 2017 10:33
Sliding window over data from the sequence
import itertools
def slide(seq, n=2):
"""Returns a sliding window (of width n) over data from the sequence
s -> (s0, s1, ..., s[n-1]), (s1, s2, ..., sn), ...
"""
it = iter(seq)
result = tuple(itertools.islice(it, n))
if len(result) == n: