Skip to content

Instantly share code, notes, and snippets.

@kmader
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save kmader/451d84937014b75368db to your computer and use it in GitHub Desktop.

Select an option

Save kmader/451d84937014b75368db to your computer and use it in GitHub Desktop.
A script for users who do not reconstruct all their samples correctly (the first touches files so they are not automatically deleted, the second reconstructs them all with the same parameters and automatic centering
"""
Touches all files in the image folder based on a list of sample names to keep (prevent from being autodeleted
"""
start_dir='/sls/X02DA/data/e14570/Data10/'
from subprocess import call,Popen,PIPE
import os
fancyExec=lambda cmd: Popen(cmd.split(' '), stdin=PIPE, stdout=PIPE, stderr=PIPE).communicate()[0].split('\n')
clean_line = lambda lines: dict(filter(lambda cline: len(cline[0])>0,map(lambda cline: (cline.upper().strip(),cline.strip()),lines)))
keeplines = clean_line(open('keepfiles.txt').readlines()).keys()
alllines = clean_line(fancyExec('find '+start_dir+' -mindepth 2 -maxdepth 2 -type d'))
import re
line_to_re = lambda line: lambda other_str: re.compile('.*'+line+'.*').match(other_str) is not None
keepmatches = map(line_to_re,keeplines)
any = lambda clist: len(filter(lambda x: x,clist))>0 # old versions of python dont have this function
anymatch = lambda cline: any(map(lambda match_fun: match_fun(cline[0]),keepmatches))
keepfiles = dict(filter(anymatch,alllines.items()))
print ('Files to be touched:',keepfiles)
"""
This command is from Heiner Billich to touch all the files in a directory efficiently
and is documented as follows
The options -print0 and -0 are just in case that any filename contains
blanks. See the man pages for details.
-n 100 will give each touch 100 filenames as arguments at max
-P 2 will run up to two touch commands in parallel.
"""
touch_cmd=lambda cpath: 'find '+cpath+' -type f -print0 | xargs -0 -n 100 -P 2 touch -m'
for cfile in keepfiles.values():
print ('Touching:',cfile)
os.system(touch_cmd(cfile))
"""
Reconstructs the image folder based on a list of sample names
"""
start_dir='/sls/X02DA/data/e14570/Data10/'
from subprocess import call,Popen,PIPE
import os
fancyExec=lambda cmd: Popen(cmd.split(' '), stdin=PIPE, stdout=PIPE, stderr=PIPE).communicate()[0].split('\n')
clean_line = lambda lines: dict(filter(lambda cline: len(cline[0])>0,map(lambda cline: (cline.upper().strip(),cline.strip()),lines)))
keeplines = clean_line(open('keepfiles.txt').readlines()).keys()
alllines = clean_line(fancyExec('find '+start_dir+' -mindepth 2 -maxdepth 2 -type d'))
import re
line_to_re = lambda line: lambda other_str: re.compile('.*'+line+'.*').match(other_str) is not None
keepmatches = map(line_to_re,keeplines)
any = lambda clist: len(filter(lambda x: x,clist))>0 # old versions of python dont have this function
anymatch = lambda cline: any(map(lambda match_fun: match_fun(cline[0]),keepmatches))
keepfiles = dict(filter(anymatch,alllines.items()))
print ('Files to be reconstructed:',keepfiles)
"""
Template command taken from Grecoman
/afs/psi/project/TOMCAT_pipeline/Devel/tomcat_pipeline/src/prj2sinSGE.sh -d -k 1 -I 3 -R 0 -g 0 -Z 0.5 -n -0.0001 -x 0.0001 -F parzen -t 8 --jobname=Ada_5_reco -O /sls/X02DA/data/e14570/Data10/disk5/Ada_5/rec_8bitB/ /sls/X02DA/data/e14570/Data10/disk5/Ada_5/sin/;
"""
job_prefix='/afs/psi/project/TOMCAT_pipeline/Devel/tomcat_pipeline/src/prj2sinSGE.sh -d -k 1 -I 3 -R 0 -g 0 -Z 0.5 -n -0.0001 -x 0.0001 -F parzen -t 8 --jobname='
touch_cmd=lambda cpath: job_prefix+cpath.split('/')[-1]+' -O '+cpath+'/rec_8bitB/ '+cpath+'/sin/;'
for cfile in keepfiles.values():
print ('Reconstructing:',cfile)
print ('Executing:',touch_cmd(cfile))
os.system(touch_cmd(cfile))
"""
Compresses the image folder into a tar based on a list of sample names
"""
start_dir='/sls/X02DA/data/e14570/Data10/'
rec_name='rec_8bitB'
from subprocess import call,Popen,PIPE
import os
fancyExec=lambda cmd: Popen(cmd.split(' '), stdin=PIPE, stdout=PIPE, stderr=PIPE).communicate()[0].split('\n')
clean_line = lambda lines: dict(filter(lambda cline: len(cline[0])>0,map(lambda cline: (cline.upper().strip(),cline.strip()),lines)))
keeplines = clean_line(open('keepfiles.txt').readlines()).keys()
alllines = clean_line(fancyExec('find '+start_dir+' -mindepth 2 -maxdepth 2 -type d'))
import re
line_to_re = lambda line: lambda other_str: re.compile('.*'+line+'.*').match(other_str) is not None
keepmatches = map(line_to_re,keeplines)
any = lambda clist: len(filter(lambda x: x,clist))>0 # old versions of python dont have this function
anymatch = lambda cline: any(map(lambda match_fun: match_fun(cline[0]),keepmatches))
keepfiles = dict(filter(anymatch,alllines.items()))
print ('Files to be tarred:',keepfiles)
tar_args = 'cvf' # adding/removing z does it without compression
job_prefix='/bin/tar -'+tar_args+' '
touch_cmd=lambda cpath: job_prefix+start_dir+'/'+cpath.split('/')[-1]+'.tar '+cpath+'/'+rec_name+';'
for cfile in keepfiles.values():
print ('Compressing:',cfile)
print ('Executing:',touch_cmd(cfile))
os.system(touch_cmd(cfile))
#
# To download cifex command line:
# curl -o cifex.zip https://cifex.ethz.ch/cifex_cli.zip
# unzip cifex.zip
#
# To setup before running
# use address https://cifex.ethz.ch (no 8443 from inside psi)
# cifex/cifex.sh init
# cifex/cifex.sh login
#
# To download cifex GUI for single files
# curl -o cifexapp.zip https://cifex.ethz.ch/CIFEXApp.zip
# unzip cifexapp.zip
# who to send the links to (probably not these people)
emailuser="steven.jansen@uni-ulm.de,B.Choat@uws.edu.au,kevinmader@gmail.com"
finisheddir="finished/"
mkdir $finisheddir
for cur_file in *.tar
do
new_file="$finisheddir/${cur_file}"
mv $cur_file $new_file
echo "Sending $cur_file to $emailuser"
cifex/cifex.sh upload $new_file -r $emailuser -c "File:$cur_file from the TOMCAT Beamline, compressed as a tar file"
done
Acer_001c
Acer_001d
Acer_005a
Acer_008a
Acer_009
Acer_009b
Acer_009c
Acer_22c
acer_leaf7
acer_leaf8
Acer_LS_01
Acer_new_03
Acer_new_11
Ada_4
Ada_5
Ada_6
Gum_40d
Gum_53
Gum_53relaxed
pine_needle58
pine_seedling_01
Pter_4
Pter_12
Pter_13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment