|
|
@ -3,10 +3,10 @@ |
|
|
|
Parsing the numerical output from Sensovations Sensospot image analysis. |
|
|
|
Parsing the numerical output from Sensovations Sensospot image analysis. |
|
|
|
""" |
|
|
|
""" |
|
|
|
|
|
|
|
|
|
|
|
__version__ = "0.9.1" |
|
|
|
__version__ = "1.0.0" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from pathlib import Path |
|
|
|
import pathlib |
|
|
|
|
|
|
|
|
|
|
|
import click |
|
|
|
import click |
|
|
|
import pandas |
|
|
|
import pandas |
|
|
@ -19,23 +19,23 @@ DEFAULT_OUTPUT_FILENAME = "collected_data.csv" |
|
|
|
|
|
|
|
|
|
|
|
@click.command() |
|
|
|
@click.command() |
|
|
|
@click.argument( |
|
|
|
@click.argument( |
|
|
|
"source", |
|
|
|
"sources", |
|
|
|
type=click.Path( |
|
|
|
type=click.Path( |
|
|
|
exists=True, |
|
|
|
exists=True, |
|
|
|
file_okay=False, |
|
|
|
file_okay=False, |
|
|
|
dir_okay=True, |
|
|
|
dir_okay=True, |
|
|
|
readable=True, |
|
|
|
readable=True, |
|
|
|
writable=True, |
|
|
|
|
|
|
|
), |
|
|
|
), |
|
|
|
|
|
|
|
required=True, |
|
|
|
|
|
|
|
nargs=-1, |
|
|
|
) |
|
|
|
) |
|
|
|
@click.option( |
|
|
|
@click.option( |
|
|
|
"-o", |
|
|
|
"-o", |
|
|
|
"--outfile", |
|
|
|
"--output", |
|
|
|
default=DEFAULT_OUTPUT_FILENAME, |
|
|
|
is_flag=False, |
|
|
|
help=( |
|
|
|
flag_value=DEFAULT_OUTPUT_FILENAME, |
|
|
|
"Output file name, use a dash '-' for stdout, " |
|
|
|
type=click.Path(exists=False, dir_okay=False), |
|
|
|
f"default: '{DEFAULT_OUTPUT_FILENAME}'" |
|
|
|
help=f"Output file path, defaults to '{DEFAULT_OUTPUT_FILENAME}'", |
|
|
|
), |
|
|
|
|
|
|
|
) |
|
|
|
) |
|
|
|
@click.option( |
|
|
|
@click.option( |
|
|
|
"-q", |
|
|
|
"-q", |
|
|
@ -44,54 +44,18 @@ DEFAULT_OUTPUT_FILENAME = "collected_data.csv" |
|
|
|
default=False, |
|
|
|
default=False, |
|
|
|
help="Ignore Sanity Check", |
|
|
|
help="Ignore Sanity Check", |
|
|
|
) |
|
|
|
) |
|
|
|
@click.option( |
|
|
|
def main(sources, output, quiet=False): |
|
|
|
"-r", |
|
|
|
"""Parses the measurement results of the Sensospot reader |
|
|
|
"--recurse", |
|
|
|
|
|
|
|
is_flag=True, |
|
|
|
|
|
|
|
default=False, |
|
|
|
|
|
|
|
help="Recurse into folders one level down", |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
def main(source, outfile, quiet=False, recurse=False): |
|
|
|
|
|
|
|
if recurse: |
|
|
|
|
|
|
|
_parse_recursive(source, outfile, quiet) |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
_parse_one_folder(source, outfile, quiet) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The resulting output is either echoed to stdout or saved to a file. |
|
|
|
|
|
|
|
|
|
|
|
def _output(data, folder, outfile): |
|
|
|
|
|
|
|
"""output a datafarme to stdout or csv file |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
data: the pandas dataframe |
|
|
|
|
|
|
|
folder: the folder to save the file to |
|
|
|
|
|
|
|
outfile: the name of the outfile, '-' will output to stdout |
|
|
|
|
|
|
|
""" |
|
|
|
""" |
|
|
|
if outfile.strip() == "-": |
|
|
|
paths = (pathlib.Path(source) for source in sources) |
|
|
|
click.echo(data.to_csv(None, sep="\t", index=False)) |
|
|
|
collection = (parse_folder(source) for source in paths) |
|
|
|
else: |
|
|
|
result = pandas.concat(collection, ignore_index=True).to_csv( |
|
|
|
csv_file = Path(folder) / outfile |
|
|
|
output, sep="\t", index=False |
|
|
|
data.to_csv(csv_file, sep="\t", index=False) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _parse_one_folder(source, outfile, quiet): |
|
|
|
|
|
|
|
"""parses the data of one folder""" |
|
|
|
|
|
|
|
source_path = Path(source) |
|
|
|
|
|
|
|
# read the raw data of a folder |
|
|
|
|
|
|
|
raw_data = parse_folder(source_path, quiet=quiet) |
|
|
|
|
|
|
|
_output(raw_data, source_path, outfile) |
|
|
|
|
|
|
|
return raw_data |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _parse_recursive(source, outfile, quiet): |
|
|
|
|
|
|
|
"""parses all folders one level down and collects the data""" |
|
|
|
|
|
|
|
child_outfile = DEFAULT_OUTPUT_FILENAME |
|
|
|
|
|
|
|
source_path = Path(source) |
|
|
|
|
|
|
|
folders = (i for i in source_path.iterdir() if i.is_dir()) |
|
|
|
|
|
|
|
non_hidden = (i for i in folders if not i.name.startswith(".")) |
|
|
|
|
|
|
|
collection = ( |
|
|
|
|
|
|
|
_parse_one_folder(f, child_outfile, quiet) for f in non_hidden |
|
|
|
|
|
|
|
) |
|
|
|
) |
|
|
|
try: |
|
|
|
# if 'output' is None, the call to 'to_csv()' returns the csv as text |
|
|
|
collected = pandas.concat(collection, ignore_index=True) |
|
|
|
# if 'output' is not None, 'to_csv()' writes to the file and returns None |
|
|
|
_output(collected.reset_index(), source_path, outfile) |
|
|
|
if result: |
|
|
|
except ValueError as e: |
|
|
|
click.echo(result) |
|
|
|
print(str(e)) |
|
|
|
|
|
|
|