Skip to content

Instantly share code, notes, and snippets.

@bradland
Created December 22, 2011 18:19
Show Gist options
  • Save bradland/1511287 to your computer and use it in GitHub Desktop.
Save bradland/1511287 to your computer and use it in GitHub Desktop.
MCMap Live Slice Sorter
#!/usr/bin/env ruby
# Sorts slice sequence images output by MCMap Live. Creates folders of every
# five and ten images for inspection.
require 'fileutils'
class McSorter
def initialize
@files = Dir.glob('*.png')
@pattern = get_pattern(@files.first)
end
def start
# Prepend label names with an underscore so they show up first in the
# directory listing, regardless of how the slices were named.
# Sort tens
sort_files('_tens', 10)
# Sort fives
sort_files('_fives', 5)
return 0 # Return 0 for great success!
end
private
# Creates a pattern that works with sprintf allowing substitution of an
# integer index
def get_pattern(name)
patterns = name.scan(/(.*?)(\d+)(.*)/).first
"#{patterns.first}%s#{patterns.last}"
end
# Returns an array of integers at a stepped interval
def steps(step)
ret = (step..128).step(step).to_a
ret.unshift(1)
ret.push(128)
end
# Take a list of file names and drop them in to a folder
def sort_files(label, step)
files = steps(step).collect {|i| @pattern % i}
FileUtils.rm_rf(label) if Dir.exists?(label)
FileUtils.mkdir(label)
FileUtils.cp(files, label)
end
end
McSorter.new.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment