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

Replace os.path Usage by pathlib #28

Merged
merged 1 commit into from
Jun 26, 2024
Merged
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
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@
# POSSIBILITY OF SUCH DAMAGE.
#
# SPDX-License-Identifier: BSD-3-Clause
import os
import re
from pathlib import Path

import setuptools


def read_file(*path):
"""Read file content."""
package_path = os.path.abspath(os.path.dirname(__file__))
with open(os.path.join(package_path, *path)) as text_file:
package_path = Path(__file__).parent.resolve()
with open(package_path.joinpath(*path)) as text_file:
return text_file.read()


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
import abc
import contextlib
import ctypes
import os
import warnings
from pathlib import Path

from stencil_benchmarks.benchmark import (
Benchmark,
Expand Down Expand Up @@ -63,10 +63,8 @@ def setup(self):
if self.backend == "cuda" and self.timers == "hip-ext":
raise ParameterError("hip-ext timers are not compatible with CUDA")

template_file = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"templates",
self.template_file(),
template_file = (
Path(__file__).parent.resolve() / "templates" / self.template_file()
)
code = template.render(template_file, **self.template_args())
code = cpphelpers.format_code(code, line_numbers=False)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import ctypes
import os
import warnings
from pathlib import Path

from stencil_benchmarks.benchmark import Benchmark, ExecutionError, Parameter
from stencil_benchmarks.tools import compilation, cpphelpers, template
Expand Down Expand Up @@ -63,9 +64,7 @@ class StencilMixin(Benchmark):
def setup(self):
super().setup()

template_file = os.path.join(
self.template_path(), "templates", self.template_file()
)
template_file = self.template_path() / "templates" / self.template_file()
code = template.render(template_file, **self.template_args())
code = cpphelpers.format_code(code, line_numbers=False)

Expand All @@ -84,7 +83,7 @@ def setup(self):
)

def template_path(self):
return os.path.dirname(os.path.abspath(__file__))
return Path(__file__).parent.resolve()

def compile_command(self):
command = [self.compiler]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#
# SPDX-License-Identifier: BSD-3-Clause
import contextlib
import os
from pathlib import Path

import numpy as np

Expand Down Expand Up @@ -60,7 +60,7 @@ def setup(self):
)

def template_path(self):
return os.path.dirname(os.path.abspath(__file__))
return Path(__file__).parent.resolve()

@property
def blocked_domain(self):
Expand Down
6 changes: 2 additions & 4 deletions stencil_benchmarks/benchmarks_collection/stream/cuda_hip.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
# POSSIBILITY OF SUCH DAMAGE.
#
# SPDX-License-Identifier: BSD-3-Clause
import os
import re
import warnings
from pathlib import Path

from ...benchmark import Benchmark, ExecutionError, Parameter
from ...tools import compilation, cpphelpers, template
Expand Down Expand Up @@ -70,9 +70,7 @@ def setup(self):
(self.array_size + elements_per_block - 1) // elements_per_block
) * elements_per_block

template_file = os.path.join(
os.path.dirname(os.path.abspath(__file__)), "cuda_hip.j2"
)
template_file = Path(__file__).parent.resolve() / "cuda_hip.j2"
code = template.render(template_file, **self.template_args())
if self.print_code:
print(cpphelpers.format_code(code))
Expand Down
5 changes: 2 additions & 3 deletions stencil_benchmarks/benchmarks_collection/stream/mc_calpin.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
# SPDX-License-Identifier: BSD-3-Clause
import os
import re
from pathlib import Path

import numpy as np

Expand Down Expand Up @@ -59,9 +60,7 @@ def setup(self):
if self.compiler.endswith("icpc"):
os.environ["KMP_INIT_AT_FORK"] = "0"

