Exemple #1
0
def main(par):

    f1 = sys.argv[1] + ' '
    f2 = sys.argv[2]

    f1_type = ivlad.getout('sfgettype', stdin=f1)
    if f1_type != 'SF_FLOAT':
        raise m8rex.TypeHandlingNotImplemented(f1_type)

    dims_str = ivlad.getout('sffiledims', 'parform=n', f1)
    ndims = int(dims_str.split(':')[0])

    cmd = 'sfspike mag=0 '
    fields = 'n o d unit label'.split()

    for i in range(ndims):
        for z in fields:
            cmd += ivlad.getout('sfget', ['parform=y', z + str(i + 1)],
                                f1) + ' '

    cmd += ' >' + f2

    ivlad.exe(cmd)

    return ivlad.unix_success
Exemple #2
0
def __run(prog, args, inp, out, verb, exe, postproc=None):

    prog = prog.strip()
    if args == None:
        args = ''
    else:
        args = args.strip()
    cmd = os.path.join(rsf.prog.RSFROOT, 'bin', prog)

    if exe == 'g':  # get output
        assert out == None
        out_str = ivlad.getout(prog, args, inp, verb)
        if postproc == None:
            return out_str
        else:
            return postproc(out_str)
    elif exe == 'p':  # call part of a pipe
        if args != '':
            cmd += ' ' + args
        return cmd
    else:
        if inp != None:
            cmd += ' <' + inp.strip()
        if out != None:
            cmd += ' >' + out.strip()
        if args != '':
            cmd += ' ' + args
        if exe == 'l':  # log
            return cmd
        else:  # exe=='x', execute
            ivlad.exe(cmd, verb)
Exemple #3
0
def __run(prog, args, inp, out, verb, exe, postproc=None):
    
    prog = prog.strip()
    if args == None:
        args = ''
    else:
        args = args.strip()
    cmd = os.path.join(rsf.prog.RSFROOT,'bin',prog)

    if exe == 'g': # get output
        assert out == None
        out_str = ivlad.getout(prog, args, inp, verb)
        if postproc == None:
            return out_str
        else:
            return postproc(out_str)
    elif exe == 'p': # call part of a pipe
        if args != '':
            cmd += ' ' + args
        return cmd
    else:
        if inp != None:
            cmd += ' <' + inp.strip()
        if out != None:
            cmd += ' >' + out.strip()
        if args != '':
            cmd +=  ' ' + args
        if exe == 'l': # log
            return cmd
        else: # exe=='x', execute
            ivlad.exe(cmd, verb)
Exemple #4
0
def main(par):

    prog = par.string('prog') # Non-madagascar utility
    inp  = par.string('inp') # Input file
    tpar_str = par.string('tpar') # Translated params, i.e.: "ni1=n1 ni2=n2"
    ipar_str = par.string('ipar') # Independent params, i.e. "perc=100 cmap=rgb"
    verb = par.bool('verb',False)

    tpar_out = ''
    if tpar_str != None:
        for tpar in tpar_str.split():
            thispar = tpar.split('=')
            tpar_out += thispar[0] + '=' + \
                ivlad.getout('sfget', ['parform=n', thispar[1]], inp, verb) +' '
           
    data = ivlad.getout('sfin',['info=n',inp])

    ivlad.exe(prog + ' <' + data + ' ' + tpar_out + ' ' + ipar_str, verb)
    
    return ivlad.unix_success
