Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debug workflow 2 #268

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions gplately/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,17 @@

"""

__version__ = "1.3.0"

def get_distribution_version():
from importlib.metadata import PackageNotFoundError, version

try:
return version(__name__)
except PackageNotFoundError:
return "UNKNOWN VERSION"


__version__ = get_distribution_version()
REQUIRED_PMM_VERSION = "1.2.0"
USING_DEV_VERSION = True ## change this to False before official release

Expand All @@ -214,16 +224,9 @@
print()
print("##########################################################################")
print(
"""
f"""
WARNING:
You are using a DEV version GPlately. Some functionalities have not been tested thoroughly.
The DEV version may break your code or produce wrong results due to its unstable nature(DEV in progress).
You might also need to install the DEV version plate_model_manager
from https://github.com/michaelchin/plate-model-manager to use this DEV version GPlately.

To disable this warning, set USING_DEV_VERSION to False in __init__.py or
set DISABLE_GPLATELY_DEV_WARNING environment variable to true.
`export DISABLE_GPLATELY_DEV_WARNING=true`
git
"""
)
print("##########################################################################")
Expand Down
78 changes: 71 additions & 7 deletions gplately/commands/feature_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@

import abc
import argparse
import logging
import re
from typing import List

import pygplates

logger = logging.getLogger("gplately")


class FeatureFilter(metaclass=abc.ABCMeta):
@classmethod
Expand Down Expand Up @@ -135,6 +139,34 @@ def should_keep(self, feature: pygplates.Feature) -> bool:
return False


class FeatureTypeFilter(FeatureFilter):
"""filter features by the feature type(regular expression)

examples:
- gplately filter "$IN_FILE" output/basins.gpmlz -t gpml:Basin
- gplately filter "$IN_FILE" output/basin_islandarc.gpmlz -t "gpml:IslandArc|gpml:Basin"

:param feature_type_re: the regular expression to match the feature type

"""

def __init__(self, feature_type_re: str):
self._feature_type_re = feature_type_re

def should_keep(self, feature: pygplates.Feature) -> bool:
feature_type = feature.get_feature_type()
if re.fullmatch(self._feature_type_re, feature_type.to_qualified_string()):
logger.debug(
f"feature type match: {self._feature_type_re} {feature_type.to_qualified_string()}"
)
return True
else:
logger.debug(
f"feature type not match: {self._feature_type_re} {feature_type.to_qualified_string()}"
)
return False


def filter_feature_collection(
feature_collection: pygplates.FeatureCollection, filters: List[FeatureFilter]
):
Expand Down Expand Up @@ -168,27 +200,56 @@ def add_parser(subparser):
filter_cmd.add_argument("filter_input_file", type=str)
filter_cmd.add_argument("filter_output_file", type=str)

# filter by feature name
name_group = filter_cmd.add_mutually_exclusive_group()
name_group.add_argument("-n", "--names", type=str, dest="names", nargs="+")
name_group.add_argument(
"--exclude-names", type=str, dest="exclude_names", nargs="+"
"-n", "--names", type=str, dest="names", nargs="+", metavar="feature_name"
)
name_group.add_argument(
"--exclude-names",
type=str,
dest="exclude_names",
nargs="+",
metavar="feature_name",
)

# filter by plate ID
pid_group = filter_cmd.add_mutually_exclusive_group()
pid_group.add_argument("-p", "--pids", type=int, dest="pids", nargs="+")
pid_group.add_argument("--exclude-pids", type=int, dest="exclude_pids", nargs="+")
pid_group.add_argument(
"-p", "--pids", type=int, dest="pids", nargs="+", metavar="pid"
)
pid_group.add_argument(
"--exclude-pids", type=int, dest="exclude_pids", nargs="+", metavar="pid"
)

