Skip to content

Commit 3fda260

Browse files
committed
Merge branch 'main' of https://github.com/NCAR/music-box into TAMU_LambdaRate
2 parents 4953d83 + 602a79d commit 3fda260

2 files changed

Lines changed: 59 additions & 0 deletions

File tree

python/acom_music_box/conditions_manager.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,22 @@ def _load_from_conditions_section(self, path_to_json: str, conditions_config: di
534534
for data_block in conditions_config["data"]:
535535
self._load_inline_data(data_block)
536536

537+
def _ensure_numeric_float64(self, df: pd.DataFrame) -> pd.DataFrame:
538+
"""
539+
Convert all numeric columns to float64 to handle integer values (e.g., 0).
540+
Uses vectorized operation for performance with wide/large DataFrames.
541+
542+
Args:
543+
df: DataFrame to convert.
544+
545+
Returns:
546+
DataFrame with numeric columns as float64.
547+
"""
548+
numeric_cols = df.select_dtypes(include=['number']).columns
549+
if len(numeric_cols) > 0:
550+
df[numeric_cols] = df[numeric_cols].astype(np.float64)
551+
return df
552+
537553
def _read_csv(self, file_path: str):
538554
"""
539555
Load conditions from a CSV file.
@@ -542,6 +558,7 @@ def _read_csv(self, file_path: str):
542558
file_path: Path to the CSV file.
543559
"""
544560
df = pd.read_csv(file_path, skipinitialspace=True)
561+
df = self._ensure_numeric_float64(df)
545562
self.add_from_dataframe(df)
546563

547564
def _load_inline_data(self, data_block: dict):
@@ -561,6 +578,7 @@ def _load_inline_data(self, data_block: dict):
561578
rows = data_block["rows"]
562579

563580
df = pd.DataFrame(rows, columns=headers)
581+
df = self._ensure_numeric_float64(df)
564582
self.add_from_dataframe(df)
565583

566584
def get_conditions_at_time(self, time: float) -> dict:

python/tests/unit/test_conditions_manager.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,47 @@ def test_surf_rate_parameter_format(self):
313313
assert "SURF.surface.particle number concentration.# m-3" in conds["rate_parameters"]
314314
assert conds["rate_parameters"]["SURF.surface.particle number concentration.# m-3"] == 1e12
315315

316+
def test_integer_values_converted_to_float64(self):
317+
"""Test that integer values (like 0) are converted to np.float64."""
318+
import numpy as np
319+
320+
manager = ConditionsManager()
321+
322+
# Test with inline data containing integer 0
323+
data_block = {
324+
"headers": ["time.s", "ENV.temperature.K", "PHOTO.rate1.s-1"],
325+
"rows": [[0, 300.0, 0]] # Integer 0 values
326+
}
327+
manager._load_inline_data(data_block)
328+
329+
# Check that values are stored as float64
330+
df = manager._df
331+
assert df["time.s"].dtype == np.float64
332+
assert df["ENV.temperature.K"].dtype == np.float64
333+
assert df["PHOTO.rate1.s-1"].dtype == np.float64
334+
335+
# Test with CSV-like data
336+
with tempfile.TemporaryDirectory() as tmpdir:
337+
csv_content = """time.s,ENV.temperature.K,PHOTO.rate2.s-1
338+
0,298.15,0
339+
3600,310.0,1e-5
340+
"""
341+
csv_path = os.path.join(tmpdir, "test.csv")
342+
with open(csv_path, 'w') as f:
343+
f.write(csv_content)
344+
345+
manager2 = ConditionsManager()
346+
manager2._read_csv(csv_path)
347+
348+
df2 = manager2._df
349+
assert df2["time.s"].dtype == np.float64
350+
assert df2["ENV.temperature.K"].dtype == np.float64
351+
assert df2["PHOTO.rate2.s-1"].dtype == np.float64
352+
353+
# Verify the values are correct
354+
assert df2.loc[0, "time.s"] == 0.0
355+
assert df2.loc[0, "PHOTO.rate2.s-1"] == 0.0
356+
316357

317358
if __name__ == "__main__":
318359
pytest.main([__file__, "-v"])

0 commit comments

Comments
 (0)