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

Fix for filenames which contain regular expressions #231

Merged
merged 3 commits into from
Sep 12, 2022
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
2 changes: 1 addition & 1 deletion anesthetic/read/cobaya.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def read_cobaya(root, *args, **kwargs):
dirname, basename = os.path.split(root)

files = os.listdir(os.path.dirname(root))
regex = basename + r'.([0-9]+)\.txt'
regex = re.escape(basename) + r'.([0-9]+)\.txt'
matches = [re.match(regex, f) for f in files]
chains_files = [(m.group(1), os.path.join(dirname, m.group(0)))
for m in matches if m]
Expand Down
2 changes: 1 addition & 1 deletion anesthetic/read/getdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def read_getdist(root, *args, **kwargs):
dirname, basename = os.path.split(root)

files = os.listdir(os.path.dirname(root))
regex = basename + r'((_|.)([0-9]+)|)\.txt'
regex = re.escape(basename) + r'((_|.)([0-9]+)|)\.txt'
matches = [re.match(regex, f) for f in files]
chains_files = [(m.group(3), os.path.join(dirname, m.group(0)))
for m in matches if m]
Expand Down
4 changes: 0 additions & 4 deletions tests/example_data/gd.ranges

This file was deleted.

5 changes: 5 additions & 0 deletions tests/example_data/gd_single+X.paramnames
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
x0 x_0
x1 x_1
x2 x_2
x3 x_3
x4 x_4
10,000 changes: 10,000 additions & 0 deletions tests/example_data/gd_single+X.txt

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions tests/example_data/gd_single.ranges

This file was deleted.

4 changes: 0 additions & 4 deletions tests/example_data/mn.ranges

This file was deleted.

4 changes: 0 additions & 4 deletions tests/example_data/mn_old.ranges

This file was deleted.

3 changes: 0 additions & 3 deletions tests/example_data/pc.ranges

This file was deleted.

4 changes: 0 additions & 4 deletions tests/example_data/pc_250.ranges

This file was deleted.

4 changes: 0 additions & 4 deletions tests/example_data/pc_single_live.ranges

This file was deleted.

4 changes: 0 additions & 4 deletions tests/example_data/pc_zero_live.ranges

This file was deleted.

7 changes: 7 additions & 0 deletions tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pytest
import numpy as np
from numpy.testing import assert_array_equal, assert_array_almost_equal
from pandas.testing import assert_frame_equal
import matplotlib.pyplot as plt
from anesthetic import MCMCSamples, NestedSamples
from anesthetic import read_chains
Expand Down Expand Up @@ -253,3 +254,9 @@ def test_discard_burn_in(root):
def test_read_fail():
with pytest.raises(FileNotFoundError):
read_chains('./tests/example_data/foo')


def test_regex_escape():
mcmc_1 = read_chains('./tests/example_data/gd_single+X')
mcmc_2 = read_chains('./tests/example_data/gd_single')
assert_frame_equal(mcmc_1, mcmc_2)