Skip to content

Commit

Permalink
Merge branch 'feature-multiple-mask-files' into development
Browse files Browse the repository at this point in the history
  • Loading branch information
markjonestx committed Jul 6, 2017
2 parents 27ddf98 + 4844370 commit 77a5712
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/)
### Added
- Process Plugin support for List Data
- Adds Exception handling to Processes
- PyMask support for multiple masking files.
### Changed
- Removed previous_event from Process Interface
- Duplex Pipes are used over Simplex Pipes for Duplex Processes
Expand Down
2 changes: 1 addition & 1 deletion PyPWA/progs/masking/__init__.py
Expand Up @@ -50,7 +50,7 @@ def __add_input_argument(self):

def __add_masking_argument(self):
self._parser.add_argument(
"--mask", "-m", type=str,help="Masking file"
"--mask", "-m", type=str, action="append", help="Masking file"
)

def __add_output_file(self):
Expand Down
19 changes: 15 additions & 4 deletions PyPWA/progs/masking/masking.py
Expand Up @@ -25,6 +25,7 @@

import logging
import warnings
from typing import List
from typing import Optional as Opt

import numpy
Expand Down Expand Up @@ -60,7 +61,7 @@ def __setup_data(self, input_file, output_file, masking_file, parser):
# type: (str, str, Opt[str], plugins.DataParser) -> None
self.__load_writer(input_file, output_file)
self.__load_reader(input_file)
self.__setup_masking_file(masking_file, parser)
self.__setup_mask_array(masking_file, parser)

def __load_writer(self, input_file, output_file):
# type: (str, str) -> None
Expand All @@ -73,13 +74,23 @@ def __load_reader(self, input_file):
# type: (str) -> None
self.__reader = self.__iterator.return_reader(input_file)

def __setup_masking_file(self, masking_file, parser):
# type: (Opt[str], plugins.DataParser) -> None
def __setup_mask_array(self, masking_file, parser):
# type: (Opt[List[str]], plugins.DataParser) -> None
if masking_file:
self.__mask = parser.parse(masking_file)
self.__mask = self.__load_masking_files(masking_file, parser)
else:
self.__mask = numpy.ones(len(self.__reader), dtype=bool)

def __load_masking_files(self, masking_files, parser):
# type: (List[str], plugins.DataParser) -> numpy.ndarray
mask = None # type: numpy.ndarray
for file in masking_files:
if isinstance(mask, type(None)):
mask = parser.parse(file)
else:
mask += parser.parse(file)
return mask

@property
def reader(self):
# type: () -> internals.Reader
Expand Down
4 changes: 4 additions & 0 deletions tests/data/test_docs/sv_test_data2.pf
@@ -0,0 +1,4 @@
0
0
1
0
32 changes: 28 additions & 4 deletions tests/progs/masking/test_masking.py
Expand Up @@ -4,6 +4,7 @@
import pytest

from PyPWA.entries import arguments
from PyPWA.core.shared import file_libs

"""
Masking Data
Expand All @@ -17,6 +18,10 @@
os.path.dirname(__file__), "../../data/test_docs/sv_test_data.pf"
)

PF2 = os.path.join(
os.path.dirname(__file__), "../../data/test_docs/sv_test_data2.pf"
)

PF_SHORT = os.path.join(
os.path.dirname(__file__), "../../data/test_docs/sv_test_data_short.pf"
)
Expand All @@ -25,11 +30,11 @@
os.path.dirname(__file__), "../../data/test_docs/sv_test_data_long.pf"
)


"""
Test Masking with Standard Data
"""


@pytest.fixture()
def correct_argv(monkeypatch):
original = sys.argv
Expand All @@ -45,7 +50,7 @@ def cleanup_temp():

tested_args = [
["pymask", "--input", INPUT, "-o", "testfile.txt"],
["pymask", "-i", INPUT, "--mask", PF, "-o", "testfile.txt"]
["pymask", "-i", INPUT, "--mask", PF, "-m", PF2, "-o", "testfile.txt"]
]
@pytest.fixture(params=tested_args)
def patch_args_clean(monkeypatch, request, correct_argv, cleanup_temp):
Expand All @@ -57,9 +62,29 @@ def test_masking_utility(patch_args_clean):


"""
Test Masking with more masked events than data
Test Masking with Standard Data
"""

@pytest.fixture()
def patch_args_double_mask(monkeypatch, request, correct_argv, cleanup_temp):
monkeypatch.setattr(
"sys.argv",
[
"pymask",
"-i", INPUT, "--mask", PF, "-m", PF2, "-o", "testfile.txt"
]
)
arguments.masking_utility()
yield


def test_masking_utility_has_correct_number_of_lines(patch_args_double_mask):
assert file_libs.get_file_length("testfile.txt") == 1


"""
Test Masking with more masked events than data
"""

@pytest.fixture()
def patch_args_warning(monkeypatch, correct_argv, cleanup_temp):
Expand All @@ -78,7 +103,6 @@ def test_masking_warning(patch_args_warning):
Test Masking with less masked events than data
"""


@pytest.fixture()
def patch_args_critical(monkeypatch, correct_argv, cleanup_temp):
monkeypatch.setattr(
Expand Down

0 comments on commit 77a5712

Please sign in to comment.