Subsample directories of files

This script will allow you to trim a folder(s) full of files to a fixed number of files, sampling randomly. My use case was to select a subsample of training images for CV research, but it could be used for whatever you like!

#!/usr/bin/python
# Programmed by Alex Knaust 2013-03-12

import os, sys, random


def sampleFiles(k, dirs):
	'''Deletes a random sample of files, leaving k remaining
	from each directory. The directories must have identical filenames
	and identical filecounts, otherwise we are in trouble
	'''
	totalFiles = len(os.listdir(dirs[0]))
	if k >= totalFiles:
		print 'Not enough files to sample from'
		exit(1)
	
	# python magic at work
	stuff = [sorted([os.path.join(d, p) for p in os.listdir(d)]) for d in dirs]
	destroy = random.sample(zip(*stuff), totalFiles - k)
	
	return destroy
		

if __name__ == '__main__':
	if len(sys.argv) < 3:
		print('Pass number of files to sample, and directories to sample from')
		sys.exit(1)
	 
	k = int(sys.argv[1])
	dirs = sys.argv[2:]
	destroy = sampleFiles(k, dirs)
	
	ans = raw_input('Will delete %d files, OK? y/n : ' % (len(destroy) * len(dirs)))
	if ans[0].lower() == 'n':
		print 'Goodbye'
		sys.exit(1)
	
	#do the actual deletion
	for files in destroy:
		for f in files:
			os.remove(f) 

CMoy Amp

My good friend (who is an electrical engineer) and I decided to make a pair of Cmoy amps for headphones. I have a pair of Audio Technica ATH-900s which I feel are sometimes not sufficiently served by most devices. We followed the plans by tangentsoft for the most part. The site is amazing and all of the information is well written and curated.

Unfortunately the design provided there doesn’t cover the power supply, it assumes you use 9V batteries. My brief readings indicate that these don’t last particularly long, and I was most interested in using my amp with a computer : no need for portability. We created our own power supply by buying an unregulated 18V wall-wart (not switching). Our circuit to reduce the noise and regulate the input was provided by the voltage regulator datasheet, however we found it necessary to add a large cap (1000\muF) to the input to reduce the AC wave.

Overall, the amp does what I hoped it to do; it gives sufficient volume on almost any device. A lot of audiophile people comment on the change in sound provided, but I don’t really feel comfortable doing so. It sounds fine, maybe the bass is a little punchier.