def gen_triplets_master(wv_master, geometry=None, debugplot=0):
    """Compute information associated to triplets in master table.

    Determine all the possible triplets that can be generated from the
    array `wv_master`. In addition, the relative position of the
    central line of each triplet is also computed.

    Parameters
    ----------
    wv_master : 1d numpy array, float
        Array with wavelengths corresponding to the master table
        (Angstroms).
    geometry : tuple (4 integers) or None
        x, y, dx, dy values employed to set the window geometry.
    debugplot : int
        Determines whether intermediate computations and/or plots
        are displayed. The valid codes are defined in
        numina.array.display.pause_debugplot.

    Returns
    -------
    ntriplets_master : int
        Number of triplets built from master table.
    ratios_master_sorted : 1d numpy array, float
        Array with values of the relative position of the central line
        of each triplet, sorted in ascending order.
    triplets_master_sorted_list : list of tuples
        List with tuples of three numbers, corresponding to the three
        line indices in the master table. The list is sorted to be in
        correspondence with `ratios_master_sorted`.

    """

    nlines_master = wv_master.size

    # Check that the wavelengths in the master table are sorted
    wv_previous = wv_master[0]
    for i in range(1, nlines_master):
        if wv_previous >= wv_master[i]:
            raise ValueError('Wavelengths:\n--> ' +
                             str(wv_previous) + '\n--> ' + str(wv_master[i]) +
                             '\nin master table are duplicated or not sorted')
        wv_previous = wv_master[i]

    # Generate all the possible triplets with the numbers of the lines
    # in the master table. Each triplet is defined as a tuple of three
    # numbers corresponding to the three line indices in the master
    # table. The collection of tuples is stored in an ordinary python
    # list.
    iter_comb_triplets = itertools.combinations(range(nlines_master), 3)
    triplets_master_list = [val for val in iter_comb_triplets]

    # Verify that the number of triplets coincides with the expected
    # value.
    ntriplets_master = len(triplets_master_list)
    if ntriplets_master == comb(nlines_master, 3, exact=True):
        if abs(debugplot) >= 10:
            print('>>> Total number of lines in master table:', 
                  nlines_master)
            print('>>> Number of triplets in master table...:', 
                  ntriplets_master)
    else:
        raise ValueError('Invalid number of combinations')

    # For each triplet, compute the relative position of the central
    # line.
    ratios_master = np.zeros(ntriplets_master)
    for index, value in enumerate(triplets_master_list):
        i1, i2, i3 = value
        delta1 = wv_master[i2] - wv_master[i1]
        delta2 = wv_master[i3] - wv_master[i1]
        ratios_master[index] = delta1 / delta2

    # Compute the array of indices that index the above ratios in
    # sorted order.
    isort_ratios_master = np.argsort(ratios_master)

    # Simultaneous sort of position ratios and triplets.
    ratios_master_sorted = ratios_master[isort_ratios_master]
    triplets_master_sorted_list = [triplets_master_list[i]
                                   for i in isort_ratios_master]

    if abs(debugplot) in [21, 22]:
        # compute and plot histogram with position ratios
        bins_in = np.linspace(0.0, 1.0, 41)
        hist, bins_out = np.histogram(ratios_master, bins=bins_in)
        #
        from numina.array.display.matplotlib_qt import plt
        fig = plt.figure()
        ax = fig.add_subplot(111)
        width_hist = 0.8*(bins_out[1]-bins_out[0])
        center = (bins_out[:-1]+bins_out[1:])/2
        ax.bar(center, hist, align='center', width=width_hist)
        ax.set_xlabel('distance ratio in each triplet')
        ax.set_ylabel('Number of triplets')
        ax.set_title("Number of lines/triplets: " +
                     str(nlines_master) + "/" + str(ntriplets_master))
        # set window geometry
        set_window_geometry(geometry)
        pause_debugplot(debugplot, pltshow=True, tight_layout=True)

    return ntriplets_master, ratios_master_sorted, triplets_master_sorted_list
def gen_triplets_master ( wv_master , geometry = None , debugplot = 0 ) : nlines_master = wv_master . size wv_previous = wv_master [ 0 ] for i in range ( 1 , nlines_master ) : if wv_previous >= wv_master [ i ] : raise ValueError ( 'Wavelengths:\n--> ' + str ( wv_previous ) + '\n--> ' + str ( wv_master [ i ] ) + '\nin master table are duplicated or not sorted' ) wv_previous = wv_master [ i ] iter_comb_triplets = itertools . combinations ( range ( nlines_master ) , 3 ) triplets_master_list = [ val for val in iter_comb_triplets ] ntriplets_master = len ( triplets_master_list ) if ntriplets_master == comb ( nlines_master , 3 , exact = True ) : if abs ( debugplot ) >= 10 : print ( '>>> Total number of lines in master table:' , nlines_master ) print ( '>>> Number of triplets in master table...:' , ntriplets_master ) else : raise ValueError ( 'Invalid number of combinations' ) ratios_master = np . zeros ( ntriplets_master ) for index , value in enumerate ( triplets_master_list ) : i1 , i2 , i3 = value delta1 = wv_master [ i2 ] - wv_master [ i1 ] delta2 = wv_master [ i3 ] - wv_master [ i1 ] ratios_master [ index ] = delta1 / delta2 isort_ratios_master = np . argsort ( ratios_master ) ratios_master_sorted = ratios_master [ isort_ratios_master ] triplets_master_sorted_list = [ triplets_master_list [ i ] for i in isort_ratios_master ] if abs ( debugplot ) in [ 21 , 22 ] : bins_in = np . linspace ( 0.0 , 1.0 , 41 ) hist , bins_out = np . histogram ( ratios_master , bins = bins_in ) from numina . array . display . matplotlib_qt import plt fig = plt . figure ( ) ax = fig . add_subplot ( 111 ) width_hist = 0.8 * ( bins_out [ 1 ] - bins_out [ 0 ] ) center = ( bins_out [ : - 1 ] + bins_out [ 1 : ] ) / 2 ax . bar ( center , hist , align = 'center' , width = width_hist ) ax . set_xlabel ( 'distance ratio in each triplet' ) ax . set_ylabel ( 'Number of triplets' ) ax . set_title ( "Number of lines/triplets: " + str ( nlines_master ) + "/" + str ( ntriplets_master ) ) set_window_geometry ( geometry ) pause_debugplot ( debugplot , pltshow = True , tight_layout = True ) return ntriplets_master , ratios_master_sorted , triplets_master_sorted_list
Compute information associated to triplets in master table.

    Determine all the possible triplets that can be generated from the
    array `wv_master`. In addition, the relative position of the
    central line of each triplet is also computed.

    Parameters
    ----------
    wv_master : 1d numpy array, float
        Array with wavelengths corresponding to the master table
        (Angstroms).
    geometry : tuple (4 integers) or None
        x, y, dx, dy values employed to set the window geometry.
    debugplot : int
        Determines whether intermediate computations and/or plots
        are displayed. The valid codes are defined in
        numina.array.display.pause_debugplot.

    Returns
    -------
    ntriplets_master : int
        Number of triplets built from master table.
    ratios_master_sorted : 1d numpy array, float
        Array with values of the relative position of the central line
        of each triplet, sorted in ascending order.
    triplets_master_sorted_list : list of tuples
        List with tuples of three numbers, corresponding to the three
        line indices in the master table. The list is sorted to be in
        correspondence with `ratios_master_sorted`.