Skip to content

Commit

Permalink
Merge pull request #257 from nasa/feature/issue-256-fix-pre-commit-du…
Browse files Browse the repository at this point in the history
…ring-workflows

fix pre-commit during workflows
  • Loading branch information
danielfromearth authored Nov 14, 2024
2 parents 0603705 + fd0745f commit 805efd6
Show file tree
Hide file tree
Showing 20 changed files with 1,929 additions and 1,780 deletions.
12 changes: 3 additions & 9 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ ci:
autofix_prs: false # Comment "pre-commit.ci autofix" on a PR to trigger

default_language_version:
python: python3
python: python3.9

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
Expand All @@ -16,7 +16,6 @@ repos:
args: [ '--allow-multiple-documents' ]
- id: check-toml
- id: check-json
args: ['--autofix', '--no-ensure-ascii', '--no-sort-keys']
# Check for common mistakes
- id: check-added-large-files
- id: check-case-conflict
Expand All @@ -29,12 +28,12 @@ repos:
- id: destroyed-symlinks
- id: detect-private-key
- id: end-of-file-fixer
exclude: ".ipynb"
exclude_types: ["jupyter", "text"]
- id: mixed-line-ending
- id: no-commit-to-branch # protects `main` by default
- id: debug-statements
- id: trailing-whitespace
exclude: ".txt"
exclude_types: ["jupyter", "text"]

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.7.3
Expand All @@ -43,11 +42,6 @@ repos:
args: ["--fix", "--exit-non-zero-on-fix"]
- id: ruff-format

- repo: https://github.com/psf/black
rev: 24.10.0
hooks:
- id: black-jupyter

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.13.0
hooks:
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [unreleased]

### Removed