# filter by birth age
birth_age_group = filter_cmd.add_mutually_exclusive_group()
birth_age_group.add_argument(
"-a", "--min-birth-age", type=float, dest="min_birth_age"
"-a",
"--min-birth-age",
type=float,
dest="min_birth_age",
metavar="min_birth_age",
)
birth_age_group.add_argument(
"--max-birth-age", type=float, dest="max_birth_age", metavar="max_birth_age"
)
birth_age_group.add_argument("--max-birth-age", type=float, dest="max_birth_age")

filter_cmd.add_argument(
"--case-sensitive", dest="case_sensitive", action="store_true"
)
filter_cmd.add_argument("--exact-match", dest="exact_match", action="store_true")

# filter by feature type
filter_cmd.add_argument(
"-t",
"--feature-type",
type=str,
dest="feature_type_re",
metavar="feature_type",
help="the regular expression of feature type",
)


def run_filter_feature_collection(args):
"""Filter the input feature collection according to command line arguments."""
Expand Down Expand Up @@ -224,12 +285,15 @@ def run_filter_feature_collection(args):
elif args.min_birth_age is not None:
filters.append(BirthAgeFilter(args.min_birth_age))

if args.feature_type_re is not None:
filters.append(FeatureTypeFilter(args.feature_type_re))

new_fc = filter_feature_collection(
input_feature_collection,
filters,
)

new_fc.write(args.filter_output_file)
print(
logger.info(
f"Done! The filtered feature collection has been saved to {args.filter_output_file}."
)
15 changes: 15 additions & 0 deletions gplately/commands/reset_feature_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import pygplates

new_fc = pygplates.FeatureCollection()
for f in pygplates.FeatureCollection("shapes_continents.gpml"):
# print(f.get_feature_type())
new_f = pygplates.Feature(
pygplates.FeatureType.gpml_unclassified_feature,
f.get_feature_id(),
pygplates.VerifyInformationModel.yes,
)
for p in f:
new_f.add(p.get_name(), p.get_value(), pygplates.VerifyInformationModel.yes)
new_fc.add(new_f)

new_fc.write("new_shapes_continents.gpmlz")
9 changes: 6 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
[build-system]
requires = ["setuptools>=61.0.0", "wheel"]
requires = ["setuptools>=61.0.0", "wheel", "setuptools-git-versioning"]
build-backend = "setuptools.build_meta"

[tool.setuptools-git-versioning]
enabled = true

[project]
name = "gplately"
dynamic = ["version"]
Expand Down Expand Up @@ -42,8 +45,8 @@ where = ["."]
exclude = ['*.examples*','*.notebooks*','tests-dir*','Notebooks*', 'scripts*']
namespaces = true

[tool.setuptools.dynamic]
version = {attr = "gplately.__version__"}
#[tool.setuptools.dynamic]
#version = {attr = "gplately.__version__"}

[project.optional-dependencies]
dev = ["black", "bumpver", "isort", "pip-tools", "pytest"]
Expand Down
8 changes: 8 additions & 0 deletions scripts/test_feature_filter.sh
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,11 @@ gplately filter "$IN_FILE" output/no_africa_north_america.gpmlz --exclude-names
# get features whose name does not conain "Africa" or "North America" and plate ID is not in 401 702

gplately filter "$IN_FILE" output/no_africa_north_america_no_401_702.gpmlz --exclude-names Africa "North America" --exclude-pids 401 702

# get all gpml:Basin features

gplately filter "$IN_FILE" output/basins.gpmlz -t gpml:Basin

# get all gpml:Basin + gpml:IslandArc features

gplately filter "$IN_FILE" output/basin_islandarc.gpmlz -t "gpml:IslandArc|gpml:Basin"
5 changes: 5 additions & 0 deletions tests-dir/unittest/test_plate_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
from common import MODEL_REPO_DIR
from plate_model_manager import PlateModelManager

import gplately

print(gplately.__version__)
print(gplately.__file__)


def main():
pm_manger = PlateModelManager()
Expand Down
Loading