Exemple #5
0
def main(par):

    verb = par.bool('verb',
                    False)  # Display headers and binaries being deleted
    mydir = par.string('dir')  # Directory with files
    recursive = par.bool('rec', False)  # Whether to go down recursively

    # Clean up headers with existing binaries

    valid_files_list = []

    if recursive:
        for root, dirs, files in mydir:
            valid_files_list += \
            ivlad.list_valid_rsf_files(root, files, chk4nan)
    else:
        files = [x for x in os.listdir(mydir) if os.path.isfile(x)]
        valid_files_list += \
        ivlad.list_valid_rsf_files(mydir, files, chk4nan=False)

    for f in valid_files_list:
        ivlad.msg(f + ': ' + ivlad.getout('sfin', ['info=n', f]), verb)
        sf.rm(f, verb)

    # Clean up headers with no binaries

    if recursive:
        hdr_str = ivlad.getout('find',
                               [mydir, '-type', 'f', '-name', '"*.rsf"'])
        hdr_list = hdr_str.split('\n')
    else:
        hdr_list = [
            x for x in glob.glob(os.path.join(mydir, '*.rsf'))
            if os.path.isfile(x)
        ]

    for f in hdr_list:
        ivlad.msg(f)
        os.remove(f)

    return ivlad.unix_success
Exemple #6
0
def main(par):

    prog = par.string('prog')  # Non-madagascar utility
    inp = par.string('inp')  # Input file
    tpar_str = par.string('tpar')  # Translated params, i.e.: "ni1=n1 ni2=n2"
    ipar_str = par.string(
        'ipar')  # Independent params, i.e. "perc=100 cmap=rgb"
    verb = par.bool('verb', False)

    tpar_out = ''
    if tpar_str != None:
        for tpar in tpar_str.split():
            thispar = tpar.split('=')
            tpar_out += thispar[0] + '=' + \
                ivlad.getout('sfget', ['parform=n', thispar[1]], inp, verb) +' '

    data = ivlad.getout('sfin', ['info=n', inp])

    ivlad.exe(prog + ' <' + data + ' ' + tpar_out + ' ' + ipar_str, verb)

    return ivlad.unix_success
Exemple #7
0
def main(par):

    verb = par.bool('verb', False)

    inp = par.string('inp')  # ifile.rsf

    out_basenm = os.path.basename(inp).rstrip(ivlad.ext) + '_split'

    outdir = par.string('outdir',
                        (out_basenm + ivlad.ext))  # Default is ifile_split.rsf

    nthick = par.int('nthick', 1)  # slice thickness

    ivlad.chk_param_limit(nthick, 'nthick', 1, '>')

    lastdim = str(ivlad.ndims(inp))

    n = int(ivlad.getout('sfget', ['parform=n', 'n' + lastdim], inp))
    ivlad.chk_param_limit(nthick, 'nthick', n, '<')

    if not os.path.isdir(outdir):
        os.mkdir(outdir)

    # Slices will be called 01.rsf, etc. Avoid datapath aliasing:
    dpath = 'datapath=$DATAPATH/' + out_basenm + '_'

    nslices_whole = int(math.floor(n / nthick))

    f = 0

    for i in range(nslices_whole):

        i_str = ivlad.add_zeros(i, nslices_whole)
        i_slc = os.path.join(outdir, i_str + '_stdout' + ivlad.ext)

        cmd = '<%s sfwindow f%s=%d ' % (inp, lastdim, f)
        cmd += 'n%s=%d %s > %s' % (lastdim, nthick, dpath, i_slc)
        ivlad.exe(cmd, verb)

        f += nthick

    ist = n - nslices_whole * nthick  # Incomplete Slice Thickness

    if ist > 0:
        i_str = ivlad.add_zeros(i + 1, nslices_whole)
        i_slc = os.path.join(outdir, i_str + '_stdout' + ivlad.ext)

        cmd = '<%s sfwindow f%s=%d ' % (inp, lastdim, f)
        cmd += 'n%s=%d > %s' % (lastdim, ist, i_slc)
        ivlad.exe(cmd, verb)

    return ivlad.unix_success
