Пример #1
0
def diagnose(white_run, sample_run=None, other_white=None, remove_zero=None, 
             tiny=None, large=None, median_lo=None, median_hi=None, signif=None, 
             bkgd_threshold=None, bkgd_range=None, variation=None,
             bleed_test=False, bleed_maxrate=None, bleed_pixels=None,
             hard_mask=None, print_results=False, inst_name=None):
    """
    Run diagnostics on the provided run and white beam files.

    There are 4 possible tests, depending on the input given:
      White beam diagnosis
      Background tests
      Second white beam
      Bleed test
    
    Required inputs:
    
      white_run  - The run number or filepath of the white beam run
    
    Optional inputs:
      sample_run - The run number or filepath of the sample run for the background test (default = None)
      other_white   - If provided an addional set of tests is performed on this file. (default = None)
      remove_zero - If true then zeroes in the data will count as failed (default = False)
      tiny          - Minimum threshold for acceptance (default = 1e-10)
      large         - Maximum threshold for acceptance (default = 1e10)
      median_lo     - Fraction of median to consider counting low (default = 0.1)
      median_hi     - Fraction of median to consider counting high (default = 3.0)
      signif           - Counts within this number of multiples of the 
                      standard dev will be kept (default = 3.3)
      bkgd_threshold - High threshold for background removal in multiples of median (default = 5.0)
      bkgd_range - The background range as a list of 2 numbers: [min,max]. 
                   If not present then they are taken from the parameter file. (default = None)
      variation  - The number of medians the ratio of the first/second white beam can deviate from
                   the average by (default=1.1)
      bleed_test - If true then the CreatePSDBleedMask algorithm is run
      bleed_maxrate - If the bleed test is on then this is the maximum framerate allowed in a tube
      bleed_pixels - If the bleed test is on then this is the number of pixels ignored within the
                     bleed test diagnostic
      hard_mask  - A file specifying those spectra that should be masked without testing
      print_results - If True then the results are printed to std out
      inst_name  - The name of the instrument to perform the diagnosis.
                   If it is not provided then the default instrument is used (default = None)
    """
    # Set the default instrument so that Mantid can search for the runs correctly, 
    # but switch it back at the end
    def_inst = mtd.settings["default.instrument"]
    if inst_name == None:
        inst_name = def_inst
    elif inst_name != def_inst:
        mtd.settings["default.instrument"] = inst_name
    else: pass

    # Load the hard mask file if necessary
    hard_mask_spectra = ''
    if hard_mask is not None:
        hard_mask_spectra = common.load_mask(hard_mask)

    # Map the test number to the results
    # Each element is the mask workspace name then the number of failures
    test_results = [ [None, None], [None, None], [None, None], [None, None]]

    ##
    ## Accumulate the masking on this workspace
    ##
    # What shall we call the output
    lhs_names = lhs_info('names')
    if len(lhs_names) > 0:
        diag_total_mask = lhs_names[0]
    else:
        diag_total_mask = 'diag_total_mask'

    ##
    ## White beam Test
    ##
    white_counts = None
    if white_run is not None and str(white_run) != '':
        # Load and integrate
        data_ws = common.load_run(white_run)
        # Make sure we have a matrix workspace otherwise Integration() returns all zeros.
        ConvertToMatrixWorkspace(data_ws, data_ws)
        white_counts = Integration(data_ws, "__counts_white-beam").workspace()
        MaskDetectors(white_counts, SpectraList=hard_mask_spectra)
        # Run first white beam tests
        __white_masks, num_failed = \
                      _do_white_test(white_counts, tiny, large, median_lo, median_hi, signif)
        test_results[0] = [str(__white_masks), num_failed]
        diag_total_mask = CloneWorkspace(__white_masks, diag_total_mask).workspace()
    else:
        raise RuntimeError('Invalid input for white run "%s"' % str(white_run))

    ##
    ## Second white beam Test
    ##
    second_white_counts = None
    if other_white is not None and str(other_white) != '':
        # Load and integrate
        data_ws = common.load_run(other_white)
        second_white_counts = Integration(data_ws, "__counts_white-beam2").workspace()
        MaskDetectors(white_counts, SpectraList=hard_mask_spectra)
        # Run tests
        __second_white_masks, num_failed = \
                             _do_second_white_test(white_counts, second_white_counts,
                                                   tiny, large, median_lo, median_hi,
                                                   signif,variation)
        test_results[1] = [str(__second_white_masks), num_failed]
        # Accumulate
        diag_total_mask += __second_white_masks

    ##
    ## Tests on the sample(s)
    ##
    if sample_run is not None and sample_run != '':
        if type(sample_run) != list:
            sample_run = [sample_run]
        __bkgd_masks = None
        __bleed_masks = None
        bkgd_failures = 0
        bleed_failures = 0
        for sample in sample_run:
            ## Background test
            __bkgd_masks_i, failures = _do_background_test(sample, white_counts, second_white_counts,
                                                           bkgd_range, bkgd_threshold, remove_zero, signif,
                                                           hard_mask_spectra)
            bkgd_failures += failures
            __bkgd_masks = _accumulate(__bkgd_masks,__bkgd_masks_i)
                
            ## PSD Bleed Test
            if type(bleed_test) == bool and bleed_test == True:
                __bleed_masks_i, failures = _do_bleed_test(sample, bleed_maxrate, bleed_pixels)
                bleed_failures += failures
                __bleed_masks = _accumulate(__bleed_masks, __bleed_masks_i)

        ## End of sample loop
        test_results[2] = [str(__bkgd_masks), bkgd_failures]
        diag_total_mask += __bkgd_masks
        if __bleed_masks is not None:
            test_results[3] = [str(__bleed_masks), bleed_failures]
            diag_total_mask += __bleed_masks

    ## End of background test

    # Remove temporary workspaces
    mtd.deleteWorkspace(str(white_counts))
    if second_white_counts is not None:
        mtd.deleteWorkspace(str(second_white_counts))
    
    # Revert our default instrument changes if necessary
    if inst_name != def_inst:
        mtd.settings["default.instrument"] = def_inst

    if print_results:
        print_test_summary(test_results)
    
    # This will be a MaskWorkspace which contains the accumulation of all of the masks
    return diag_total_mask