Exemplo n.º 1
0
def filter_normalization(obj, lothreshold, hithreshold, config=None):
    """
    This function takes an object with normalization integration information
    and a threshold and creates a mask file containing the pixel IDs that do
    not make it above the threshold.

    @param obj: The object containing the normalization information
    @type obj: C{SOM.SOM}

    @param lothreshold: The value below which a pixel will be masked.
    @type lothreshold: C{float}

    @param hithreshold: The value above which a pixel will be masked.
    @type hithreshold: C{float}    

    @param config: The object holding the DR configuration
    @type config: L{hlr_utils.Configure}


    @raise TypeError: The incoming object is not a C{SOM}.
    """
    import hlr_utils

    o_descr = hlr_utils.get_descr(obj)
    if o_descr != "SOM":
        raise TypeError("Only SOMs are allowed in this function!")

    if config is None:
        # Make mask file name from object information
        instname = obj.attr_list.inst.get_name()
        runnum = obj.attr_list["run_number"]
        outfile = "%s_%s_mask.dat" % (instname, str(runnum))
    else:
        # Make mask file name from configuration information
        outfile = hlr_utils.ext_replace(config.output, config.ext_replacement,
                                        "dat")
        outfile = hlr_utils.add_tag(outfile, "mask")

    ofile = open(outfile, "w")

    import SOM
    import utils

    len_obj = hlr_utils.get_length(obj)
    for i in xrange(len_obj):
        norm = hlr_utils.get_value(obj, i, o_descr)
        if utils.compare(norm, lothreshold) <= 0 or \
               utils.compare(norm, hithreshold) >= 0:
            map_so = hlr_utils.get_map_so(obj, None, i)
            pix_id = SOM.NeXusId.fromString(str(map_so.id)).toJoinedStr()

            print >> ofile, pix_id

    ofile.close()
Exemplo n.º 2
0
def filter_normalization(obj, lothreshold, hithreshold, config=None):
    """
    This function takes an object with normalization integration information
    and a threshold and creates a mask file containing the pixel IDs that do
    not make it above the threshold.

    @param obj: The object containing the normalization information
    @type obj: C{SOM.SOM}

    @param lothreshold: The value below which a pixel will be masked.
    @type lothreshold: C{float}

    @param hithreshold: The value above which a pixel will be masked.
    @type hithreshold: C{float}    

    @param config: The object holding the DR configuration
    @type config: L{hlr_utils.Configure}


    @raise TypeError: The incoming object is not a C{SOM}.
    """
    import hlr_utils

    o_descr = hlr_utils.get_descr(obj)
    if o_descr != "SOM":
        raise TypeError("Only SOMs are allowed in this function!")

    if config is None:
        # Make mask file name from object information
        instname = obj.attr_list.inst.get_name()
        runnum = obj.attr_list["run_number"]
        outfile = "%s_%s_mask.dat" % (instname, str(runnum))
    else:
        # Make mask file name from configuration information
        outfile = hlr_utils.ext_replace(config.output, config.ext_replacement, "dat")
        outfile = hlr_utils.add_tag(outfile, "mask")

    ofile = open(outfile, "w")

    import SOM
    import utils

    len_obj = hlr_utils.get_length(obj)
    for i in xrange(len_obj):
        norm = hlr_utils.get_value(obj, i, o_descr)
        if utils.compare(norm, lothreshold) <= 0 or utils.compare(norm, hithreshold) >= 0:
            map_so = hlr_utils.get_map_so(obj, None, i)
            pix_id = SOM.NeXusId.fromString(str(map_so.id)).toJoinedStr()

            print >> ofile, pix_id

    ofile.close()
Exemplo n.º 3
0
def merge_roi_files(filelist, conf):
    """
    This function takes a set of ROI files and combines them into a new ROI
    file.

    @param filelist: The set of ROI file names.
    @type filelist: C{list}

    @param conf: The object containing the current configuration.
    @type conf: L{hlr_utils.Configure}


    @return: The filename of the combined ROI
    @rtype: C{string}
    """
    import hlr_utils
    # Get first set of pixel IDs
    rfile = open(filelist[0], 'r')
    roi_set = set([rid.strip() for rid in rfile])
    rfile.close()

    # Merge each of the other sets of pixel IDs
    for filename in filelist[1:]:
        rfile = open(filename, 'r')
        for rid in rfile:
            roi_set.add(rid.strip())
        rfile.close()

    # Create output filename
    import os
    if conf.path_replacement is None:
        path_replacement = os.getcwd()
    else:
        path_replacement = conf.path_replacement

    ofilename = os.path.join(path_replacement, os.path.basename(filelist[0]))
    ofilename = hlr_utils.add_tag(ofilename, "comb")
    
    # Create new ROI file from combined information
    ofile = open(ofilename, 'w')
    for rid in roi_set:
        print >> ofile, rid
    ofile.close()
    
    return ofilename
