Skip to content

Commit e085e37

Browse files
authored
Merge pull request #236 from pynbody/timestep-match
Add option to tangos write to target particular timesteps
2 parents 67124b6 + 24eb130 commit e085e37

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

tangos/tools/property_writer.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ def add_parser_arguments(self, parser):
4444
help='Specify a simulation (or multiple simulations) to run on')
4545
parser.add_argument('--latest', action='store_true',
4646
help='Run only on the latest timesteps')
47+
parser.add_argument('--timesteps-matching', action='append', type=str,
48+
help='Run only on timesteps with extensions matching the specified string. Multiple timesteps may be specified.')
4749
parser.add_argument('--force', action='store_true',
4850
help='Run calculations even if a value is already stored in the database')
4951
parser.add_argument('--debug', action='store_true',
@@ -96,10 +98,18 @@ def _build_file_list(self):
9698
except IndexError:
9799
pass
98100
else:
99-
files = core.get_default_session().query(core.timestep.TimeStep).filter(
100-
core.timestep.TimeStep.simulation_id.in_([q.id for q in query.all()])). \
101+
timestep_filter = core.timestep.TimeStep.simulation_id.in_([q.id for q in query.all()])
102+
if self.options.timesteps_matching is not None and len(self.options.timesteps_matching)>0:
103+
subfilter = core.timestep.TimeStep.extension.like(self.options.timesteps_matching[0])
104+
for m in self.options.timesteps_matching[1:]:
105+
subfilter |= core.timestep.TimeStep.extension.like(m)
106+
timestep_filter &= subfilter
107+
108+
files = core.get_default_session().query(core.timestep.TimeStep).filter(timestep_filter). \
101109
order_by(core.timestep.TimeStep.time_gyr).all()
102110

111+
112+
103113
if self.options.backwards:
104114
files = files[::-1]
105115

tests/test_db_writer.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
from tangos.util import proxy_object
1212

1313

14-
def setup_func():
14+
def setup_func(sim="dummy_sim_1"):
1515
parallel_tasks.use('null')
1616

1717
testing.init_blank_db_for_testing()
1818
db.config.base = os.path.join(os.path.dirname(__file__), "test_simulations")
19-
manager = add_simulation.SimulationAdderUpdater(output_testing.TestInputHandler("dummy_sim_1"))
19+
manager = add_simulation.SimulationAdderUpdater(output_testing.TestInputHandler(sim))
2020
with log.LogCapturer():
2121
manager.scan_simulation_and_add_all_descendants()
2222

@@ -29,6 +29,12 @@ def fresh_database():
2929
yield
3030
teardown_func()
3131

32+
@fixture
33+
def fresh_database_2():
34+
setup_func("dummy_sim_2")
35+
yield
36+
teardown_func()
37+
3238
class DummyProperty(properties.PropertyCalculation):
3339
names = "dummy_property",
3440
requires_particle_data = True
@@ -225,3 +231,10 @@ def test_writer_handles_sim_properties(fresh_database):
225231
ts = db.get_timestep("dummy_sim_1/step.%d"%i)
226232
x, = ts.calculate_all("dummy_property_accessing_simulation_property")
227233
npt.assert_equal(x,[1]*ts.halos.count())
234+
235+
def test_timesteps_matching(fresh_database_2):
236+
run_writer_with_args("dummy_property", "--timesteps-matching", "step.1", "--timesteps-matching", "step.2")
237+
assert 'dummy_property' in db.get_halo("dummy_sim_2/step.1/1").keys()
238+
assert 'dummy_property' in db.get_halo("dummy_sim_2/step.1/2").keys()
239+
assert 'dummy_property' in db.get_halo("dummy_sim_2/step.2/1").keys()
240+
assert 'dummy_property' not in db.get_halo("dummy_sim_2/step.3/1").keys()

0 commit comments

Comments
 (0)