From dee700be58e88f24ba3f8283ecf1b86baf34a93f Mon Sep 17 00:00:00 2001 From: Haibao Tang Date: Mon, 13 May 2024 23:21:32 -0700 Subject: [PATCH 1/5] Add setup_magick_home --- jcvi/apps/base.py | 26 ++++++++++++++++++++++---- jcvi/graphics/grabseeds.py | 4 ++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/jcvi/apps/base.py b/jcvi/apps/base.py index b69946d7..f431af8f 100644 --- a/jcvi/apps/base.py +++ b/jcvi/apps/base.py @@ -7,6 +7,7 @@ import logging import os import os.path as op +import platform import shutil import signal import sys @@ -1206,13 +1207,30 @@ def Popen(cmd, stdin=None, stdout=PIPE, debug=False, shell="/bin/bash"): return proc -def is_macOS(): +def get_system_processor() -> Tuple[str, str]: """ - Check if current OS is macOS, this impacts mostly plotting code. + Get the system and processor information. """ - import platform + return platform.system(), platform.processor() - return platform.system() == "Darwin" + +def is_macOS_arm() -> bool: + """ + Check if the system is macOS on ARM. + """ + system, processor = get_system_processor() + return system == "Darwin" and "arm" in processor + + +def setup_magick_home(): + """ + Set MAGICK_HOME for ImageMagick. + """ + if "MAGICK_HOME" not in os.environ: + if is_macOS_arm(): + magick_home = "/opt/homebrew/opt/imagemagick" + os.environ["MAGICK_HOME"] = magick_home + logger.info("Set MAGICK_HOME to `%s`", magick_home) def popen(cmd, debug=True, shell="/bin/bash"): diff --git a/jcvi/graphics/grabseeds.py b/jcvi/graphics/grabseeds.py index eb020f35..df80cd32 100644 --- a/jcvi/graphics/grabseeds.py +++ b/jcvi/graphics/grabseeds.py @@ -15,6 +15,10 @@ import numpy as np +from ..apps.base import setup_magick_home + +setup_magick_home() + from PIL.Image import open as iopen from pyefd import elliptic_fourier_descriptors from pytesseract import image_to_string From 255fbc1fe169e3be5e17a862e382c20eb484d47c Mon Sep 17 00:00:00 2001 From: Haibao Tang Date: Mon, 13 May 2024 23:26:11 -0700 Subject: [PATCH 2/5] check path --- jcvi/apps/base.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/jcvi/apps/base.py b/jcvi/apps/base.py index f431af8f..a519f764 100644 --- a/jcvi/apps/base.py +++ b/jcvi/apps/base.py @@ -1229,8 +1229,10 @@ def setup_magick_home(): if "MAGICK_HOME" not in os.environ: if is_macOS_arm(): magick_home = "/opt/homebrew/opt/imagemagick" - os.environ["MAGICK_HOME"] = magick_home - logger.info("Set MAGICK_HOME to `%s`", magick_home) + if op.isdir(magick_home): + os.environ["MAGICK_HOME"] = magick_home + else: + logger.error("MAGICK_HOME not set") def popen(cmd, debug=True, shell="/bin/bash"): From 2160b24d884991d4cfd01bacbd22939a3430be4b Mon Sep 17 00:00:00 2001 From: Haibao Tang Date: Mon, 13 May 2024 23:32:20 -0700 Subject: [PATCH 3/5] Add sampling date --- jcvi/graphics/grabseeds.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/jcvi/graphics/grabseeds.py b/jcvi/graphics/grabseeds.py index df80cd32..80c4211d 100644 --- a/jcvi/graphics/grabseeds.py +++ b/jcvi/graphics/grabseeds.py @@ -10,6 +10,7 @@ import sys from collections import Counter +from datetime import date from math import cos, pi, sin from typing import Any, List, Optional, Tuple @@ -91,7 +92,7 @@ def __init__( self.circularity = 4 * pi * props.area / props.perimeter**2 self.rgb = rgb self.colorname = closest_color(rgb) - self.datetime = exif.get("exif:DateTimeOriginal", "none") + self.datetime = exif.get("exif:DateTimeOriginal", date.today()) self.rgbtag = triplet_to_rgb(rgb) self.pixeltag = f"length={self.length} width={self.width} area={self.area}" self.hashtag = " ".join((self.rgbtag, self.colorname)) From 4283d5195217c0d6768d4091e8fcdebe086142ca Mon Sep 17 00:00:00 2001 From: Haibao Tang Date: Mon, 13 May 2024 23:59:42 -0700 Subject: [PATCH 4/5] Add otsu --- jcvi/graphics/grabseeds.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/jcvi/graphics/grabseeds.py b/jcvi/graphics/grabseeds.py index 80c4211d..e70c1cde 100644 --- a/jcvi/graphics/grabseeds.py +++ b/jcvi/graphics/grabseeds.py @@ -27,7 +27,7 @@ from scipy.optimize import fmin_bfgs as fmin from skimage.color import gray2rgb, rgb2gray from skimage.feature import canny, peak_local_max -from skimage.filters import roberts, sobel +from skimage.filters import roberts, sobel, threshold_otsu from skimage.measure import find_contours, regionprops, label from skimage.morphology import disk, closing from skimage.segmentation import clear_border, watershed @@ -313,7 +313,7 @@ def add_seeds_options(p, args): ) g3 = p.add_argument_group("De-noise") - valid_filters = ("canny", "roberts", "sobel") + valid_filters = ("canny", "roberts", "sobel", "otsu") g3.add_argument( "--filter", default="canny", @@ -654,6 +654,9 @@ def seeds(args): edges = roberts(img_gray) elif ff == "sobel": edges = sobel(img_gray) + elif ff == "otsu": + thresh = threshold_otsu(img_gray) + edges = img_gray > thresh edges = clear_border(edges, buffer_size=opts.border) selem = disk(kernel) closed = closing(edges, selem) if kernel else edges @@ -722,6 +725,9 @@ def seeds(args): contour = find_contours(labels == props.label, 0.5)[0] efds = efd_feature(contour) y0, x0 = props.centroid + # perimeter = props.perimeter + # area = props.area + # print(perimeter, area) orientation = props.orientation major, minor = props.major_axis_length, props.minor_axis_length major_dx = sin(orientation) * major / 2 From 215072902e0306c7ef727621b4ba1dfb0cf1751b Mon Sep 17 00:00:00 2001 From: Haibao Tang Date: Tue, 14 May 2024 00:05:12 -0700 Subject: [PATCH 5/5] remove comments --- jcvi/graphics/grabseeds.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/jcvi/graphics/grabseeds.py b/jcvi/graphics/grabseeds.py index e70c1cde..489f5063 100644 --- a/jcvi/graphics/grabseeds.py +++ b/jcvi/graphics/grabseeds.py @@ -725,9 +725,6 @@ def seeds(args): contour = find_contours(labels == props.label, 0.5)[0] efds = efd_feature(contour) y0, x0 = props.centroid - # perimeter = props.perimeter - # area = props.area - # print(perimeter, area) orientation = props.orientation major, minor = props.major_axis_length, props.minor_axis_length major_dx = sin(orientation) * major / 2