Skip to content

Commit

Permalink
Merge pull request #3 from Shimwell/develop
Browse files Browse the repository at this point in the history
update to dockerfile
  • Loading branch information
shimwell authored Apr 16, 2021
2 parents 8d94d7b + 11f3008 commit f7c58a1
Show file tree
Hide file tree
Showing 10 changed files with 342 additions and 53 deletions.
1 change: 1 addition & 0 deletions .github/workflows/docker_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name: docker based CI
on:
pull_request:

jobs:
build:
runs-on: ubuntu-latest
Expand Down
16 changes: 15 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,16 @@ RUN apt-get --yes install libeigen3-dev && \
apt-get --yes install libtbb-dev && \
apt-get --yes install libglfw3-dev && \
apt-get --yes install cmake && \
apt-get --yes install g++ && \
apt-get --yes install git

# Clone and install MOAB
RUN pip install --upgrade numpy cython
RUN mkdir MOAB && \
cd MOAB && \
git clone --single-branch --branch 5.2.1 --depth 1 https://bitbucket.org/fathomteam/moab.git && \
git clone --single-branch --branch 5.2.1 --depth 1 https://bitbucket.org/fathomteam/moab.git

RUN cd MOAB && \
mkdir build && \
cd build && \
cmake ../moab -DENABLE_HDF5=ON \
Expand All @@ -56,5 +59,16 @@ RUN mkdir MOAB && \
-DCMAKE_INSTALL_PREFIX=/MOAB && \
make install && \
cd pymoab && \
# not quite sure why these two lines are needed but it appears that pymoab
# is not available as "import pymoab" without them
bash install.sh && \
python setup.py install

RUN pip install pytest-cov

COPY setup.py setup.py
COPY remove_dagmc_tags remove_dagmc_tags/
COPY tests tests/
COPY README.md README.md

