Skip to content

Instantly share code, notes, and snippets.

View dsalaj's full-sized avatar
🐙

Darjan Salaj dsalaj

🐙
View GitHub Profile
@dsalaj
dsalaj / resize.py
Created August 19, 2017 10:25
Resize and pad images
import os
import cv2
dir_files = os.listdir('raw_data')
extensions = [filename[-4:] for filename in dir_files]
for ext in extensions:
assert ext == '.jpg'
max_width = 0
@dsalaj
dsalaj / get_results.py
Last active January 22, 2019 08:26
Traverse directory for json files with results and print some value from dict
import json
import sys
import os
folders = []
files = []
RESULT_KEY = 'test_per'
data = {
'subdir1': [],
'subdir2': [],
@dsalaj
dsalaj / glider.py
Last active November 23, 2018 12:15
Answer to a question on stackoverflow
#!/usr/bin/python
#!/usr/bin/python
# Answer to this:
# https://stackoverflow.com/questions/53442614/conways-game-of-life-in-python-3-with-matplotlib-problem-with-displaying-a-fo#53442614
# call with: python3 cgl.py 10 500 1 1
import os
import argparse
import numpy as np
@dsalaj
dsalaj / grammar.py
Created December 16, 2018 23:18
Reber grammar generation and validation
import nltk
from nltk.parse.generate import generate
# Define Reber grammar
# http://christianherta.de/lehre/dataScience/machineLearning/neuralNetworks/pics/embeddedReberGrammar.png
grammarStr = '''
START -> 'B' 'T' REBER 'T' 'E' | 'B' 'P' REBER 'P' 'E'
REBER -> 'B' E1
E1 -> 'T' E2 | 'P' E3
@dsalaj
dsalaj / juwels_cheatsheet.md
Last active February 5, 2024 11:25
JUWELS cheatsheet

local .ssh/config contents

Host juwels
    HostName juwels.fz-juelich.de
    User salaj1
    IdentityFile ~/.ssh/id_rsa_juwels.pub

To connect simply ssh juwels.

juwels

@dsalaj
dsalaj / extract_frames.py
Created February 1, 2019 09:24
Extract and downsample frames from video to numpy array
import cv2
import numpy as np
print(cv2.__version__)
vidcap = cv2.VideoCapture('vid.mp4')
success,image = vidcap.read()
count = 0
success = True
frames = []
while success:
frames.append(image)
@dsalaj
dsalaj / video_to_dataset.sh
Last active February 1, 2019 09:33
shell commands used to extract and downsample video to numpy arrray
# install Anaconda to control the environment: https://www.anaconda.com/distribution/#linux
wget https://repo.anaconda.com/archive/Anaconda3-2018.12-Linux-x86_64.sh
chmod +x Anaconda3-2018.12-Linux-x86_64.sh
./Anaconda3-2018.12-Linux-x86_64.sh
# answer to the installation prompts
# activate environment and install the required libraries
conda create -n vid2frame
conda activate vid2frame
conda install opencv scipy
# # First create and activate conda python3 environment:
# conda create -n video python=3.6
# conda activate video
# # Then install the requirements:
# conda install ffmpeg
# conda install tensorflow-gpu==1.13.1
# pip install tensorflow_datasets
# # The bellow code would still produce an error because of the missing file ("ucf101_labels.txt")
# # So manually download the "ucf101_labels.txt" and put it in place:
# cd "/home/$USER/anaconda3/envs/video/lib/python3.6/site-packages/tensorflow_datasets/video/"
@dsalaj
dsalaj / cte_analog_to_spikes.py
Last active December 9, 2019 08:26
Crossing Threshold Encoding of pixel values to spikes
def find_onset_offset(y, threshold):
"""
Given the input signal `y` with samples,
find the indices where `y` increases and descreases through the value `threshold`.
Return stacked binary arrays of shape `y` indicating onset and offset threshold crossings.
`y` must be 1-D numpy arrays.
"""
if threshold == 1:
equal = y == threshold
transition_touch = np.where(equal)[0]
@dsalaj
dsalaj / jupyter_setup.sh
Created September 16, 2019 08:39
Setup python jupyter notebooks for editing over SSH
# Steps for setting up python jupyter notebook for editing over SSH
# this is not a runnable script as different commands need to be executed on different machines
# ON REMOTE MACHINE
ssh username@remotepc123
# make sure the jupyter is installed
pip install jupyter
# start jupyter on specified port and no-browser mode
jupyter notebook --no-browser --port=8080
# copy the url with token that looks something like this: