Skip to content

Commit

Permalink
Auto-format code using Clang-Format (#276)
Browse files Browse the repository at this point in the history
Co-authored-by: GitHub Actions <[email protected]>
  • Loading branch information
github-actions[bot] and actions-user authored Nov 12, 2024
1 parent 825c560 commit 3e3f542
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/acom_music_box/data_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

logger = logging.getLogger(__name__)


class DataOutput:
"""
A class to handle data output operations for a DataFrame, including converting to CSV
Expand Down Expand Up @@ -41,7 +42,7 @@ class DataOutput:
... })
>>> args = Namespace(output='output.nc', output_format='netcdf')
>>> data_output = DataOutput(df, args)
>>> data_output.output()
>>> data_output.output()
"""

def __init__(self, df, args):
Expand Down Expand Up @@ -75,7 +76,7 @@ def __init__(self, df, args):
def _get_default_filename(self):
"""Generate a default filename based on the current datetime and output format."""
now = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
extension = 'csv' if self.args.output_format == 'csv' else 'nc'
extension = 'csv' if self.args.output_format == 'csv' else 'nc'
return f"music_box_{now}.{extension}"

def _ensure_output_path(self):
Expand Down Expand Up @@ -153,4 +154,3 @@ def output(self):
error = f"Unsupported output format: {self.args.output_format}"
logger.error(error)
raise ValueError(error)

1 change: 1 addition & 0 deletions src/acom_music_box/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ def on_add(sel):

plt.show()


def main():
start = datetime.datetime.now()

Expand Down
2 changes: 1 addition & 1 deletion src/acom_music_box/music_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def solve(self, callback=None):
the specified file.
Args:
callback (function, optional): A callback function that is called after each time step. Defaults to None.
callback (function, optional): A callback function that is called after each time step. Defaults to None.
The callback will take the most recent results, the current time, conditions, and the total simulation time as arguments.
Returns:
Expand Down
10 changes: 10 additions & 0 deletions tests/integration/test_executable_data_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,56 @@
import pytest
import tempfile


@pytest.fixture
def temp_dir():
with tempfile.TemporaryDirectory() as tmpdirname:
yield tmpdirname


def test_print_results_to_terminal(temp_dir):
result = subprocess.run(['music_box', '-e', 'Analytical'], capture_output=True, text=True, cwd=temp_dir)
assert len(result.stdout) > 0


def test_create_netcdf_with_timestamp(temp_dir):
subprocess.run(['music_box', '-e', 'Analytical', '--output-format', 'netcdf'], cwd=temp_dir)
assert glob.glob(os.path.join(temp_dir, "music_box_*.nc"))


def test_create_csv_with_timestamp(temp_dir):
subprocess.run(['music_box', '-e', 'Analytical', '--output-format', 'csv'], cwd=temp_dir)
assert glob.glob(os.path.join(temp_dir, "music_box_*.csv"))


def test_create_named_csv(temp_dir):
subprocess.run(['music_box', '-e', 'Analytical', '--output-format', 'csv', '-o', 'out.csv'], cwd=temp_dir)
assert os.path.exists(os.path.join(temp_dir, "out.csv"))


def test_create_named_netcdf(temp_dir):
subprocess.run(['music_box', '-e', 'Analytical', '--output-format', 'netcdf', '-o', 'out.nc'], cwd=temp_dir)
assert os.path.exists(os.path.join(temp_dir, "out.nc"))


def test_create_directory_and_named_netcdf(temp_dir):
os.makedirs(os.path.join(temp_dir, "results"), exist_ok=True)
subprocess.run(['music_box', '-e', 'Analytical', '--output-format', 'netcdf', '-o', 'results/out.nc'], cwd=temp_dir)
assert os.path.exists(os.path.join(temp_dir, "results/out.nc"))


def test_create_directory_and_named_csv(temp_dir):
os.makedirs(os.path.join(temp_dir, "results"), exist_ok=True)
subprocess.run(['music_box', '-e', 'Analytical', '--output-format', 'csv', '-o', 'results/out.csv'], cwd=temp_dir)
assert os.path.exists(os.path.join(temp_dir, "results/out.csv"))


def test_create_directory_and_timestamped_csv(temp_dir):
os.makedirs(os.path.join(temp_dir, "results"), exist_ok=True)
subprocess.run(['music_box', '-e', 'Analytical', '--output-format', 'csv', '-o', 'results/'], cwd=temp_dir)
assert glob.glob(os.path.join(temp_dir, "results/music_box_*.csv"))


def test_create_directory_and_timestamped_netcdf(temp_dir):
os.makedirs(os.path.join(temp_dir, "results"), exist_ok=True)
subprocess.run(['music_box', '-e', 'Analytical', '--output-format', 'netcdf', '-o', 'results/'], cwd=temp_dir)
Expand Down
10 changes: 6 additions & 4 deletions tests/unit/test_data_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from argparse import Namespace
from acom_music_box import DataOutput


class TestDataOutput(unittest.TestCase):

def setUp(self):
Expand All @@ -19,7 +20,7 @@ def setUp(self):
self.temp_dir = tempfile.TemporaryDirectory()
self.csv_path = os.path.join(self.temp_dir.name, 'output.csv')
self.netcdf_path = os.path.join(self.temp_dir.name, 'output.nc')

def tearDown(self):
# Clean up temporary directory
self.temp_dir.cleanup()
Expand All @@ -42,7 +43,7 @@ def test_convert_to_netcdf(self):
data_output = DataOutput(self.df, args)
data_output._convert_to_netcdf()
self.assertTrue(os.path.exists(self.netcdf_path))

# Load the NetCDF file to check the attributes
ds = xr.open_dataset(self.netcdf_path)
self.assertEqual(ds['ENV.temperature'].attrs['units'], 'K')
Expand All @@ -56,7 +57,7 @@ def test_output_csv(self):
data_output = DataOutput(self.df, args)
data_output.output()
self.assertTrue(os.path.exists(self.csv_path))

# Check the contents of the CSV file
output_df = pd.read_csv(self.csv_path)
expected_columns = ['ENV.temperature.K', 'ENV.pressure.Pa', 'ENV.number_density_air.kg -m3', 'time.s']
Expand All @@ -67,7 +68,7 @@ def test_output_netcdf(self):
data_output = DataOutput(self.df, args)
data_output.output()
self.assertTrue(os.path.exists(self.netcdf_path))

# Check the contents of the NetCDF file
ds = xr.open_dataset(self.netcdf_path)
self.assertEqual(ds['ENV.temperature'].attrs['units'], 'K')
Expand All @@ -76,5 +77,6 @@ def test_output_netcdf(self):
self.assertEqual(ds['time'].attrs['units'], 's')
ds.close()


if __name__ == '__main__':
unittest.main()

0 comments on commit 3e3f542

Please sign in to comment.