Exemple #8
0
def main(par):

    verb = par.bool('verb', False)

    inp = par.string('inp') # ifile.rsf

    out_basenm = os.path.basename(inp).rstrip(ivlad.ext) + '_split'

    outdir = par.string('outdir',(out_basenm+ivlad.ext)) # Default is ifile_split.rsf

    nthick = par.int('nthick', 1) # slice thickness

    ivlad.chk_param_limit(nthick, 'nthick', 1, '>')

    lastdim = str(ivlad.ndims(inp))

    n = int(ivlad.getout('sfget',['parform=n','n'+lastdim], inp))
    ivlad.chk_param_limit(nthick, 'nthick', n, '<')

    if not os.path.isdir(outdir):
        os.mkdir(outdir)

    # Slices will be called 01.rsf, etc. Avoid datapath aliasing:
    dpath = 'datapath=$DATAPATH/' + out_basenm + '_'

    nslices_whole = int(math.floor(n/nthick))

    f = 0

    for i in range(nslices_whole):

        i_str = ivlad.add_zeros(i,nslices_whole)
        i_slc = os.path.join(outdir, i_str + '_stdout' + ivlad.ext)

        cmd = '<%s sfwindow f%s=%d ' % (inp, lastdim,f)
        cmd += 'n%s=%d %s > %s' % (lastdim, nthick, dpath, i_slc)
        ivlad.exe(cmd, verb)

        f += nthick

    ist = n - nslices_whole * nthick # Incomplete Slice Thickness

    if ist > 0:
        i_str = ivlad.add_zeros(i+1,nslices_whole)
        i_slc = os.path.join(outdir, i_str + '_stdout' + ivlad.ext)

        cmd = '<%s sfwindow f%s=%d ' % (inp, lastdim, f)
        cmd += 'n%s=%d > %s' % (lastdim, ist, i_slc)
        ivlad.exe(cmd, verb)

    return ivlad.unix_success
Exemple #9
0
def main(par):

    verb = par.bool('verb', False) # Display headers and binaries being deleted
    mydir = par.string('dir')           # Directory with files
    recursive = par.bool('rec', False)  # Whether to go down recursively

    # Clean up headers with existing binaries

    valid_files_list = []

    if recursive:
        for root, dirs, files in mydir:
            valid_files_list += \
            ivlad.list_valid_rsf_files(root, files, chk4nan)
    else:
        files = filter(lambda x:os.path.isfile(x),os.listdir(mydir))
        valid_files_list += \
        ivlad.list_valid_rsf_files(mydir, files, chk4nan=False)

    for f in valid_files_list:
        ivlad.msg(f + ': ' + ivlad.getout('sfin',['info=n',f]), verb)
        sf.rm(f, verb)

    # Clean up headers with no binaries

    if recursive:
        hdr_str = ivlad.getout('find',[mydir, '-type', 'f', '-name', '"*.rsf"'])
        hdr_list = hdr_str.split('\n')
    else:
        hdr_list = filter(lambda x:os.path.isfile(x),
                          glob.glob(os.path.join(mydir,'*.rsf')))

    for f in hdr_list:
        ivlad.msg(f)
        os.remove(f)

    return ivlad.unix_success
Exemple #10
0
def main(par):

    f1 = sys.argv[1] + ' '
    f2 = sys.argv[2]

    f1_type = ivlad.getout('sfgettype', stdin=f1)
    if f1_type != 'SF_FLOAT':
        raise m8rex.TypeHandlingNotImplemented(f1_type)

    dims_str = ivlad.getout('sffiledims','parform=n',f1)
    ndims = int(dims_str.split(':')[0])

    cmd = 'sfspike mag=0 '
    fields = 'n o d unit label'.split()

    for i in range(ndims):
        for z in fields:            
            cmd += ivlad.getout('sfget',['parform=y',z+str(i+1)],f1) + ' '

    cmd += ' >' + f2

    ivlad.exe(cmd)

    return ivlad.unix_success
Exemple #11
0
def main(par):

    inp = par.string('inp')  # input file
    out = par.string('out')  # output file

    verb = par.bool('verb', False)  # if y, print system commands, outputs
    pclip = par.float('pclip', 99)  # percentile clip

    if pclip < 0 or pclip > 100:
        raise m8rex.ParamOutOfRange('pclip', 0, 100)

    prog_nm_root = os.path.join(RSFROOT, 'bin', 'sf')
    sfquantile = prog_nm_root + 'quantile'
    sfclip = prog_nm_root + 'clip'

    clip = ivlad.getout('sfquantile', 'pclip=' + str(pclip), inp, verb, True)
    sf.clip(inp, out, clip, verb)

    return ivlad.unix_success
