def download_test_model(output_dir, overwrite=False) -> None: os.makedirs(os.path.abspath(output_dir), exist_ok=True) urls = [ ("demo_ilastik_model.zip", "demo.ilp", DEMO_ILASTIK_MODEL), ] for zipfln, fln, url in urls: zipfln = pjoin(output_dir, zipfln) fln = pjoin(output_dir, fln) if os.path.exists(fln) and not overwrite: log.debug("File exists, skipping: '%s'", url) continue log.info("Downloading test model: '%s'", url) request.urlretrieve(url, zipfln) log.info("Unzipping test model: '%s'", url) run_shell_command(f"unzip -d {output_dir} {zipfln}")
def download_test_data(output_dir, overwrite=False) -> None: os.makedirs(os.path.abspath(output_dir), exist_ok=True) urls = [ ("imcpipeline-example_pannel.csv", DEMO_CSV), ("imcpipeline-demo_data.zip", DEMO_DATA), ] for fln, url in urls: fln = pjoin(output_dir, fln) if os.path.exists(fln) and not overwrite: log.debug("File exists, skipping: '%s'", url) continue log.info("Downloading test file: '%s'", url) if not os.path.exists(fln): request.urlretrieve(url, fln)
def get_docker_image_or_build() -> None: """Pull docker image to use or build it if not existing.""" def check_image() -> bool: try: # check if exists out = (subprocess.check_output( "docker images".split(" ")).decode().strip()) for line in out.split("\n")[1:]: if line.split(" ")[0] == DOCKER_IMAGE: return True except FileNotFoundError: log.error("Docker installation not detected.") raise except IndexError: pass return False if not check_image(): log.debug("Did not find cellprofiler docker image. Will build.") build_docker_image() else: log.debug("Found docker image.") cfg.args.container_image = DOCKER_IMAGE
def run_shell_command(cmd) -> int: """ Run a system command. Will detect whether a separate shell is required. """ # in case the command has unix pipes or bash builtins, # the subprocess call must have its own shell # this should only occur if cellprofiler is being run uncontainerized # and needs a command to be called prior such as conda activate, etc symbol = any([x in cmd for x in ["&", "&&", "|"]]) source = cmd.startswith("source") shell = bool(symbol or source) log.debug( "Running command%s:\n%s", " in shell" if shell else "", textwrap.dedent(cmd) + "\n", ) c = re.findall(r"\S+", cmd.replace("\\\n", "")) if not cfg.args.dry_run: if shell: log.debug("Running command in shell.") code = subprocess.call(cmd, shell=shell) else: code = subprocess.call(c, shell=shell) if code != 0: log.error( "Process for command below failed with error:\n'%s'\nTerminating pipeline.\n", textwrap.dedent(cmd), ) sys.exit(code) if not shell: usage = resource.getrusage(resource.RUSAGE_SELF) log.debug("Maximum used memory so far: {:.2f}Gb".format( usage.ru_maxrss / 1e6)) return code