def combination_peaks(peak_batch, chem_vec, mode, temp_name, crystal_family, user_info, URL, prediction_per_level, subset, num_peaks):
    outpath = 'Ready'
    if (not os.path.exists(outpath)):
        os.makedirs(outpath)
    find_valid_peaks = list(powerset(peak_batch['vec']))
    find_valid_peaks = [item for item in find_valid_peaks if ((len(item) > num_peaks[0]) and (len(item) < num_peaks[1]))]
    print(len(find_valid_peaks), 'valid peak combinations')
    valid_peaks_combinations = [{'vec': proto_combo} for proto_combo in find_valid_peaks]
    found = False
    threshold = 0
    tot_spec = 1
    for p in prediction_per_level:
        tot_spec *= p
    guesses = {'num_pred': tot_spec}
    for k in range(1, (tot_spec + 1)):
        guesses[('species_' + str(k))] = []
        guesses[('spec_confidence_' + str(k))] = []
    common_peaks = []
    failed_combos = valid_peaks_combinations
    persistance = 0
    LIMIT = 3
    if ((subset > 0) and (subset < len(failed_combos))):
        failed_combos = random.sample(failed_combos, subset)
        print('using ', len(failed_combos), ' peak combinations')
    while ((len(failed_combos) > 0) and (persistance < LIMIT)):
        for combo in failed_combos:
            try:
                classificated = ClientSide2.Send_For_Classification(combo, chem_vec, mode, crystal_family, user_info, URL, prediction_per_level)
                print(classificated)
                classificated['file_name'] = temp_name
                print(os.path.join(outpath, temp_name))
                cf.write_to_csv((os.path.join(outpath, temp_name) + '.csv'), classificated, prediction_per_level)
                print(tot_spec)
                for k in range(1, (tot_spec + 1)):
                    print(guesses)
                    guesses[('species_' + str(k))].append(classificated[('species_' + str(k))])
                    guesses[('spec_confidence_' + str(k))].append(classificated[('spec_confidence_' + str(k))])
                    common_peaks.append(classificated['peaks'])
                failed_combos.remove(combo)
            except KeyboardInterrupt:
                raise
            except:
                print('An error occured this combination was not classified.\nIt will be retried {} more times'.format((LIMIT - persistance)))
        persistance += 1
    if (len(failed_combos) > 0):
        print('there were {} failed combinations'.format(len(failed_combos)))
    print('returning')
    return (common_peaks, guesses)
def combination_peaks(peak_batch, chem_vec, mode, temp_name, crystal_family, user_info, URL, prediction_per_level, subset, num_peaks):
    outpath = 'Ready'
    if (not os.path.exists(outpath)):
        os.makedirs(outpath)
    find_valid_peaks = list(powerset(peak_batch['vec']))
    find_valid_peaks = [item for item in find_valid_peaks if ((len(item) > num_peaks[0]) and (len(item) < num_peaks[1]))]
    print(len(find_valid_peaks), 'valid peak combinations')
    valid_peaks_combinations = [{'vec': proto_combo} for proto_combo in find_valid_peaks]
    found = False
    threshold = 0
    tot_spec = 1
    for p in prediction_per_level:
        tot_spec *= p
    guesses = {'num_pred': tot_spec}
    for k in range(1, (tot_spec + 1)):
        guesses[('species_' + str(k))] = []
        guesses[('spec_confidence_' + str(k))] = []
    common_peaks = []
    failed_combos = valid_peaks_combinations
    persistance = 0
    LIMIT = 3
    if ((subset > 0) and (subset < len(failed_combos))):
        failed_combos = random.sample(failed_combos, subset)
        print('using ', len(failed_combos), ' peak combinations')
    while ((len(failed_combos) > 0) and (persistance < LIMIT)):
        for combo in failed_combos:
            try:
                classificated = ClientSide2.Send_For_Classification(combo, chem_vec, mode, crystal_family, user_info, URL, prediction_per_level)
                print(classificated)
                classificated['file_name'] = temp_name
                print(os.path.join(outpath, temp_name))
                cf.write_to_csv((os.path.join(outpath, temp_name) + '.csv'), classificated, prediction_per_level)
                print(tot_spec)
                for k in range(1, (tot_spec + 1)):
                    print(guesses)
                    guesses[('species_' + str(k))].append(classificated[('species_' + str(k))])
                    guesses[('spec_confidence_' + str(k))].append(classificated[('spec_confidence_' + str(k))])
                    common_peaks.append(classificated['peaks'])
                failed_combos.remove(combo)
            except KeyboardInterrupt:
                raise
            except:
                print('An error occured this combination was not classified.\nIt will be retried {} more times'.format((LIMIT - persistance)))
        persistance += 1
    if (len(failed_combos) > 0):
        print('there were {} failed combinations'.format(len(failed_combos)))
    print('returning')
    return (common_peaks, guesses)