Exemple #12
0
def main(par):

    inp = par.string('inp') # input file
    out = par.string('out') # output file

    verb = par.bool('verb', False) # if y, print system commands, outputs
    pclip = par.float('pclip',99)  # percentile clip

    if pclip < 0 or pclip > 100:
        raise m8rex.ParamOutOfRange('pclip',0,100)

    prog_nm_root = os.path.join(RSFROOT,'bin','sf')
    sfquantile = prog_nm_root + 'quantile'
    sfclip     = prog_nm_root + 'clip'

    clip = ivlad.getout('sfquantile', 'pclip='+str(pclip), inp, verb, True)
    sf.clip(inp, out, clip, verb)

    return ivlad.unix_success
Exemple #13
0
def main(par):

    verb = par.bool('verb', False)      # Display what is wrong with the dataset
    mydir = par.string('dir','.')       # Directory with files
    recursive = par.bool('rec', False)  # Whether to go down recursively
    chk4nan = par.bool('chk4nan',False) # Check for NaN values. Expensive!!

    valid_files_list = []

    if recursive:
        for root, dirs, files in os.walk(mydir):
            valid_files_list += \
            ivlad.list_valid_rsf_files(root, files, chk4nan)
    else:
        files = [x for x in os.listdir(mydir) if os.path.isfile(x)]
        valid_files_list += \
        ivlad.list_valid_rsf_files(mydir, files, chk4nan)

    for f in valid_files_list:
        bfile = ivlad.getout('sfin',['info=n',f])
        ivlad.msg(f + ': ' + bfile, verb)
        ivlad.exe('touch -c ' + bfile)

    return ivlad.unix_success
Exemple #14
0
def main(par):

    verb = par.bool('verb', False)      # Display what is wrong with the dataset
    mydir = par.string('dir','.')       # Directory with files
    recursive = par.bool('rec', False)  # Whether to go down recursively
    chk4nan = par.bool('chk4nan',False) # Check for NaN values. Expensive!!

    valid_files_list = []

    if recursive:
        for root, dirs, files in os.walk(mydir):
            valid_files_list += \
            ivlad.list_valid_rsf_files(root, files, chk4nan)
    else:
        files = filter(lambda x:os.path.isfile(x),os.listdir(mydir))
        valid_files_list += \
        ivlad.list_valid_rsf_files(mydir, files, chk4nan)

    for f in valid_files_list:
        bfile = ivlad.getout('sfin',['info=n',f])
        ivlad.msg(f + ': ' + bfile, verb)
        ivlad.exe('touch -c ' + bfile)

    return ivlad.unix_success