template_file = os.path.join(
os.path.dirname(os.path.abspath(__file__)), self.template_file()
)
template_file = Path(__file__).parent.resolve() / self.template_file()
code = template.render(template_file, **self.template_args())
if self.print_kernels:
print(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import ctypes
import os
import warnings
from pathlib import Path

from stencil_benchmarks.benchmark import Benchmark, ExecutionError, Parameter
from stencil_benchmarks.tools import compilation, cpphelpers, template
Expand Down Expand Up @@ -64,8 +65,8 @@ class UnstructuredMixin(Benchmark):
def setup(self):
super().setup()

template_file = os.path.join(
self.template_path(), "templates", self.template_file()
template_file = (
Path(__file__).parent.resolve() / "templates" / self.template_file()
)
code = template.render(template_file, **self.template_args())
code = cpphelpers.format_code(code, line_numbers=False)
Expand All @@ -84,9 +85,6 @@ def setup(self):
"false negatives for stencils with read-write fields"
)

def template_path(self):
return os.path.dirname(os.path.abspath(__file__))

def compile_command(self):
command = [self.compiler]
if self.platform_preset != "none":
Expand Down
15 changes: 6 additions & 9 deletions stencil_benchmarks/scripts/sbench_analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
#
# SPDX-License-Identifier: BSD-3-Clause

import os
import re
import textwrap
from pathlib import Path

import click
import numpy as np
Expand Down Expand Up @@ -84,7 +84,7 @@ def arrange(df, query, groups, aggregation, unstack, select, sort):


@main.command(name="print")
@click.argument("csv", type=click.Path(exists=True))
@click.argument("csv", type=click.Path(exists=True, path_type=Path))
@click.option(
"--common/--non-common",
"-c",
Expand Down Expand Up @@ -134,7 +134,7 @@ def print_csv(


@main.command()
@click.argument("csv", type=click.Path(exists=True))
@click.argument("csv", type=click.Path(exists=True, path_type=Path))
@click.option("--uniform/--non-uniform", help="Equidistant placement of x-axis ticks.")
@click.option("--ylim", type=float, nargs=2, help="Y-axis limits.")
@click.option("--title", "-t", help="Plot title.")
Expand Down Expand Up @@ -166,7 +166,7 @@ def print_csv(
help="Search and replace a pattern in the final labels, "
"input as /pattern/repl/ in Python regex syntax.",
)
@click.option("--output", "-o", type=click.Path(), help="Output file.")
@click.option("--output", "-o", type=click.Path(path_type=Path), help="Output file.")
@click.option("--dpi", default=300, type=int, help="Output DPI (dots per inch).")
def plot(
csv,
Expand All @@ -186,9 +186,6 @@ def plot(
dpi,
):
"""Plot output of sbench.

X is the data column name for the values used for the x-axis in the plot, Y
is the column name for the y-axis.
"""
import cycler
from matplotlib import pyplot as plt
Expand Down Expand Up @@ -247,7 +244,7 @@ def plot(
if relative_to:
plt.gca().yaxis.set_major_formatter(ticker.PercentFormatter(1.0))
if not title:
title = os.path.split(csv)[-1]
title = csv.name
plt.title(title)
subtitle = ", ".join(f"{k}: {v}" for k, v in common.items())
plt.text(
Expand All @@ -269,7 +266,7 @@ def plot(


@main.command()
@click.argument("csv", type=click.Path(exists=True), nargs=-1)
@click.argument("csv", type=click.Path(exists=True, path_type=Path), nargs=-1)
@click.argument("output", type=click.File(mode="w"))
def merge(csv, output):
"""Merge multiple CSV files produces by sbench."""
Expand Down
12 changes: 6 additions & 6 deletions stencil_benchmarks/tools/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@
# POSSIBILITY OF SUCH DAMAGE.
#
# SPDX-License-Identifier: BSD-3-Clause
import os
from typing import Any, Dict
from pathlib import Path
from typing import Any, Dict, Union

import jinja2


def render(template_file: str, **kwargs: Dict[str, Any]) -> str:
template_path, template_file = os.path.split(template_file)
env = jinja2.Environment(loader=jinja2.FileSystemLoader(template_path))
template = env.get_template(template_file)
def render(template_file: Union[str, Path], **kwargs: Dict[str, Any]) -> str:
template_file = Path(template_file)
env = jinja2.Environment(loader=jinja2.FileSystemLoader(template_file.parent))
template = env.get_template(template_file.name)
return template.render(**kwargs)