Skip to content

Commit

Permalink
Remove <1.0 syntax (#615)
Browse files Browse the repository at this point in the history
* Replace .write with .to_dfs

* Similar for Dfsu 3d

* Dfs1

* Dfs0

* Ruffed
  • Loading branch information
ecomodeller authored Nov 30, 2023
1 parent 0162841 commit cf089da
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 157 deletions.
135 changes: 39 additions & 96 deletions tests/test_dfs0.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import numpy as np
import pandas as pd
import datetime
import mikeio
from mikeio import Dfs0, EUMType, EUMUnit, ItemInfo

Expand All @@ -27,7 +26,6 @@ def test_repr_equidistant():


def test_write_float(tmp_path):

fp = tmp_path / "simple_float.dfs0"

nt = 100
Expand All @@ -43,7 +41,6 @@ def test_write_float(tmp_path):


def test_write_double(tmp_path):

fp = tmp_path / "simple_float.dfs0"

nt = 100
Expand All @@ -59,7 +56,6 @@ def test_write_double(tmp_path):


def test_write_int_not_possible(tmp_path):

fp = tmp_path / "simple_float.dfs0"

nt = 100
Expand All @@ -74,7 +70,6 @@ def test_write_int_not_possible(tmp_path):


def test_read_units_write_new(tmp_path):

fp = tmp_path / "random.dfs0"

ds = mikeio.read("tests/testdata/random.dfs0")
Expand All @@ -89,7 +84,6 @@ def test_read_units_write_new(tmp_path):


def test_read_start_end_time():

dfs0file = r"tests/testdata/random.dfs0"

dfs = Dfs0(dfs0file)
Expand All @@ -100,7 +94,6 @@ def test_read_start_end_time():


def test_read_all_time_steps_without_reading_items():

dfs0file = r"tests/testdata/random.dfs0"
dfs = mikeio.open(dfs0file)
assert isinstance(dfs.time, pd.DatetimeIndex)
Expand All @@ -124,29 +117,26 @@ def test_read_all_time_steps_without_reading_items_neq():

def test_write_non_equidistant_calendar(tmp_path):
dfs0file = tmp_path / "neq.dfs0"
data = [np.zeros(1000), np.ones(1000)]

start_time = datetime.datetime(2017, 1, 1)
time_vector = []
for i in range(1000):
time_vector.append(start_time + datetime.timedelta(hours=i * 0.1))
title = "Hello Test"
items = [ItemInfo("VarFun01", 100000, 1000, 0), ItemInfo("NotFun", 100000, 1000, 1)]

dfs = Dfs0()
dfs.write(
filename=dfs0file,
data=data,
datetimes=time_vector,
items=items,
title=title,
time = pd.DatetimeIndex(["2001-01-01", "2001-01-01 01:00", "2001-01-01 01:10"])
da1 = mikeio.DataArray(
data=np.zeros(3),
time=time,
item=ItemInfo("VarFun01", EUMType.Water_Level, unit=EUMUnit.meter),
)

da2 = mikeio.DataArray(
data=np.ones(3),
time=time,
item=ItemInfo("NotFun", EUMType.Rainfall_Depth, data_value_type="Accumulated"),
)
ds = mikeio.Dataset([da1, da2])
ds.to_dfs(dfs0file)
assert dfs0file.exists()

ds2 = mikeio.read(dfs0file)
assert not ds2.is_equidistant

def test_read_equidistant_dfs0_to_dataframe_fixed_freq():

def test_read_equidistant_dfs0_to_dataframe_fixed_freq():
dfs0file = "tests/testdata/random.dfs0"

dfs = Dfs0(dfs0file)
Expand All @@ -158,7 +148,6 @@ def test_read_equidistant_dfs0_to_dataframe_fixed_freq():


def test_read_equidistant_dfs0_to_dataframe_unit_in_name():

dfs0file = "tests/testdata/random.dfs0"

dfs = Dfs0(dfs0file)
Expand All @@ -168,7 +157,6 @@ def test_read_equidistant_dfs0_to_dataframe_unit_in_name():


def test_read_nonequidistant_dfs0_to_dataframe_no_freq():

dfs0file = "tests/testdata/da_diagnostic.dfs0"

dfs = Dfs0(dfs0file)
Expand All @@ -178,7 +166,6 @@ def test_read_nonequidistant_dfs0_to_dataframe_no_freq():


def test_read_dfs0_delete_value_conversion():

dfs0file = "tests/testdata/da_diagnostic.dfs0"

dfs = Dfs0(dfs0file)
Expand All @@ -195,7 +182,6 @@ def test_read_dfs0_delete_value_conversion():


def test_read_dfs0_small_value_not_delete_value(tmp_path):

filename = tmp_path / "small.dfs0"
d = np.array([0.0, 0.0000001, -0.0001])
assert np.isclose(d, -1e-35, atol=1e-33).any()
Expand All @@ -213,7 +199,6 @@ def test_read_dfs0_small_value_not_delete_value(tmp_path):


def test_write_from_data_frame(tmp_path):

df = pd.read_csv(
"tests/testdata/co2-mm-mlo.csv",
parse_dates=True,
Expand All @@ -235,7 +220,6 @@ def test_write_from_data_frame(tmp_path):


def test_write_from_data_frame_monkey_patched(tmp_path):

df = pd.read_csv(
"tests/testdata/co2-mm-mlo.csv",
parse_dates=True,
Expand All @@ -257,7 +241,6 @@ def test_write_from_data_frame_monkey_patched(tmp_path):


def test_write_dataframe_different_eum_types_to_dfs0(tmp_path):

time = pd.DatetimeIndex(["2001-01-01", "2001-01-01 01:00", "2001-01-01 01:10"])

df = pd.DataFrame(
Expand Down Expand Up @@ -287,7 +270,6 @@ def test_write_dataframe_different_eum_types_to_dfs0(tmp_path):


def test_write_from_pandas_series_monkey_patched(tmp_path):

df = pd.read_csv(
"tests/testdata/co2-mm-mlo.csv",
parse_dates=True,
Expand All @@ -313,7 +295,6 @@ def test_write_from_pandas_series_monkey_patched(tmp_path):


def test_write_from_data_frame_different_types(tmp_path):

df = pd.read_csv(
"tests/testdata/co2-mm-mlo.csv",
parse_dates=True,
Expand Down Expand Up @@ -343,7 +324,6 @@ def test_write_from_data_frame_different_types(tmp_path):


def test_read_dfs0_single_item():

dfs0file = r"tests/testdata/random.dfs0"

dfs = Dfs0(dfs0file)
Expand All @@ -353,7 +333,6 @@ def test_read_dfs0_single_item():


def test_read_dfs0_single_item_named_access():

dfs0file = r"tests/testdata/random.dfs0"

dfs = Dfs0(dfs0file)
Expand All @@ -363,7 +342,6 @@ def test_read_dfs0_single_item_named_access():


def test_read_dfs0_temporal_subset():

dfs0file = r"tests/testdata/random.dfs0"

dfs = Dfs0(dfs0file)
Expand All @@ -374,7 +352,6 @@ def test_read_dfs0_temporal_subset():


def test_read_non_eq_dfs0__temporal_subset():

dfs0file = r"tests/testdata/da_diagnostic.dfs0"

dfs = Dfs0(dfs0file)
Expand All @@ -384,7 +361,6 @@ def test_read_non_eq_dfs0__temporal_subset():


def test_read_dfs0_single_item_read_by_name():

dfs0file = r"tests/testdata/random.dfs0"

items = ["NotFun", "VarFun01"]
Expand All @@ -401,7 +377,6 @@ def test_read_dfs0_single_item_read_by_name():


def test_read_dfs0_to_dataframe():

dfs0file = r"tests/testdata/random.dfs0"

dfs = Dfs0(dfs0file)
Expand All @@ -423,8 +398,7 @@ def test_write_data_with_missing_values(tmp_path):
dfs0file = r"tests/testdata/random.dfs0"
tmpfile = tmp_path / "random.dfs0"

dfs = Dfs0(dfs0file)
ds = dfs.read()
ds = mikeio.read(dfs0file)

# Do something with the data
ds[0].values = np.zeros_like(ds[0].values)
Expand All @@ -434,78 +408,51 @@ def test_write_data_with_missing_values(tmp_path):
ds[1].values[0:10] = np.nan

# Overwrite the file
dfs.write(tmpfile, ds)
ds.to_dfs(tmpfile)

# Write operation does not modify the data
assert np.isnan(ds[1].values[1])

moddfs = Dfs0(tmpfile)
modified = moddfs.read()
modified = mikeio.read(tmpfile)
assert np.isnan(modified[1].values[5])


def test_read_relative_time_axis():
filename = "tests/testdata/eq_relative.dfs0"

filename = r"tests/testdata/eq_relative.dfs0"

dfs0 = Dfs0(filename)

ds = dfs0.read()
ds = mikeio.read(filename)
assert len(ds) == 5


def test_write_accumulated_datatype(tmp_path):
filename = tmp_path / "simple.dfs0"

data = []
d = np.random.random(100)
data.append(d)

dfs = Dfs0()

dfs.write(
filename=filename,
data=data,
start_time=datetime.datetime(2012, 1, 1),
items=[
ItemInfo(
"testing water level",
EUMType.Water_Level,
EUMUnit.meter,
data_value_type="MeanStepBackward",
)
],
title="test dfs0",
da = mikeio.DataArray(
data=np.random.random(100),
time=pd.date_range("2000", periods=100, freq="H"),
item=ItemInfo(
"testing water level",
EUMType.Water_Level,
EUMUnit.meter,
data_value_type="MeanStepBackward",
),
)

newdfs = Dfs0(filename)
assert newdfs.items[0].data_value_type == 3
da.to_dfs(filename)
newds = mikeio.read(filename)
assert newds[0].item.data_value_type == 3


def test_write_default_datatype(tmp_path):
filename = tmp_path / "simple.dfs0"

data = []
d = np.random.random(100)
data.append(d)

dfs = Dfs0()

dfs.write(
filename=filename,
data=data,
start_time=datetime.datetime(2012, 1, 1),
dt=12,
items=[ItemInfo("testing water level", EUMType.Water_Level, EUMUnit.meter)],
title="test dfs0",
)

newdfs = Dfs0(filename)
assert newdfs.items[0].data_value_type == 0
da = mikeio.DataArray(data=np.random.random(100), time=pd.date_range("2000", periods=100, freq="H"))
da.to_dfs(filename)
newds = mikeio.read(filename)
assert newds[0].item.data_value_type == 0


def test_write_from_pandas_series_monkey_patched_data_value_not_default(tmp_path):

df = pd.read_csv(
"tests/testdata/co2-mm-mlo.csv",
parse_dates=True,
Expand Down Expand Up @@ -540,7 +487,6 @@ def test_write_from_pandas_series_monkey_patched_data_value_not_default(tmp_path


def test_write_from_data_frame_monkey_patched_data_value_not_default(tmp_path):

df = pd.read_csv(
"tests/testdata/co2-mm-mlo.csv",
parse_dates=True,
Expand Down Expand Up @@ -574,7 +520,6 @@ def test_write_from_data_frame_monkey_patched_data_value_not_default(tmp_path):


def test_read_write_eum(tmp_path):

ds = mikeio.read("tests/testdata/waterlevel_viken.dfs0")
assert ds["ST 2: WL (m)"].type == EUMType.Water_Level
assert ds["ST 2: WL (m)"].unit == EUMUnit.meter
Expand All @@ -589,7 +534,6 @@ def test_read_write_eum(tmp_path):


def test_read_write_single_step(tmp_path):

ds = mikeio.read("tests/testdata/waterlevel_viken.dfs0", time=-1)
outfilename = tmp_path / "last_step.dfs0"
ds.to_dfs(outfilename)
Expand All @@ -608,13 +552,12 @@ def test_read_write_single_step_to_dataframe(tmp_path):


def test_read_dfs0_with_many_items():

ds = mikeio.read("tests/testdata/many_items.dfs0")

assert ds.n_items == 800

def test_read_dfs0_with_non_unique_item_names():

def test_read_dfs0_with_non_unique_item_names():
with pytest.warns(match="item name"):
ds = mikeio.read("tests/testdata/untitled_3_items.dfs0")

Expand All @@ -625,11 +568,11 @@ def test_read_dfs0_with_non_unique_item_names():
assert ds.Untitled_3.values[0] == pytest.approx(0.0)
assert np.isnan(ds.Untitled_3.values[1])


def test_non_equidistant_time_can_read_correctly_with_open(tmp_path):

dfs = mikeio.open("tests/testdata/neq_daily_time_unit.dfs0")
dfs.time
ds = dfs.read()

assert all(dfs.time == ds.time)
assert all(dfs.time == ds.time)
2 changes: 1 addition & 1 deletion tests/test_dfs1.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def test_write_some_time_steps_new_file(tmp_path):
data = ds[0].to_numpy()
assert data.shape == (6, 3) # time, x

dfs.write(fp, ds)
ds.to_dfs(fp)

dfsnew = mikeio.open(fp)

Expand Down
Loading

0 comments on commit cf089da

Please sign in to comment.