Exemple #15
0
def main(par):

    inp = par.string('inp') # input file
    out = par.string('out') # output file

    verb = par.bool('verb', False) # if y, print system commands, outputs

    ivlad.chk_file_dims(inp, 2)

    n1 = ivlad.getout('sfget',['parform=n','n1'], inp, verb)
    n2 = ivlad.getout('sfget',['parform=n','n2'], inp, verb)
    n1 = int(n1)
    n2 = int(n2)

    h = par.int('h', 768)  # output height
    w = par.int('w', 1024) # output width
    has_h = par.string('h')
    has_w = par.string('w')

    if has_h and has_w: # both w and h were read. Check for sanity:
        ivlad.chk_param_limit(w, 'w')
        ivlad.chk_param_limit(h, 'h')
        if (h,w) == (n1,n2):
            ivlad.msg('Change h or w if you want out != inp')
            sf.cp(inp, out, verb)
            return ivlad.unix_success

    # Transform h and w to pixels, if they are not
    # No default value for par.string -- Quirk of rsf, replicated in rsfbak
    unit = par.string('unit', 'px') # unit of h and w. Can be: px, mm, cm, in
    ivlad.chk_par_in_list(unit,['mm','cm','in','px'])

    if unit != 'px':
        ppi = par.int('ppi') # outp. resolution (px/in). Necessary when unit!=px
        ivlad.chk_param_limit(ppi, 'ppi')
        # Transform w and h to px
        if unit == 'in':
            scale = 1
        elif unit == 'mm':
            scale = 254
        elif unit == 'cm':
            scale = 25.4
        w *= ppi / float(scale)
        h *= ppi / float(scale)
        # Don't worry, we'll convert to int after prar block
        del scale

    # Now h and w are in pixels

    # If prar=y, then h and/or w define a bounding box.
    # Find the dimensions of the image inside this box
    prar = par.bool('prar', True) # if y, PReserve Aspect Ratio of input
    if prar: # preserve aspect ratio
        if has_h and not has_w:
            w = n2 * float(h) / n1
        elif has_w and not has_h:
            h = n1 * float(w) / n2
        else: # Full bounding box specified
            hscale = float(h) / n1
            wscale = float(w) / n2
            if hscale < wscale:
                w = n2 * hscale
            else:
                h = n1 * wscale

    h = int(h)
    w = int(w)

    assert h > 1
    assert w > 1

    h = ivlad.valswitch(h, n1, None)
    w = ivlad.valswitch(w, n2, None)
    tmp = tempfile.mktemp(dir=rsf.path.datapath())

    # Interpolation and, if needed, bandpass 
    if h != None:
        d1 = ivlad.getout('sfget', ['parform=n','d1'], inp, verb)
        d1 = float(d1) * (n1-1)/float(h-1)
        if h < n1:
            ready_for_remap_1 = tmp + '1'
            sf.bandpass(inp, ready_for_remap_1, fhi=0.5/d1, verb=verb)
            rem2del_junk1 = True
        else:
            ready_for_remap_1 = inp
            rem2del_junk1 = False
        if w:
            out_remap1 = tmp + '2'
            rem2del_junk2 = True
        else:
            out_remap1 = out
            rem2del_junk2 = False
        sf.remap1(ready_for_remap_1, out_remap1, n1=h, d1=d1, verb=verb)
        if rem2del_junk1:
            sf.rm(ready_for_remap_1, verb)
    else: # no action on axis 1
        out_remap1 = inp
        rem2del_junk2 = False

    if w != None:
        d2 = ivlad.getout('sfget', ['parform=n','d2'], inp, verb)
        d2 = float(d2) * (n2-1)/float(w-1)
        out_transp1 = tmp + '3'
        sf.transp(out_remap1, out_transp1, verb=verb)
        if rem2del_junk2:
            sf.rm(out_remap1, verb)
        if w < n2:
            ready_for_remap_2 = tmp + '4'
            sf.bandpass(out_transp1, ready_for_remap_2, fhi=0.5/d2, verb=verb)
            rem2del_junk4 = True
        else:
            ready_for_remap_2 = out_transp1
            rem2del_junk4 = False
        ready_for_transp2 = tmp + '5'
        sf.remap1(ready_for_remap_2,ready_for_transp2,n1=w,d1=d2,verb=verb)
        sf.rm(out_transp1, verb)
        if rem2del_junk4:
            sf.rm(ready_for_remap_2, verb)
        sf.transp(ready_for_transp2, out, verb=verb)
        sf.rm(ready_for_transp2, verb)

    return ivlad.unix_success
