From 2558c97f69f12e2a20253d36ee3e4d0f40a4d5a3 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Fri, 13 Oct 2023 17:32:06 +0800 Subject: [PATCH 1/4] Figure.plot: Refactor to increase code readability --- pygmt/src/plot.py | 81 ++++++++++++++++++++++++----------------------- 1 file changed, 41 insertions(+), 40 deletions(-) diff --git a/pygmt/src/plot.py b/pygmt/src/plot.py index 069ef5c7077..1e5ccb3832d 100644 --- a/pygmt/src/plot.py +++ b/pygmt/src/plot.py @@ -210,52 +210,53 @@ def plot(self, data=None, x=None, y=None, size=None, direction=None, **kwargs): ``x``/``y``. {wrap} """ - # pylint: disable=too-many-locals + # pylint: disable=too-many-locals,too-many-branches kwargs = self._preprocess(**kwargs) # pylint: disable=protected-access kind = data_kind(data, x, y) - extra_arrays = [] - if kwargs.get("S") is not None and kwargs["S"][0] in "vV" and direction is not None: - extra_arrays.extend(direction) - elif ( - kwargs.get("S") is None - and kind == "geojson" - and data.geom_type.isin(["Point", "MultiPoint"]).all() - ): # checking if the geometry of a geoDataFrame is Point or MultiPoint - kwargs["S"] = "s0.2c" - elif kwargs.get("S") is None and kind == "file" and str(data).endswith(".gmt"): - # checking that the data is a file path to set default style - try: - with open(which(data), mode="r", encoding="utf8") as file: - line = file.readline() - if "@GMULTIPOINT" in line or "@GPOINT" in line: - # if the file is gmt style and geometry is set to Point + + if kind != "vectors": + # Parameters can't be 1-D arrays if "data" is used + for arg, name in [ + (direction, "direction"), + (kwargs.get("G"), "fill"), + (size, "size"), + (kwargs.get("I"), "intensity"), + (kwargs.get("t"), "transparency"), + ]: + if is_nonstr_iter(arg): + raise GMTInvalidInput(f"'{name}' can't be 1-D array if 'data' is used.") + + # Set the default style if data has a geometry of Point or MultiPoint + if kwargs.get("S") is None: + if kind == "geojson" and data.geom_type.isin(["Point", "MultiPoint"]).all(): kwargs["S"] = "s0.2c" - except FileNotFoundError: - pass - if kwargs.get("G") is not None and is_nonstr_iter(kwargs["G"]): - if kind != "vectors": - raise GMTInvalidInput( - "Can't use arrays for fill if data is matrix or file." - ) - extra_arrays.append(kwargs["G"]) - del kwargs["G"] - if size is not None: - if kind != "vectors": - raise GMTInvalidInput( - "Can't use arrays for 'size' if data is a matrix or file." - ) - extra_arrays.append(size) + elif kind == "file" and str(data).endswith(".gmt"): # OGR_GMT file + try: + with open(which(data), mode="r", encoding="utf8") as file: + line = file.readline() + if "@GMULTIPOINT" in line or "@GPOINT" in line: + kwargs["S"] = "s0.2c" + except FileNotFoundError: + pass + else: + if ( + kwargs.get("S") is not None + and kwargs["S"][0] in "vV" + and direction is not None + ): + extra_arrays.extend(direction) - for flag in ["I", "t"]: - if kwargs.get(flag) is not None and is_nonstr_iter(kwargs[flag]): - if kind != "vectors": - raise GMTInvalidInput( - f"Can't use arrays for {plot.aliases[flag]} if data is matrix or file." - ) - extra_arrays.append(kwargs[flag]) - kwargs[flag] = "" + if is_nonstr_iter(kwargs.get("G")): + extra_arrays.append(kwargs.get("G")) + del kwargs["G"] + if is_nonstr_iter(size): + extra_arrays.append(size) + for flag in ["I", "t"]: + if is_nonstr_iter(kwargs.get(flag)): + extra_arrays.append(kwargs.get(flag)) + kwargs[flag] = "" with Session() as lib: file_context = lib.virtualfile_from_data( From 3592cdd840b1b8307d76b1cd6c9abbc54f0e59be Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Sat, 14 Oct 2023 21:11:33 +0800 Subject: [PATCH 2/4] Refactor --- pygmt/src/plot.py | 58 +++++++++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/pygmt/src/plot.py b/pygmt/src/plot.py index 1e5ccb3832d..af324660481 100644 --- a/pygmt/src/plot.py +++ b/pygmt/src/plot.py @@ -216,8 +216,8 @@ def plot(self, data=None, x=None, y=None, size=None, direction=None, **kwargs): kind = data_kind(data, x, y) extra_arrays = [] + # Some Parameters can't be 1-D arrays if the data kind is not vectors if kind != "vectors": - # Parameters can't be 1-D arrays if "data" is used for arg, name in [ (direction, "direction"), (kwargs.get("G"), "fill"), @@ -228,35 +228,35 @@ def plot(self, data=None, x=None, y=None, size=None, direction=None, **kwargs): if is_nonstr_iter(arg): raise GMTInvalidInput(f"'{name}' can't be 1-D array if 'data' is used.") - # Set the default style if data has a geometry of Point or MultiPoint - if kwargs.get("S") is None: - if kind == "geojson" and data.geom_type.isin(["Point", "MultiPoint"]).all(): - kwargs["S"] = "s0.2c" - elif kind == "file" and str(data).endswith(".gmt"): # OGR_GMT file - try: - with open(which(data), mode="r", encoding="utf8") as file: - line = file.readline() - if "@GMULTIPOINT" in line or "@GPOINT" in line: - kwargs["S"] = "s0.2c" - except FileNotFoundError: - pass - else: - if ( - kwargs.get("S") is not None - and kwargs["S"][0] in "vV" - and direction is not None - ): - extra_arrays.extend(direction) + # Set the default style if data has a geometry of Point or MultiPoint + if kwargs.get("S") is None: + if kind == "geojson" and data.geom_type.isin(["Point", "MultiPoint"]).all(): + kwargs["S"] = "s0.2c" + elif kind == "file" and str(data).endswith(".gmt"): # OGR_GMT file + try: + with open(which(data), mode="r", encoding="utf8") as file: + line = file.readline() + if "@GMULTIPOINT" in line or "@GPOINT" in line: + kwargs["S"] = "s0.2c" + except FileNotFoundError: + pass - if is_nonstr_iter(kwargs.get("G")): - extra_arrays.append(kwargs.get("G")) - del kwargs["G"] - if is_nonstr_iter(size): - extra_arrays.append(size) - for flag in ["I", "t"]: - if is_nonstr_iter(kwargs.get(flag)): - extra_arrays.append(kwargs.get(flag)) - kwargs[flag] = "" + # prepare 1-D arrays for input + if ( + kwargs.get("S") is not None + and kwargs["S"][0] in "vV" + and is_nonstr_iter(direction) + ): + extra_arrays.extend(direction) + if is_nonstr_iter(kwargs.get("G")): + extra_arrays.append(kwargs.get("G")) + del kwargs["G"] + if is_nonstr_iter(size): + extra_arrays.append(size) + for flag in ["I", "t"]: + if is_nonstr_iter(kwargs.get(flag)): + extra_arrays.append(kwargs.get(flag)) + kwargs[flag] = "" with Session() as lib: file_context = lib.virtualfile_from_data( From 8914d21c6cd14ced365e4be9eb756c66cfdbd0b9 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 18 Oct 2023 13:49:15 +0800 Subject: [PATCH 3/4] Fix a typo --- pygmt/src/plot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/src/plot.py b/pygmt/src/plot.py index 887ff5865ff..e9cc9befd02 100644 --- a/pygmt/src/plot.py +++ b/pygmt/src/plot.py @@ -216,7 +216,7 @@ def plot(self, data=None, x=None, y=None, size=None, direction=None, **kwargs): kind = data_kind(data, x, y) extra_arrays = [] - # Some Parameters can't be 1-D arrays if the data kind is not vectors + # Some parameters can't be 1-D arrays if the data kind is not vectors if kind != "vectors": for arg, name in [ (direction, "direction"), From abc59d0899ff4d3622661100769ad45c7326f9a6 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Mon, 4 Mar 2024 20:58:44 +0800 Subject: [PATCH 4/4] Updates --- pygmt/src/plot.py | 55 ++++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/pygmt/src/plot.py b/pygmt/src/plot.py index 50b52d573cd..29400f780dd 100644 --- a/pygmt/src/plot.py +++ b/pygmt/src/plot.py @@ -208,17 +208,35 @@ def plot( # noqa: PLR0912 kind = data_kind(data, x, y) extra_arrays = [] - - # Some parameters can't be 1-D arrays if the data kind is not vectors - if kind != "vectors": - for arg, name in [ - (direction, "direction"), - (kwargs.get("G"), "fill"), - (size, "size"), - (kwargs.get("I"), "intensity"), - (kwargs.get("t"), "transparency"), + if kind == "vectors": # Add more columns for vectors input + # Parameters for vector styles + if ( + kwargs.get("S") is not None + and kwargs["S"][0] in "vV" + and is_nonstr_iter(direction) + ): + extra_arrays.extend(direction) + # Fill + if is_nonstr_iter(kwargs.get("G")): + extra_arrays.append(kwargs.get("G")) + del kwargs["G"] + # Size + if is_nonstr_iter(size): + extra_arrays.append(size) + # Intensity and transparency + for flag in ["I", "t"]: + if is_nonstr_iter(kwargs.get(flag)): + extra_arrays.append(kwargs.get(flag)) + kwargs[flag] = "" + else: + for name, value in [ + ("direction", direction), + ("fill", kwargs.get("G")), + ("size", size), + ("intensity", kwargs.get("I")), + ("transparency", kwargs.get("t")), ]: - if is_nonstr_iter(arg): + if is_nonstr_iter(value): raise GMTInvalidInput(f"'{name}' can't be 1-D array if 'data' is used.") # Set the default style if data has a geometry of Point or MultiPoint @@ -234,23 +252,6 @@ def plot( # noqa: PLR0912 except FileNotFoundError: pass - # prepare 1-D arrays for input - if ( - kwargs.get("S") is not None - and kwargs["S"][0] in "vV" - and is_nonstr_iter(direction) - ): - extra_arrays.extend(direction) - if is_nonstr_iter(kwargs.get("G")): - extra_arrays.append(kwargs.get("G")) - del kwargs["G"] - if is_nonstr_iter(size): - extra_arrays.append(size) - for flag in ["I", "t"]: - if is_nonstr_iter(kwargs.get(flag)): - extra_arrays.append(kwargs.get(flag)) - kwargs[flag] = "" - with Session() as lib: with lib.virtualfile_in( check_kind="vector", data=data, x=x, y=y, extra_arrays=extra_arrays