Skip to content

Instantly share code, notes, and snippets.

View lebedov's full-sized avatar

Lev E. Givon lebedov

View GitHub Profile
@lebedov
lebedov / pytorch_compute_out_size.py
Created June 30, 2017 20:49
Compute pytorch network layer output size given an input.
#!/usr/bin/env python
"""
Compute pytorch network layer output size given an input.
"""
import numpy as np
import torch
import torch.autograd as autograd
import torch.nn as nn
@lebedov
lebedov / compare_construct_numpy_timing.py
Last active June 27, 2017 16:59
Compare timing of record parsing in construct and numpy.
#!/usr/bin/env python
"""
Compare timing of record parsing in construct and numpy.
"""
import time
from construct import LazyStruct, Struct, Int16un, Int8un
import numpy as np
@lebedov
lebedov / cast_void_ptr_to_2d_ptr_array.cpp
Created April 27, 2017 14:21
How to save a 2D array of pointers to a class attribute and access the array's contents properly.
// How to save a 2D array of pointers to a class attribute and
// access the array's contents properly.
#include <iostream>
#include <stdio.h>
// Show pointer values in array:
void show_ptr(int *a[2][2]) {
for (int i=0; i<2; i++) {
for (int j=0; j<2; j++) {
printf("[%d,%d] %p\n", i, j, &a[i][j]);
@lebedov
lebedov / cuda_cpp_class.cu
Created April 24, 2017 21:56
How to "wrap" a CUDA kernel with a C++ class.
// How to "wrap" a CUDA kernel with a C++ class; the kernel must be defined outside of
// the class and launched from within a class instance's method.
#include <iostream>
#include <cuda.h>
#include <cuda_runtime.h>
#define LEN 10
__global__ void kernel(int *a, int *b, unsigned int N);
@lebedov
lebedov / translate_bilinear_interp.py
Created April 20, 2017 19:28
Translate an image by fractional number of pixels using bilinear interpolation.
#!/usr/bin/env python
"""
Translate an image by fractional number of pixels using bilinear interpolation.
Based on various snippets of code on StackOverflow.
"""
import numpy as np
import skimage.draw as skdraw
@lebedov
lebedov / ct_win.py
Created April 6, 2017 16:49
Routines for scaling biomedical image data by window level and width.
#!/usr/bin/env python
"""
Routines for scaling biomedical image data by window level and width.
"""
import numpy as np
win_dict = {'abdomen':
{'wl': 60, 'ww': 400},
@lebedov
lebedov / tick_space.py
Created April 4, 2017 18:07
Adjust evenly spaced ticks on current matplotlib axis.
#!/usr/bin/env python
"""
Adjust evenly spaced ticks on current matplotlib axis.
"""
import matplotlib.pyplot as plt
import numpy as np
def tick_space(space, which='major', axis='both'):
@lebedov
lebedov / Makefile
Last active April 3, 2017 22:17
Example of how to access a static C++ class instance from C.
%.o: %.c
gcc -c $<
%.o: %.cpp
g++ -c $<
main: main.o mylib.o
gcc $^ -o $@
clean:
@lebedov
lebedov / lasso_imshow.py
Last active May 2, 2022 08:12
How to interactively select part of an array displayed as an image with matplotlib.
#!/usr/bin/env python
"""
How to interactively select part of an array displayed as an image with matplotlib.
"""
import matplotlib.pyplot as plt
from matplotlib.path import Path
from matplotlib.widgets import LassoSelector
import numpy as np
@lebedov
lebedov / Makefile
Last active March 15, 2017 15:56
Simple demo of how to write structs from C and read them in Python.
write: write.c
gcc $< -o $@
test: write
./write
python read.py