def test_natural_sort(self): names = ['Image0.msdf', 'Image0.h5', 'Image10.h5', 'Image2.h5'] util.natural_sort(names) expected_names = [ 'Image0.h5', 'Image0.msdf', 'Image2.h5', 'Image10.h5' ] self.assertEqual(names, expected_names)
def merge_masses(df): """Merges 'Target' cells of a DataFrame with the same 'Mass' value. This function merges multiple targets that are conjugated to the same mass tag such that the returned DataFrame contains only unique masses. Target names are combined using the conventions of :func:'util.natural_sort()'. Args: df: A DataFrame of the panel containing columns 'Mass' and 'Target'. Returns: A DataFrame containing columns 'Mass' and 'Target' with merged targets of the same mass. """ conjugates = {} target_list = [] for conj in df.to_dict(orient='records'): mass = conj['Mass'] target = conj['Target'] if conjugates.get(mass): conjugates[mass].append(target) else: conjugates[mass] = [target] for mass in conjugates: target_list = conjugates[mass] util.natural_sort(target_list) conjugates[mass] = ', '.join(target_list) return pd.DataFrame(list(conjugates.items()), columns=['Mass', 'Target'])