- Remove usage of black for code formatting ([#257](https://github.com/nasa/ncompare/pull/257)) ([**@danielfromearth**](https://github.com/danielfromearth))

## [1.10.0] - 2024-07-10

### Changed

- Group dependabot updates into fewer PRs ([#233](https://github.com/nasa/ncompare/issues/233)) ([**@danielfromearth**](https://github.com/danielfromearth))
Expand Down
3 changes: 1 addition & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,7 @@ features or old documentation updated for any changed features

### Python Style Guide

_Ncompare_ follows PEP8 as much as possible. Reference the _ruff_ and
_black_ configuration sections in [pyproject.toml](pyproject.toml) for specific expectations.
_Ncompare_ follows PEP8 as much as possible. Reference the _ruff_ configuration section in [pyproject.toml](pyproject.toml) for specific expectations.

### Documentation

Expand Down
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ _____
<a href="https://pypi.org/project/ncompare" target="_blank">
<img src="https://img.shields.io/pypi/v/ncompare?color=%2334D058label=pypi%20package" alt="Package version">
</a>
<a href="https://github.com/python/black" target="_blank">
<img src="https://img.shields.io/badge/code%20style-black-000000.svg" alt="Code style">
</a>
<a href="https://mypy-lang.org/" target="_blank">
<img src="https://www.mypy-lang.org/static/mypy_badge.svg" alt="Mypy checked">
</a>
Expand Down
1,060 changes: 530 additions & 530 deletions docs/example/ncompare-example-usage.ipynb

Large diffs are not rendered by default.

16 changes: 10 additions & 6 deletions ncompare/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
# See the License for the specific language governing permissions and limitations under the License.

"""Command line interface for `ncompare` -- to compare the structure of two NetCDF files."""

import argparse
import importlib.metadata
import sys
Expand All @@ -34,7 +35,7 @@

from ncompare.core import compare

__version__ = importlib.metadata.version('ncompare')
__version__ = importlib.metadata.version("ncompare")


def _cli(args: Optional[Sequence[str]]) -> argparse.Namespace:
Expand Down Expand Up @@ -65,7 +66,10 @@ def _cli(args: Optional[Sequence[str]]) -> argparse.Namespace:
)
parser.add_argument("--file-xlsx", help="An Excel file to which the output will be written.")
parser.add_argument(
"--no-color", action="store_true", default=False, help="Turn off all colorized output"
"--no-color",
action="store_true",
default=False,
help="Turn off all colorized output",
)
parser.add_argument(
"--show-attributes",
Expand All @@ -90,8 +94,8 @@ def _cli(args: Optional[Sequence[str]]) -> argparse.Namespace:

parser.add_argument(
"--version",
action='version',
version=f'%(prog)s {__version__}',
action="version",
version=f"%(prog)s {__version__}",
default=False,
help="Show the current version.",
)
Expand All @@ -103,7 +107,7 @@ def main() -> None: # pragma: no cover
"""Run from the command line."""
args = _cli(None)

delattr(args, 'version')
delattr(args, "version")

try:
compare(**vars(args))
Expand All @@ -113,5 +117,5 @@ def main() -> None: # pragma: no cover
sys.exit(0) # a clean, no-issue, exit


if __name__ == '__main__': # pragma: no cover
if __name__ == "__main__": # pragma: no cover
main()
143 changes: 87 additions & 56 deletions ncompare/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@
# pylint: disable=fixme

"""Compare the structure of two NetCDF files."""

import random
import traceback
from collections import namedtuple
from collections.abc import Iterable
from collections.abc import Iterable, Iterator
from pathlib import Path
from typing import Optional, Union

Expand All @@ -46,6 +47,12 @@

VarProperties = namedtuple("VarProperties", "varname, variable, dtype, shape, chunking, attributes")

GroupPair = namedtuple(
"GroupPair",
"group_a_name group_a group_b_name group_b",
defaults=("", None, "", None),
)


def compare(
nc_a: Union[str, Path],
Expand Down Expand Up @@ -195,7 +202,11 @@ def run_through_comparisons(
+ f"\nChecking multiple random values within specified variable <{comparison_var_name}>:"
)
compare_multiple_random_values(
out, nc_a, nc_b, groupname=comparison_var_group, varname=comparison_var_name
out,
nc_a,
nc_b,
groupname=comparison_var_group,
varname=comparison_var_name,
)

except KeyError:
Expand Down Expand Up @@ -250,12 +261,12 @@ def compare_multiple_random_values(
out.print("Done.", colors=False)


def walk_common_groups_tree( # type:ignore[misc]
def walk_common_groups_tree(
top_a_name: str,
top_a: Union[netCDF4.Dataset, netCDF4.Group],
top_b_name: str,
top_b: Union[netCDF4.Dataset, netCDF4.Group],
) -> tuple[str, netCDF4.Group, str, netCDF4.Group]:
) -> Iterator[GroupPair]:
"""Yield names and groups from a netCDF4's group tree.
Parameters
Expand All @@ -267,25 +278,30 @@ def walk_common_groups_tree( # type:ignore[misc]
Yields
------
group A name : str
group A object : netCDF4.Group
group B name : str
group B object : netCDF4.Group
tuple
group A name : str
group A object : netCDF4.Group or None
group B name : str
group B object : netCDF4.Group or None
"""
yield (
(
top_a_name + "/" + group_a_name if group_a_name else "",
top_a[group_a_name] if (group_a_name and (group_a_name in top_a.groups)) else None,
top_b_name + "/" + group_b_name if group_b_name else "",
top_b[group_b_name] if (group_b_name and (group_b_name in top_b.groups)) else None,
)
for (_, group_a_name, group_b_name) in common_elements(
top_a.groups if top_a is not None else "", top_b.groups if top_b is not None else ""
for _, group_a_name, group_b_name in common_elements(
top_a.groups if top_a is not None else "",
top_b.groups if top_b is not None else "",
):
yield GroupPair(
group_a_name=top_a_name + "/" + group_a_name if group_a_name else "",
group_a=(
top_a[group_a_name] if (group_a_name and (group_a_name in top_a.groups)) else None
),
group_b_name=top_b_name + "/" + group_b_name if group_b_name else "",
group_b=(
top_b[group_b_name] if (group_b_name and (group_b_name in top_b.groups)) else None
),
)
)

for _, subgroup_a_name, subgroup_b_name in common_elements(
top_a.groups if top_a is not None else "", top_b.groups if top_b is not None else ""
top_a.groups if top_a is not None else "",
top_b.groups if top_b is not None else "",
):
yield from walk_common_groups_tree(
top_a_name + "/" + subgroup_a_name if subgroup_a_name else "",
Expand All @@ -311,50 +327,57 @@ def compare_two_nc_files(
show_attributes: bool = False,
) -> tuple[int, int, int]:
"""Go through all groups and all variables, and show them side by side - whether they align and where they don't."""
out.side_by_side(' ', 'File A', 'File B', force_display_even_if_same=True)
out.side_by_side(" ", "File A", "File B", force_display_even_if_same=True)

num_var_diffs = {"left": 0, "right": 0, "both": 0}
with netCDF4.Dataset(nc_one) as nc_a, netCDF4.Dataset(nc_two) as nc_b:
out.side_by_side(
'All Variables', ' ', ' ', dash_line=False, force_display_even_if_same=True
"All Variables", " ", " ", dash_line=False, force_display_even_if_same=True
)
out.side_by_side('-', '-', '-', dash_line=True, force_display_even_if_same=True)
out.side_by_side("-", "-", "-", dash_line=True, force_display_even_if_same=True)

group_counter = 0
_print_group_details_side_by_side(
out, nc_a, "/", nc_b, "/", group_counter, num_var_diffs, show_attributes, show_chunks
out,
nc_a,
"/",
nc_b,
"/",
group_counter,
num_var_diffs,
show_attributes,
show_chunks,
)
group_counter += 1

for group_pairs in walk_common_groups_tree("", nc_a, "", nc_b):
for group_a_name, group_a, group_b_name, group_b in group_pairs:
_print_group_details_side_by_side(
out,
group_a,
group_a_name,
group_b,
group_b_name,
group_counter,
num_var_diffs,
show_attributes,
show_chunks,
)
group_counter += 1
for group_pair in walk_common_groups_tree("", nc_a, "", nc_b):
_print_group_details_side_by_side(
out,
group_pair.group_a,
group_pair.group_a_name,
group_pair.group_b,
group_pair.group_b_name,
group_counter,
num_var_diffs,
show_attributes,
show_chunks,
)
group_counter += 1

out.side_by_side('-', '-', '-', dash_line=True, force_display_even_if_same=True)
out.side_by_side("-", "-", "-", dash_line=True, force_display_even_if_same=True)
out.side_by_side(
'Total number of shared items:',
str(num_var_diffs['both']),
str(num_var_diffs['both']),
"Total number of shared items:",
str(num_var_diffs["both"]),
str(num_var_diffs["both"]),
force_display_even_if_same=True,
)
out.side_by_side(
'Total number of non-shared items:',
str(num_var_diffs['left']),
str(num_var_diffs['right']),
"Total number of non-shared items:",
str(num_var_diffs["left"]),
str(num_var_diffs["right"]),
force_display_even_if_same=True,
)
return num_var_diffs['left'], num_var_diffs['right'], num_var_diffs['both']
return num_var_diffs["left"], num_var_diffs["right"], num_var_diffs["both"]


def _print_group_details_side_by_side(
Expand All @@ -369,7 +392,12 @@ def _print_group_details_side_by_side(
show_chunks: bool,
) -> None:
out.side_by_side(
" ", " ", " ", dash_line=False, highlight_diff=False, force_display_even_if_same=True
" ",
" ",
" ",
dash_line=False,
highlight_diff=False,
force_display_even_if_same=True,
)
out.side_by_side(
f"GROUP #{group_counter:02}",
Expand All @@ -388,19 +416,19 @@ def _print_group_details_side_by_side(
if group_b:
vars_b_sorted = sorted(group_b.variables)
out.side_by_side(
'num variables in group:',
"num variables in group:",
len(vars_a_sorted),
len(vars_b_sorted),
highlight_diff=True,
force_display_even_if_same=True,
)
out.side_by_side('-', '-', '-', dash_line=True, force_display_even_if_same=True)
out.side_by_side("-", "-", "-", dash_line=True, force_display_even_if_same=True)

# Count differences between the lists of variables in this group.
left, right, both = count_diffs(vars_a_sorted, vars_b_sorted)
num_var_diffs['left'] += left
num_var_diffs['right'] += right
num_var_diffs['both'] += both
num_var_diffs["left"] += left
num_var_diffs["right"] += right
num_var_diffs["both"] += both

# Go through each variable in the current group.
for variable_pair in common_elements(vars_a_sorted, vars_b_sorted):
Expand Down Expand Up @@ -473,7 +501,10 @@ def _print_var_properties_side_by_side(
for attr_a_key, attr_a, attr_b_key, attr_b in get_and_check_variable_attributes(v_a, v_b):
# Check whether attr_a_key is empty, because it might be if the variable doesn't exist in File A.
out.side_by_side(
f"{attr_a_key if attr_a_key else attr_b_key}:", attr_a, attr_b, highlight_diff=True
f"{attr_a_key if attr_a_key else attr_b_key}:",
attr_a,
attr_b,
highlight_diff=True,
)

# Scale Factor
Expand All @@ -483,14 +514,14 @@ def _print_var_properties_side_by_side(


def get_and_check_variable_scale_factor(v_a, v_b) -> Union[None, tuple[str, str]]:
if getattr(v_a.variable, 'scale_factor', None):
if getattr(v_a.variable, "scale_factor", None):
sf_a = v_a.variable.scale_factor
else:
sf_a = ' '
if getattr(v_b.variable, 'scale_factor', None):
sf_a = " "
if getattr(v_b.variable, "scale_factor", None):
sf_b = v_b.variable.scale_factor
else:
sf_b = ' '
sf_b = " "
if (sf_a != " ") or (sf_b != " "):
return str(sf_a), str(sf_b)
else:
Expand Down
Loading

0 comments on commit 805efd6

Please sign in to comment.