def get_text_lines( file, return_lines=None, rstrip=True, sort=False, remove_empty_lines=True, encoding=None, ): """ Return only the nth line of a text file :param file: Any text file :param return_lines: Which specific line/lines to read :param rstrip: Remove trailing characters :param sort: If true, naturally sort the data :param remove_empty_lines: If True, ignore empty lines :param encoding: What encoding the text file has. Default: None (platform dependent) :return: The nth line """ with open(file, encoding=encoding) as f: lines = f.readlines() if rstrip: lines = [line.strip() for line in lines] if remove_empty_lines: lines = list.remove_empty_string(lines) if sort: lines = natsorted(lines) if return_lines is not None: lines = lines[return_lines] return lines
def summary_run(args): # if args.structures_file_path is None: # args.structures_file_path = get_structures_path() # reference_structures_table = pd.read_csv(args.structures_file_path) # else: # raise NotImplementedError( # "Only the Allen adult mouse atlas is " "currently supported." # ) reference_structures_table = pd.read_csv(get_structures_path()) if not args.regions and not args.regions_list: # regions = list(pd.read_csv(args.structures_file_path)["name"]) regions = list(pd.read_csv(get_structures_path())["name"]) else: regions = [] if args.regions: regions = regions + args.regions if args.regions_list: regions = regions + list(pd.read_csv(args.regions_list)["name"]) regions = remove_empty_string(regions) regions = unique_elements_lists(regions) csvs_folder = args.csv_dir destination_folder = os.path.join(args.csv_dir, "summary") csvs_names = [f for f in os.listdir(csvs_folder) if f.endswith(".csv")] if len(csvs_names) == 0: raise FileNotFoundError( "No CSV files were found in the directory: " "{}. Please check the arguments".format(csvs_folder)) csvs_paths = [os.path.join(csvs_folder, f) for f in csvs_names] if not os.path.exists(destination_folder): os.makedirs(destination_folder) for csv_file_path in csvs_paths: print("CSV file: {}".format(os.path.basename(csv_file_path))) summary = make_summary_df_for_brain( csv_file_path, regions, reference_structures_table, sum_regions=args.sum_regions, ) filename = os.path.basename(csv_file_path) dest_path = os.path.join(destination_folder, filename) summary.to_csv(dest_path, index=False) print("Done!")
def test_remove_empty_string(): assert ( list_tools.remove_empty_string(list_with_empty) == list_without_empty)