RUN python setup.py install
42 changes: 34 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
[![N|Python](https://www.python.org/static/community_logos/python-powered-w-100x40.png)](https://www.python.org)
[![docker based CI](https://github.com/Shimwell/remove_dagmc_tags/actions/workflows/docker_ci.yml/badge.svg)](https://github.com/Shimwell/remove_dagmc_tags/actions/workflows/docker_ci.yml)
[![PyPI](https://img.shields.io/pypi/v/remove-dagmc-tags?color=brightgreen&label=pypi&logo=grebrightgreenen&logoColor=green)](https://pypi.org/project/remove-dagmc-tags/)
[![codecov](https://codecov.io/gh/shimwell/remove_dagmc_tags/branch/main/graph/badge.svg)](https://codecov.io/gh/shimwell/remove_dagmc_tags)

This is a minimal Python package that provides both **command line** and **API** interfaces for removing **multiple** tags from a DAGMC h5m file.

This is useful for preparing the h5m file for visulisation where it is desirable to remove reflecting surfaces, vacuum materials and the graveyard(s) region.
This is useful for preparing the h5m file for visulisation where it is desirable to remove reflecting surfaces, vacuum materials and the graveyard(s) region
from the geometry before viewing the vtk file.


# Command line usage
Expand All @@ -12,19 +17,21 @@ remove-dagmc-tags -i dagmc.h5m -o dagmc_no_graveyard.h5m -t graveyard
```

- the ```-i``` or ```--input``` argument specifies the input h5m file
- the ```-o``` or ```--output``` argument specifies the output h5m file
- the ```-o``` or ```--output``` argument specifies the output h5m or vtk filename
- the ```-t``` or ```--tags``` argument specifies the tags to remove.
- the ```-v``` or ```--verbose``` argument enables (true) or disables (false) the printing of additional details

Multiple tags can also be removed. This example removes three tags from the dagmc.h5m file
Multiple tags can also be removed and vtk files can be generated. This example
removes three tags from the dagmc.h5m file and saves the result as a vtk file.

```bash
remove-dagmc-tags -i dagmc.h5m -o dagmc_output.h5m -t mat:graveyard mat:vacuum reflective
remove-dagmc-tags -i dagmc.h5m -o output.vtk -t mat:graveyard mat:vacuum reflective
```

# Python API usage

Removing a single tag called ```mat:graveyard``` from the dagmc.h5m file.

```python
from remove_dagmc_tags import remove_tags

Expand All @@ -35,26 +42,45 @@ remove_tags(
)
```

Removing two tags called ```mat:graveyard``` and ```reflective``` from the dagmc.h5m file.
Removing two tags called ```mat:graveyard``` and ```reflective``` from the
dagmc.h5m file and saves the result as a vtk file and h5m file.

```python
from remove_dagmc_tags import remove_tags

remove_tags(
input='dagmc.h5m',
output='output.h5m',
tags='reflective'
output=['output.vtk', 'output.h5m'],
tags=['reflective', 'mat:graveyard']
)
```

# Installation

The recommended method of installing is via conda install as this is able to
install all the dependencies including PyMoab.
```bash
conda install -c conda-forge remove_dagmc_tags
```

However the package is available via the PyPi package manager. In this case you
will have to seperatly install PyMoab.

```bash
pip install remove_dagmc_tags
```

Some Python dependencies (such as Numpy) are installed during the ```pip install remove_dagmc_tags process```, however [PyMoab](https://bitbucket.org/fathomteam/moab/src/master/) needs to be installed seperatly to make full use of this package.

One method of installing ```pymoab``` is to compile MOAB with pymoab enabled.

One method of installing ```pymoab``` is to install MOAB via Conda which
includes PyMoab.

```bash
conda install -c conda-forge moab
```

Another method of installing ```pymoab``` is to compile MOAB with pymoab enabled.

```bash
mkdir MOAB
Expand Down
2 changes: 2 additions & 0 deletions remove_dagmc_tags/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

from .core import remove_tags, find_tags
137 changes: 137 additions & 0 deletions remove_dagmc_tags/core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@

import warnings
from pathlib import Path
from typing import List, Optional, Union

import numpy as np
from pymoab import core, types
from pymoab.types import MBENTITYSET


def create_moab_core(input):

moab_core = core.Core()
moab_core.load_file(str(input))

tag_name = moab_core.tag_get_handle(str(types.NAME_TAG_NAME))

tag_category = moab_core.tag_get_handle(str(types.CATEGORY_TAG_NAME))
root = moab_core.get_root_set()

# An array of tag values to be matched for entities returned by the
# following call.
group_tag_values = np.array(["Group"])

# Retrieve all EntitySets with a category tag of the user input value.
group_categories = list(moab_core.get_entities_by_type_and_tag(
root, MBENTITYSET, tag_category, group_tag_values))

return moab_core, group_categories, tag_name


def find_tags(
input: Optional[str] = 'dagmc.h5m',
) -> List[str]:
"""Removes a specific tag from a dagmc h5m file and saves the remaining
geometry as a new h5m file. Useful for visulising the geometry by removing
the graveyard tag and then the vtk file can be made without a bounding box
graveyard obstructing the view. Adapted from
https://github.com/svalinn/DAGMC-viz source code
Arguments:
input: The name of the h5m file to remove the dagmc tags from
output: The name of the outfile file(s) with the tags removed.
Supported extentions are .vtk and .h5m
tags: The tag or tags to remove.
verbose:
Returns:
filename of the new dagmc h5m file with the tags removed
"""

moab_core, group_categories, tag_name = create_moab_core(input)

# Retrieve all EntitySets with a name tag.
group_names = moab_core.tag_get_data(tag_name, group_categories, flat=True)

return group_names


def remove_tags(
input: Optional[str] = 'dagmc.h5m',
output: Optional[Union[str, List[str]]] = 'dagmc_removed_tag.vtk',
tags: Optional[Union[str, List[str]]] = 'graveyard',
verbose: Optional[bool] = False,
):
# -> List[List[str], List[str], List[str]]:
"""Removes a specific tag from a dagmc h5m file and saves the remaining
geometry as a new h5m file. Useful for visulising the geometry by removing
the graveyard tag and then the vtk file can be made without a bounding box
graveyard obstructing the view. Adapted from
https://github.com/svalinn/DAGMC-viz source code
Arguments:
input: The name of the h5m file to remove the dagmc tags from
output: The name of the outfile file(s) with the tags removed.
Supported extentions are .vtk and .h5m
tags: The tag(S) to be removed.
verbose: Print out additional information (True) or not (False)
Returns:
filename(s) of the output files produced, names of tags removed, names
of all the tags available
"""
if verbose is True:
print()
print('tags_to_remove', tags)

moab_core, group_categories, tag_name = create_moab_core(input)

group_names = find_tags(input=input)

if isinstance(tags, str):
tags_to_remove = [tags]
else:
tags_to_remove = tags

# Find the EntitySet whose name includes tag provided
sets_to_remove = []
names_to_remove = []
names_not_remove_removed = []

for group_set, name in zip(group_categories, group_names):
for tag_to_remove in tags_to_remove:
if tag_to_remove in str(name.lower()):
names_to_remove.append(name.lower())
sets_to_remove.append(group_set)
else:
names_not_remove_removed.append(name.lower())

names_to_remove = list(sorted(set(names_to_remove)))

if len(sets_to_remove) == 0:
warnings.warn('No tags removed.')

# prints out
if verbose is True:
print()
for name in sorted(set(group_names)):
if str(name.lower()) in tags_to_remove:
print(str(name.lower()), ' --- > Removing tag')
else:
print(str(name.lower()))
print()

# Remove the EntitySet from the data.
groups_to_write = [
group_set for group_set in group_categories if group_set not in sets_to_remove]

if isinstance(output, (str, Path)):
output = [output]

for out in output:
if verbose is True:
print('Writing', out)
moab_core.write_file(str(out), output_sets=groups_to_write)

return output, names_to_remove, sorted(set(group_names))
45 changes: 45 additions & 0 deletions remove_dagmc_tags/remove-dagmc-tags
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/python

from remove_dagmc_tags import remove_tags
from pathlib import Path
import argparse


if __name__ == '__main__':

parser = argparse.ArgumentParser()

parser.add_argument(
'-t', '--tags',
nargs='*',
default=['graveyard'],
help="The tag or tags to remove."
)

parser.add_argument(
'-i', '--input',
type=Path, default=None,
help='Input h5m file',
required=True
)

parser.add_argument(
'-o', '--output',
type=Path, default='dagmc_removed_tag.h5m',
help='Output h5m file with removed tags'
)

parser.add_argument(
'-v', '--verbose',
type=bool, default=True,
help='Print additional details of the opperation'
)

args = parser.parse_args()

remove_tags(
input=args.input,
output=args.output,
tags=args.tags,
verbose=args.verbose
)
30 changes: 30 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import setuptools

with open("README.md", "r") as fh:
long_description = fh.read()

setuptools.setup(
name="remove_dagmc_tags",
version="0.0.2",
author="Jonathan Shimwell",
author_email="[email protected]",
description="A tool for selectively removing tags such as the graveyard from DAGMC h5m files.",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/shimwell/remove_dagmc_tags",
packages=setuptools.find_packages(),
zip_safe=True,
package_dir={"remove_dagmc_tags": "remove_dagmc_tags"},
scripts=['remove_dagmc_tags/remove-dagmc-tags'],
package_data={
"remove_dagmc_tags": [
"requirements.txt",
"README.md",
"LICENSE",
]
},
tests_require=["pytest-cov"],
# install_requires=[
# # 'pymoab' is is required for this package but is not available via pip install at the moment
# ],
)
Binary file added tests/dagmc.h5m
Binary file not shown.
Loading

0 comments on commit f7c58a1

Please sign in to comment.