Skip to content

Commit

Permalink
Merge pull request #87 from /issues/86
Browse files Browse the repository at this point in the history
Add option to skip image header
  • Loading branch information
mcquin authored Dec 15, 2017
2 parents b025aee + 36c36e5 commit eeb22f9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
15 changes: 13 additions & 2 deletions cytominer_database/commands/command_ingest.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,17 @@
the CSV will be split into one CSV per compartment.
"""
)
def command(source, target, config_file, munge):
@click.option(
"--skip-image-prefix/--no-skip-image-prefix",
default=True,
help="""\
True if the prefix of image table name should be
excluded from the names of columns from per image
table e.g. use `Metadata_Plate` instead of
`Image_Metadata_Plate`.
"""
)
def command(source, target, config_file, munge, skip_image_prefix):
config = configparser.ConfigParser()

with open(config_file, "r") as config_fd:
Expand All @@ -53,4 +63,5 @@ def command(source, target, config_file, munge):
if munge:
cytominer_database.munge.munge(config=config, source=source)

cytominer_database.ingest.seed(source, target, config)
cytominer_database.ingest.seed(source, target, config,
skip_image_prefix)
14 changes: 10 additions & 4 deletions cytominer_database/ingest.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,15 @@ def __format__(name, header):
return "{}_{}".format(name, header)


def into(input, output, name, identifier):
def into(input, output, name, identifier, skip_table_prefix=False):
"""Ingest a CSV file into a table in a database.
:param input: Input CSV file.
:param output: Connection string for the database.
:param name: Table in database into which the CSV file will be ingested
:param identifier: Unique identifier for ``input``.
:param skip_table_prefix: True if the prefix of the table name should be excluded
from the names of columns.
"""

with backports.tempfile.TemporaryDirectory() as directory:
Expand All @@ -80,7 +82,8 @@ def into(input, output, name, identifier):
writer = csv.writer(fout)

headers = next(reader)
headers = [__format__(name, header) for header in headers]
if not skip_table_prefix:
headers = [__format__(name, header) for header in headers]
headers = ["TableNumber"] + headers

writer.writerow(headers)
Expand All @@ -97,13 +100,15 @@ def into(input, output, name, identifier):
odo.odo(source, "{}::{}".format(output, name), has_header=True, delimiter=",")


def seed(source, target, config):
def seed(source, target, config, skip_image_prefix=True):
"""
Read CSV files into a database backend.
:param config: Configuration file.
:param source: Directory containing subdirectories that contain CSV files.
:param target: Connection string for the database.
:param skip_image_prefix: True if the prefix of image table name should be excluded
from the names of columns from per image table
"""

directories = sorted(list(cytominer_database.utils.find_directories(source)))
Expand All @@ -122,7 +127,8 @@ def seed(source, target, config):
name, _ = os.path.splitext(config["filenames"]["image"])

try:
into(input=image, output=target, name=name.capitalize(), identifier=identifier)
into(input=image, output=target, name=name.capitalize(), identifier=identifier,
skip_table_prefix = skip_image_prefix)
except sqlalchemy.exc.DatabaseError as e:
click.echo(e)

Expand Down

0 comments on commit eeb22f9

Please sign in to comment.