From 22122f8f2a08fcaaea87181584ed2545610f6421 Mon Sep 17 00:00:00 2001 From: Steven Rieder Date: Thu, 6 Jul 2023 19:07:19 +0200 Subject: [PATCH 1/2] io test fixes (#969) * rename class to prevent collection warning * prevent writing to a possibly existing file * randomise tempdir name --- src/amuse/test/amusetest.py | 10 +- src/amuse/test/suite/core_tests/test_io.py | 24 +- .../test/suite/core_tests/test_textio.py | 310 +++++++++--------- 3 files changed, 179 insertions(+), 165 deletions(-) diff --git a/src/amuse/test/amusetest.py b/src/amuse/test/amusetest.py index 51cdf92c50..4f980cb6b8 100644 --- a/src/amuse/test/amusetest.py +++ b/src/amuse/test/amusetest.py @@ -302,8 +302,8 @@ class TestDefaults(_Defaults): @late def temporarydir(self): - dirname=tempfile.mkdtemp() - print(("generating temporary dir for test results: {0}". format(dirname))) + dirname = tempfile.mkdtemp(dir="./") + print(f"generating temporary dir for test results: {dirname}") return dirname @options.option(sections=['test']) @@ -317,7 +317,7 @@ def path_to_results(self): test_results_dir = os.path.join(amuse_root_dir, self.name_of_testresults_directory) if os.path.exists(test_results_dir): try: - f = open(os.path.join(test_results_dir,'test.txt'),'w') + f = open(os.path.join(test_results_dir, 'test.txt'), 'w') f.close() return test_results_dir except IOError as ex: @@ -327,9 +327,9 @@ def path_to_results(self): @options.option(sections=['test']) def name_of_testresults_directory(self): - return 'test_results' + return self.temporarydir - @options.option(type='boolean',sections=['test']) + @options.option(type='boolean', sections=['test']) def can_run_tests_to_compile_modules(self): return True diff --git a/src/amuse/test/suite/core_tests/test_io.py b/src/amuse/test/suite/core_tests/test_io.py index 73947fb77c..447ee5461e 100644 --- a/src/amuse/test/suite/core_tests/test_io.py +++ b/src/amuse/test/suite/core_tests/test_io.py @@ -10,7 +10,7 @@ from amuse import datamodel -class TestFileFormatProcessor(base.FileFormatProcessor): +class ForTestFileFormatProcessor(base.FileFormatProcessor): """ Save files in a test format @@ -21,7 +21,7 @@ class TestFileFormatProcessor(base.FileFormatProcessor): def __init__(self, filename=None, set=None, format=None): base.FileFormatProcessor.__init__(self, filename, set, format) - TestFileFormatProcessor.instance = self + ForTestFileFormatProcessor.instance = self self.stored = False @base.format_option @@ -44,24 +44,24 @@ def load(self): class FrameworkTests(amusetest.TestCase): def test1(self): - options = TestFileFormatProcessor.get_options() + options = ForTestFileFormatProcessor.get_options() self.assertTrue('add_comma' in options) self.assertTrue('save_fast' in options) - TestFileFormatProcessor.register() + ForTestFileFormatProcessor.register() base.write_set_to_file(None, "test.txt", format="123") - self.assertEqual(TestFileFormatProcessor.instance.set, None) - self.assertEqual(TestFileFormatProcessor.instance.filename, "test.txt") - self.assertEqual(TestFileFormatProcessor.instance.format, "123") - self.assertTrue(TestFileFormatProcessor.instance.stored) - self.assertTrue(TestFileFormatProcessor.instance.add_comma) + self.assertEqual(ForTestFileFormatProcessor.instance.set, None) + self.assertEqual(ForTestFileFormatProcessor.instance.filename, "test.txt") + self.assertEqual(ForTestFileFormatProcessor.instance.format, "123") + self.assertTrue(ForTestFileFormatProcessor.instance.stored) + self.assertTrue(ForTestFileFormatProcessor.instance.add_comma) def test2(self): - TestFileFormatProcessor.register() + ForTestFileFormatProcessor.register() base.write_set_to_file(None, "test.txt", format="123", add_comma=False) - self.assertFalse(TestFileFormatProcessor.instance.add_comma) + self.assertFalse(ForTestFileFormatProcessor.instance.add_comma) def test3(self): - TestFileFormatProcessor.register() + ForTestFileFormatProcessor.register() documentation = base.write_set_to_file.__doc__ self.assertTrue("**123**,\n Save files in a test format" in documentation) diff --git a/src/amuse/test/suite/core_tests/test_textio.py b/src/amuse/test/suite/core_tests/test_textio.py index 25c34cc25c..8a5fbb22f6 100644 --- a/src/amuse/test/suite/core_tests/test_textio.py +++ b/src/amuse/test/suite/core_tests/test_textio.py @@ -3,6 +3,7 @@ import textwrap import os import numpy +import tempfile from amuse import io from amuse.io import text @@ -72,13 +73,13 @@ def test3(self): x = datamodel.Particles(2) x.mass = [1.0, 2.0] | units.MSun x.radius = [3.0, 4.0] | units.RSun - io.write_set_to_file(x, "test.csv", "txt", attribute_types=(units.MSun, units.RSun)) - with open("test.csv", "r") as f: - contents = f.read() + with tempfile.NamedTemporaryFile() as tmp: + filename = tmp.name + io.write_set_to_file(x, filename, "txt", attribute_types=(units.MSun, units.RSun)) + with open(filename, "r") as f: + contents = f.read() self.assertEqual("#mass radius\n#MSun RSun\n1.0 3.0\n2.0 4.0\n", contents) - os.remove("test.csv") - def test4(self): mass = [1.0, 2.0, 3.0] | generic_unit_system.mass length = [3.0, 4.0, 5.0] | generic_unit_system.length @@ -101,30 +102,31 @@ def test5(self): "#mass radius\n#MSun RSun\n1.0 3.0 {0}\n2.0 4.0 {1}\n".format(x[0].key, x[1].key), ] for column_index, expected_content in enumerate(expected): - io.write_set_to_file( - x, - "test.csv", - "txt", - key_in_column=column_index, - attribute_types=(units.MSun, units.RSun) - ) - - with open("test.csv", "r") as f: - contents = f.read() - - self.assertEqual(expected_content, contents) - - y = io.read_set_from_file( - "test.csv", - "txt", - key_in_column=column_index, - attribute_types=(units.MSun, units.RSun), - attribute_names=('mass', 'radius') - ) + with tempfile.NamedTemporaryFile() as tmp: + filename = tmp.name + io.write_set_to_file( + x, + filename, + "txt", + key_in_column=column_index, + attribute_types=(units.MSun, units.RSun) + ) + + with open(filename, "r") as f: + contents = f.read() + + self.assertEqual(expected_content, contents) + + y = io.read_set_from_file( + filename, + "txt", + key_in_column=column_index, + attribute_types=(units.MSun, units.RSun), + attribute_names=('mass', 'radius') + ) self.assertEqual(y[0], x[0]) self.assertEqual(y[1], x[1]) - os.remove("test.csv") def test6(self): p = datamodel.Particles(2) @@ -154,47 +156,49 @@ def test7(self): self.assertEqual("#a b c\n#- m m\n1.0 2.0 3.0\n4.0 5.0 6.0\n", contents) def test8(self): - table = io.ReportTable( - "test.csv", - "txt", - attribute_types=(units.MSun, units.RSun) - ) - table.add_row(1.0 | units.MSun, 3.0 | units.RSun) - table.add_row(2.0 | units.MSun, 4.0 | units.RSun) - table.close() + with tempfile.NamedTemporaryFile() as tmp: + filename = tmp.name + table = io.ReportTable( + filename, + "txt", + attribute_types=(units.MSun, units.RSun) + ) + table.add_row(1.0 | units.MSun, 3.0 | units.RSun) + table.add_row(2.0 | units.MSun, 4.0 | units.RSun) + table.close() - with open("test.csv", "r") as f: - contents = f.read() + with open(filename, "r") as f: + contents = f.read() self.assertEqual("#MSun RSun\n1.0 3.0\n2.0 4.0\n", contents) - os.remove("test.csv") - def test9(self): p = datamodel.Particles(5) p.a = [1.0, 2.0, 3.0, 4.0, 5.0] p.b = [10, 11, 12, 13, 14] | units.m p.c = [20, 21, 22, 23, 24] | units.m - io.write_set_to_file( - p, - "test.csv", - "txt", - attribute_names=('a', 'b', 'c'), - attribute_types=(None, units.m, units.m), - maximum_number_of_lines_buffered=1, - ) - with open("test.csv", "r") as f: - contents = f.read() - - expected_contents = '#a b c\n#- m m\n1.0 10.0 20.0\n2.0 11.0 21.0\n3.0 12.0 22.0\n4.0 13.0 23.0\n5.0 14.0 24.0\n' - self.assertEqual(expected_contents, contents) - p2 = io.read_set_from_file( - "test.csv", - "txt", - attribute_names=('a', 'b', 'c'), - attribute_types=(None, units.m, units.m), - maximum_number_of_lines_buffered=1, - ) + with tempfile.NamedTemporaryFile() as tmp: + filename = tmp.name + io.write_set_to_file( + p, + filename, + "txt", + attribute_names=('a', 'b', 'c'), + attribute_types=(None, units.m, units.m), + maximum_number_of_lines_buffered=1, + ) + with open(filename, "r") as f: + contents = f.read() + + expected_contents = '#a b c\n#- m m\n1.0 10.0 20.0\n2.0 11.0 21.0\n3.0 12.0 22.0\n4.0 13.0 23.0\n5.0 14.0 24.0\n' + self.assertEqual(expected_contents, contents) + p2 = io.read_set_from_file( + filename, + "txt", + attribute_names=('a', 'b', 'c'), + attribute_types=(None, units.m, units.m), + maximum_number_of_lines_buffered=1, + ) self.assertAlmostRelativeEquals(p2.a, p.a) self.assertAlmostRelativeEquals(p2.b, p.b) self.assertAlmostRelativeEquals(p2.c, p.c) @@ -204,27 +208,29 @@ def test10(self): p.a = [1.0, 2.0, 3.0, 4.0, 5.0] p.b = [10, 11, 12, 13, 14] | units.m p.c = [20, 21, 22, 23, 24] | units.m - io.write_set_to_file( - p, - "test.csv", - "txt", - attribute_names=('a', 'b', 'c'), - attribute_types=(None, units.m, units.m), - maximum_number_of_lines_buffered=1, - key_in_column=0 - ) - with open("test.csv", "r") as f: - contents = f.read() - expected_contents = '#a b c\n#- m m\n30 1.0 10.0 20.0\n31 2.0 11.0 21.0\n32 3.0 12.0 22.0\n33 4.0 13.0 23.0\n34 5.0 14.0 24.0\n' - self.assertEqual(expected_contents, contents) - p2 = io.read_set_from_file( - "test.csv", - "txt", - attribute_names=('a', 'b', 'c'), - attribute_types=(None, units.m, units.m), - maximum_number_of_lines_buffered=1, - key_in_column=0 - ) + with tempfile.NamedTemporaryFile() as tmp: + filename = tmp.name + io.write_set_to_file( + p, + filename, + "txt", + attribute_names=('a', 'b', 'c'), + attribute_types=(None, units.m, units.m), + maximum_number_of_lines_buffered=1, + key_in_column=0 + ) + with open(filename, "r") as f: + contents = f.read() + expected_contents = '#a b c\n#- m m\n30 1.0 10.0 20.0\n31 2.0 11.0 21.0\n32 3.0 12.0 22.0\n33 4.0 13.0 23.0\n34 5.0 14.0 24.0\n' + self.assertEqual(expected_contents, contents) + p2 = io.read_set_from_file( + filename, + "txt", + attribute_names=('a', 'b', 'c'), + attribute_types=(None, units.m, units.m), + maximum_number_of_lines_buffered=1, + key_in_column=0 + ) self.assertEqual(p2.key, p.key) self.assertAlmostRelativeEquals(p2.a, p.a) self.assertAlmostRelativeEquals(p2.b, p.b) @@ -234,23 +240,25 @@ def test11(self): p = datamodel.Particles(200) p.a = 2 | units.m - io.write_set_to_file( - p, - "test.csv", - "txt", - attribute_names=('a'), - attribute_types=(units.m,), - maximum_number_of_lines_buffered=10, - key_in_column=0 - ) - p2 = io.read_set_from_file( - "test.csv", - "txt", - attribute_names=('a'), - attribute_types=(units.m,), - maximum_number_of_lines_buffered=10, - key_in_column=0 - ) + with tempfile.NamedTemporaryFile() as tmp: + filename = tmp.name + io.write_set_to_file( + p, + filename, + "txt", + attribute_names=('a'), + attribute_types=(units.m,), + maximum_number_of_lines_buffered=10, + key_in_column=0 + ) + p2 = io.read_set_from_file( + filename, + "txt", + attribute_names=('a'), + attribute_types=(units.m,), + maximum_number_of_lines_buffered=10, + key_in_column=0 + ) self.assertEqual(p2.key, p.key) self.assertAlmostRelativeEquals(p2.a, p.a) @@ -260,30 +268,32 @@ def test12(self): daltons.name = ["Joe", "William", "Jack", "Averell"] daltons.length = [1.1, 1.4, 1.7, 2.0] | core.unit_with_specific_dtype(units.m, "float32") daltons.age = [21, 20, 19, 18] | core.unit_with_specific_dtype(units.yr, "int32") - path = os.path.abspath(os.path.join(self.get_path_to_results(), "daltons.txt")) - io.write_set_to_file( - daltons, - path, - "txt", - attribute_names=('name', 'length', 'age'), - attribute_types=(None, units.m, units.yr), - maximum_number_of_lines_buffered=2, - key_in_column=0 - ) - with open(path, "r") as f: - contents = f.read() - expected_contents = '#name length age\n#- m yr\n30 Joe 1.1 21\n31 William 1.4 20\n32 Jack 1.7 19\n33 Averell 2.0 18\n' - self.assertEqual(expected_contents, contents) - - read = io.read_set_from_file( - path, - "txt", - attribute_names=('name', 'length', 'age'), - attribute_types=(None, units.m, units.yr), - attribute_dtypes=("str", "float32", "int32"), - maximum_number_of_lines_buffered=2, - key_in_column=0 - ) + with tempfile.NamedTemporaryFile() as tmp: + filename = tmp.name + path = os.path.abspath(os.path.join(self.get_path_to_results(), filename)) + io.write_set_to_file( + daltons, + path, + "txt", + attribute_names=('name', 'length', 'age'), + attribute_types=(None, units.m, units.yr), + maximum_number_of_lines_buffered=2, + key_in_column=0 + ) + with open(path, "r") as f: + contents = f.read() + expected_contents = '#name length age\n#- m yr\n30 Joe 1.1 21\n31 William 1.4 20\n32 Jack 1.7 19\n33 Averell 2.0 18\n' + self.assertEqual(expected_contents, contents) + + read = io.read_set_from_file( + path, + "txt", + attribute_names=('name', 'length', 'age'), + attribute_types=(None, units.m, units.yr), + attribute_dtypes=("str", "float32", "int32"), + maximum_number_of_lines_buffered=2, + key_in_column=0 + ) self.assertEqual(read.key, daltons.key) self.assertEqual(read.name, daltons.name) self.assertEqual(read.length, daltons.length) @@ -296,22 +306,24 @@ def test13(self): p = datamodel.Particles(100) p.a = numpy.arange(0, 1, 0.01) | units.m - path = os.path.abspath(os.path.join(self.get_path_to_results(), "test.csv")) + with tempfile.NamedTemporaryFile() as tmp: + filename = tmp.name + path = os.path.abspath(os.path.join(self.get_path_to_results(), filename)) - io.write_set_to_file( - p, - path, - "amuse-txt", - attribute_names=('a'), - maximum_number_of_lines_buffered=10, - key_in_column=0 - ) - p2 = io.read_set_from_file( - path, - "txt", - maximum_number_of_lines_buffered=10, - key_in_column=0 - ) + io.write_set_to_file( + p, + path, + "amuse-txt", + attribute_names=('a'), + maximum_number_of_lines_buffered=10, + key_in_column=0 + ) + p2 = io.read_set_from_file( + path, + "txt", + maximum_number_of_lines_buffered=10, + key_in_column=0 + ) self.assertEqual(p2.key, p.key) self.assertAlmostRelativeEquals(p2.a, p.a) @@ -320,19 +332,21 @@ def test14(self): p.a = numpy.arange(0, 1, 0.01) | units.m p.b = numpy.arange(0, 1, 0.01) - io.write_set_to_file( - p, - "test.csv", - "amuse-txt", - maximum_number_of_lines_buffered=10, - key_in_column=0 - ) - p2 = io.read_set_from_file( - "test.csv", - "txt", - maximum_number_of_lines_buffered=10, - key_in_column=0 - ) + with tempfile.NamedTemporaryFile() as tmp: + filename = tmp.name + io.write_set_to_file( + p, + filename, + "amuse-txt", + maximum_number_of_lines_buffered=10, + key_in_column=0 + ) + p2 = io.read_set_from_file( + filename, + "txt", + maximum_number_of_lines_buffered=10, + key_in_column=0 + ) self.assertEqual(p2.key, p.key) self.assertAlmostRelativeEquals(p2.a, p.a) self.assertAlmostRelativeEquals(p2.b, p.b) From f9b21bf7352565703875165e0759761a965467e4 Mon Sep 17 00:00:00 2001 From: Steven Rieder Date: Thu, 6 Jul 2023 19:12:01 +0200 Subject: [PATCH 2/2] fix divide by zero warning (#968) --- src/amuse/ic/brokenimf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/amuse/ic/brokenimf.py b/src/amuse/ic/brokenimf.py index fc19b34f8c..9966f03ac8 100644 --- a/src/amuse/ic/brokenimf.py +++ b/src/amuse/ic/brokenimf.py @@ -117,7 +117,7 @@ def calculate_fraction_per_bin(self): result.append(factor) total = sum(result, 0.0) - return numpy.array(result)/total + return numpy.array(result)/total if bool(total) else numpy.array(result) def mass_mean(self): result = 0 | units.MSun