Skip to content

Instantly share code, notes, and snippets.

View lebedov's full-sized avatar

Lev E. Givon lebedov

View GitHub Profile
@lebedov
lebedov / cat_half.py
Created February 23, 2017 15:10
Algorithm for concatenating half precision pytorch tensors.
#!/usr/bin/env python
"""
Algorithm for concatenating half precision tensors by allocating new output matrix
of appropriate size and copying each of the constituent tensors into it with
appropriate offsets.
"""
import numpy as np
import torch
@lebedov
lebedov / pairwise_distance_pytorch.py
Created February 21, 2017 15:39
Pairwise distance module for pytorch.
#!/usr/bin/env python
"""
Pairwise distance module for pytorch.
"""
import torch.autograd as autograd
import torch.nn as nn
class PairwiseDistance(nn.Module):
@lebedov
lebedov / running_var.py
Last active February 21, 2017 21:13
Numerically stable algorithm for computing running variance.
#!/usr/bin/env python
"""
Numerically stable algorithm for computing running sample variance.
Reference
---------
.. [1] B.P. Welford. "Note on a method for calculating corrected sums of squares
and products", Technometrics, Vol. 4, No. 3, pp. 419-420.
"""
@lebedov
lebedov / pytorch_view.py
Created January 22, 2017 17:53
Non-legacy View module for pytorch (http://pytorch.org).
#!/usr/bin/env python
"""
Non-legacy View module for pytorch (http://pytorch.org)
"""
import torch.autograd as autograd
import torch.nn as nn
class View(nn.Module):
@lebedov
lebedov / retain_fixed_number_sublists.m
Last active January 18, 2017 16:26
How to only retain a fixed number of sublists that contain specific values.
% How to only retain a fixed number of sublists that contain specific values.
% Sublists to analyze:
ids = {{0, 0}, {1, 0}, {0, 1, 2}, {0, 2}, {1, 2}, {0, 2}, {1, 1}};
counts = [0, 0, 0];
for i=1:length(ids),
id = cell2mat(ids{i});
% Retain `id` only if it contains at least one
@lebedov
lebedov / copy_convert.lua
Created November 3, 2016 23:26
How to copy a Torch model to a new type without explicit cloning.
-- How to copy a Torch model to a new type without explicit cloning.
-- Based on code in checkpoints.lua in http://github.com/facebook/fb.resnet.torch
require 'torch'
function copy_convert(obj, t)
local copy = {}
for k, v in pairs(obj) do
if type(v) == 'table' then
@lebedov
lebedov / nipype_filter_mapnode.py
Last active October 21, 2016 16:10
Filter out certain outputs in a nipype MapNode.
#!/usr/bin/env python
"""
Filter out certain outputs in a nipype MapNode.
"""
import logging
import nipype
import nipype.interfaces.utility as utility
@lebedov
lebedov / nipype_fixed_reorient2std.py
Created October 16, 2016 16:14
Ensure Reorient2Std output is compressed or uncompressed as per output_type trait.
#!/usr/bin/env python
"""
Ensure Reorient2Std output is compressed or uncompressed as per output_type trait.
Notes
-----
Works around possible fslreorient2std bug: https://github.com/nipy/nipype/issues/1683
"""
@lebedov
lebedov / nipype_normalize.py
Created October 10, 2016 14:58
How to normalize intensity of brain images to the range [0, 1] with nipype and FSL.
#!/usr/bin/env python
"""
How to normalize intensity of brain images to the range [0, 1] with nipype and FSL.
"""
import os.path
import nipype
import nipype.interfaces
@lebedov
lebedov / nipype_grabber_sink_demo.py
Last active October 10, 2016 02:50
Simple demo of a nipype pipeline that uses DataGrabber and DataSink.
#!/usr/bin/env python
"""
Simple demo of a nipype pipeline that uses DataGrabber and DataSink.
"""
import os.path
import nipype
import nipype.interfaces.fsl as fsl