Exemple #16
0
def main(par):

    inp = par.string('inp')  # input file
    out = par.string('out')  # output file

    verb = par.bool('verb', False)  # if y, print system commands, outputs

    ivlad.chk_file_dims(inp, 2)

    n1 = ivlad.getout('sfget', ['parform=n', 'n1'], inp, verb)
    n2 = ivlad.getout('sfget', ['parform=n', 'n2'], inp, verb)
    n1 = int(n1)
    n2 = int(n2)

    h = par.int('h', 768)  # output height
    w = par.int('w', 1024)  # output width
    has_h = par.string('h')
    has_w = par.string('w')

    if has_h and has_w:  # both w and h were read. Check for sanity:
        ivlad.chk_param_limit(w, 'w')
        ivlad.chk_param_limit(h, 'h')
        if (h, w) == (n1, n2):
            ivlad.msg('Change h or w if you want out != inp')
            sf.cp(inp, out, verb)
            return ivlad.unix_success

    # Transform h and w to pixels, if they are not
    # No default value for par.string -- Quirk of rsf, replicated in rsfbak
    unit = par.string('unit', 'px')  # unit of h and w. Can be: px, mm, cm, in
    ivlad.chk_par_in_list(unit, ['mm', 'cm', 'in', 'px'])

    if unit != 'px':
        ppi = par.int(
            'ppi')  # outp. resolution (px/in). Necessary when unit!=px
        ivlad.chk_param_limit(ppi, 'ppi')
        # Transform w and h to px
        if unit == 'in':
            scale = 1
        elif unit == 'mm':
            scale = 254
        elif unit == 'cm':
            scale = 25.4
        w *= ppi / float(scale)
        h *= ppi / float(scale)
        # Don't worry, we'll convert to int after prar block
        del scale

    # Now h and w are in pixels

    # If prar=y, then h and/or w define a bounding box.
    # Find the dimensions of the image inside this box
    prar = par.bool('prar', True)  # if y, PReserve Aspect Ratio of input
    if prar:  # preserve aspect ratio
        if has_h and not has_w:
            w = n2 * float(h) / n1
        elif has_w and not has_h:
            h = n1 * float(w) / n2
        else:  # Full bounding box specified
            hscale = float(h) / n1
            wscale = float(w) / n2
            if hscale < wscale:
                w = n2 * hscale
            else:
                h = n1 * wscale

    h = int(h)
    w = int(w)

    assert h > 1
    assert w > 1

    h = ivlad.valswitch(h, n1, None)
    w = ivlad.valswitch(w, n2, None)
    tmp = tempfile.mktemp(dir=rsf.path.datapath())

    # Interpolation and, if needed, bandpass
    if h != None:
        d1 = ivlad.getout('sfget', ['parform=n', 'd1'], inp, verb)
        d1 = float(d1) * (n1 - 1) / float(h - 1)
        if h < n1:
            ready_for_remap_1 = tmp + '1'
            sf.bandpass(inp, ready_for_remap_1, fhi=0.5 / d1, verb=verb)
            rem2del_junk1 = True
        else:
            ready_for_remap_1 = inp
            rem2del_junk1 = False
        if w:
            out_remap1 = tmp + '2'
            rem2del_junk2 = True
        else:
            out_remap1 = out
            rem2del_junk2 = False
        sf.remap1(ready_for_remap_1, out_remap1, n1=h, d1=d1, verb=verb)
        if rem2del_junk1:
            sf.rm(ready_for_remap_1, verb)
    else:  # no action on axis 1
        out_remap1 = inp
        rem2del_junk2 = False

    if w != None:
        d2 = ivlad.getout('sfget', ['parform=n', 'd2'], inp, verb)
        d2 = float(d2) * (n2 - 1) / float(w - 1)
        out_transp1 = tmp + '3'
        sf.transp(out_remap1, out_transp1, verb=verb)
        if rem2del_junk2:
            sf.rm(out_remap1, verb)
        if w < n2:
            ready_for_remap_2 = tmp + '4'
            sf.bandpass(out_transp1,
                        ready_for_remap_2,
                        fhi=0.5 / d2,
                        verb=verb)
            rem2del_junk4 = True
        else:
            ready_for_remap_2 = out_transp1
            rem2del_junk4 = False
        ready_for_transp2 = tmp + '5'
        sf.remap1(ready_for_remap_2, ready_for_transp2, n1=w, d1=d2, verb=verb)
        sf.rm(out_transp1, verb)
        if rem2del_junk4:
            sf.rm(ready_for_remap_2, verb)
        sf.transp(ready_for_transp2, out, verb=verb)
        sf.rm(ready_for_transp2, verb)

    return ivlad.unix_success