Exemplo n.º 4
0
def merge_roi_files(filelist, conf):
    """
    This function takes a set of ROI files and combines them into a new ROI
    file.

    @param filelist: The set of ROI file names.
    @type filelist: C{list}

    @param conf: The object containing the current configuration.
    @type conf: L{hlr_utils.Configure}


    @return: The filename of the combined ROI
    @rtype: C{string}
    """
    import hlr_utils
    # Get first set of pixel IDs
    rfile = open(filelist[0], 'r')
    roi_set = set([rid.strip() for rid in rfile])
    rfile.close()

    # Merge each of the other sets of pixel IDs
    for filename in filelist[1:]:
        rfile = open(filename, 'r')
        for rid in rfile:
            roi_set.add(rid.strip())
        rfile.close()

    # Create output filename
    import os
    if conf.path_replacement is None:
        path_replacement = os.getcwd()
    else:
        path_replacement = conf.path_replacement

    ofilename = os.path.join(path_replacement, os.path.basename(filelist[0]))
    ofilename = hlr_utils.add_tag(ofilename, "comb")

    # Create new ROI file from combined information
    ofile = open(ofilename, 'w')
    for rid in roi_set:
        print >> ofile, rid
    ofile.close()

    return ofilename
Exemplo n.º 5
0
def write_file(filename, dst_type, data, **kwargs):
    """
    This function performs the steps necessary to write an output file. One
    can pass a data filename or an output filename. If a data filename is
    passed, the data file extension, output file extension and the replace
    keyword must be passed. The expected data object to write to the file is
    a C{SOM}. B{NOTE}: Extra keyword arguments can be passed onto the C{DST}
    instance via calling them in the kwargs list. Those arguments will not be
    processed by this function, but just pass them on.

    @param filename: The name of the data file from which the output is
                     generated or the name of an output file
    @type filename: C{string}
    
    @param dst_type: The MIME type of the output formatter
    @type dst_type: C{string}
    
    @param data: Object that contains the output to be written to file
    @type data: C{SOM.SOM}
    
    @param kwargs: A list of keyword arguments that the function accepts:
    
    @keyword message: This is part of the message that will be printed to
                      STDOUT if verbose keyword is set to True. The default
                      message is \"output file\"
    @type message: C{string}
    
    @keyword data_ext: This is the extension on the data file. This is used in
                       conjunction with output_ext and replace to convert the
                       data filename into an output filename. The default
                       value is \"nxs\".
    @type data_ext: C{string}
    
    @keyword output_ext: This is the extension to be used for the output file.
                         The default value is \"txt\".
    @type output_ext: C{string}
    
    @keyword verbose: This determines whether or not the print statement is
    executed. The default value is I{False}.
    @type verbose: C{boolean}
    
    @keyword replace_ext: This determines whether or not the extension on the
                          incoming filename is replaced with output_ext. The
                          default behavior is I{True} (replace extension)
    @type replace_ext: C{boolean}
    
    @keyword replace_path: This determines whether or not the directory path on
                           the incoming filename is replaced with the
                           directory where the driver is running. The default
                           behavior is I{True} (replace path)
    @type replace_path: C{boolean}
    
    @keyword path_replacement: This is a directory path that will be prepended
                               to the output filename. The default value is
                               C{None} and will cause the working directory to
                               be the prepended path.
    @type path_replacement: C{string}
    
    @keyword extra_tag: This is a tag that will be inserted into the file name
                        just before the file extension.
    @type extra_tag: C{string}

    @keyword getsom_kwargs: This is a collection of keyword arguments that
                            are to be passed to the writeSOM function call.
    @type getsom_kwargs: C{dict}
    """

    import os

    import DST
    import hlr_utils

    try:
        message = kwargs["message"]
    except KeyError:
        message = "output file"

    try:
        data_ext = kwargs["data_ext"]
    except KeyError:
        data_ext = "nxs"

    try:
        output_ext = kwargs["output_ext"]
    except KeyError:
        output_ext = "txt"

    try:
        verbose = kwargs["verbose"]
    except KeyError:
        verbose = False

    try:
        replace_path = kwargs["replace_path"]
    except KeyError:
        replace_path = True

    try:
        path_replacement = kwargs["path_replacement"]
    except KeyError:
        path_replacement = None       

    try:
        replace_ext = kwargs["replace_ext"]
    except KeyError:
        replace_ext = True        

    try:
        extra_tag = kwargs["extra_tag"]
    except KeyError:
        extra_tag = None

    try:
        arguments = kwargs["arguments"]
    except KeyError:
        arguments = None

    getsom_kwargs = kwargs.get("getsom_kwargs", {})

    if replace_path:
        if path_replacement is None:
            path_replacement = os.getcwd()
            
        fixed_filename = os.path.join(path_replacement,
                                      os.path.basename(filename))
    else:
        fixed_filename = filename

    if replace_ext:
        fixed_filename = hlr_utils.ext_replace(fixed_filename, data_ext,
                                               output_ext)
    else:
        pass

    if extra_tag is not None:
        fixed_filename = hlr_utils.add_tag(fixed_filename, extra_tag)

    # Handle difference between NeXus and other files
    if dst_type != "application/x-RedNxs":
        resource = open(fixed_filename, "w")
    else:
        resource = fixed_filename
        
    output_dst = DST.getInstance(dst_type, resource, arguments, **kwargs)
    if verbose:
        print "Writing %s" % message

    output_dst.writeSOM(data, **getsom_kwargs)
    output_dst.release_resource()