|
| 1 | +"""Tests for DataFrame API for period data.""" |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import pandas as pd |
| 5 | +import pytest |
| 6 | + |
| 7 | +from flopy4.mf6.constants import FILL_DNODATA |
| 8 | +from flopy4.mf6.gwf import Chd, Drn, Gwf, Wel |
| 9 | +from flopy4.mf6.utils.grid import StructuredGrid |
| 10 | +from flopy4.mf6.utils.time import Time |
| 11 | + |
| 12 | + |
| 13 | +def test_chd_to_dataframe(): |
| 14 | + """Test converting CHD package to DataFrame.""" |
| 15 | + time = Time(perlen=[1.0], nstp=[1]) |
| 16 | + grid = StructuredGrid(nlay=1, nrow=10, ncol=10) |
| 17 | + dims = { |
| 18 | + "nlay": grid.nlay, |
| 19 | + "nrow": grid.nrow, |
| 20 | + "ncol": grid.ncol, |
| 21 | + "nper": time.nper, |
| 22 | + "nodes": grid.nnodes, |
| 23 | + } |
| 24 | + |
| 25 | + chd = Chd(dims=dims, head={0: {(0, 0, 0): 1.0, (0, 9, 9): 0.0}}) |
| 26 | + df = chd.to_dataframe("head") |
| 27 | + |
| 28 | + assert isinstance(df, pd.DataFrame) |
| 29 | + assert len(df) == 2 |
| 30 | + assert list(df.columns) == ["per", "layer", "row", "col", "head"] |
| 31 | + |
| 32 | + # Check first record |
| 33 | + assert df.iloc[0]["per"] == 0 |
| 34 | + assert df.iloc[0]["layer"] == 0 |
| 35 | + assert df.iloc[0]["row"] == 0 |
| 36 | + assert df.iloc[0]["col"] == 0 |
| 37 | + assert df.iloc[0]["head"] == 1.0 |
| 38 | + |
| 39 | + # Check second record |
| 40 | + assert df.iloc[1]["per"] == 0 |
| 41 | + assert df.iloc[1]["layer"] == 0 |
| 42 | + assert df.iloc[1]["row"] == 9 |
| 43 | + assert df.iloc[1]["col"] == 9 |
| 44 | + assert df.iloc[1]["head"] == 0.0 |
| 45 | + |
| 46 | + |
| 47 | +def test_chd_from_dataframe(): |
| 48 | + """Test creating CHD package from DataFrame.""" |
| 49 | + df = pd.DataFrame( |
| 50 | + { |
| 51 | + "per": [0, 0], |
| 52 | + "layer": [0, 0], |
| 53 | + "row": [0, 9], |
| 54 | + "col": [0, 9], |
| 55 | + "head": [1.0, 0.0], |
| 56 | + } |
| 57 | + ) |
| 58 | + |
| 59 | + dims = {"nper": 1, "nlay": 1, "nrow": 10, "ncol": 10, "nodes": 100} |
| 60 | + chd = Chd.from_dataframe(df, "head", dims=dims) |
| 61 | + |
| 62 | + assert chd.head is not None |
| 63 | + assert chd.head.shape == (1, 100) |
| 64 | + |
| 65 | + # Check that the correct cells have values |
| 66 | + assert chd.head[0, 0] == 1.0 # (0, 0, 0) -> node 0 |
| 67 | + assert chd.head[0, 99] == 0.0 # (0, 9, 9) -> node 99 |
| 68 | + |
| 69 | + |
| 70 | +def test_chd_roundtrip(): |
| 71 | + """Test round-trip conversion: dict -> to_dataframe -> from_dataframe.""" |
| 72 | + dims = {"nper": 1, "nlay": 1, "nrow": 10, "ncol": 10, "nodes": 100} |
| 73 | + |
| 74 | + # Create original package |
| 75 | + chd1 = Chd(dims=dims, head={0: {(0, 0, 0): 1.0, (0, 9, 9): 0.0}}) |
| 76 | + |
| 77 | + # Convert to DataFrame |
| 78 | + df = chd1.to_dataframe("head") |
| 79 | + |
| 80 | + # Create new package from DataFrame |
| 81 | + chd2 = Chd.from_dataframe(df, "head", dims=dims) |
| 82 | + |
| 83 | + # Compare |
| 84 | + assert np.allclose(chd1.head, chd2.head, equal_nan=True) |
| 85 | + |
| 86 | + |
| 87 | +def test_wel_to_dataframe(): |
| 88 | + """Test converting WEL package to DataFrame.""" |
| 89 | + dims = {"nper": 1, "nlay": 1, "nrow": 10, "ncol": 10, "nodes": 100} |
| 90 | + |
| 91 | + wel = Wel( |
| 92 | + dims=dims, |
| 93 | + q={0: {(0, 5, 5): -100.0, (0, 8, 8): 50.0}}, |
| 94 | + ) |
| 95 | + df = wel.to_dataframe("q") |
| 96 | + |
| 97 | + assert isinstance(df, pd.DataFrame) |
| 98 | + assert len(df) == 2 |
| 99 | + assert list(df.columns) == ["per", "layer", "row", "col", "q"] |
| 100 | + |
| 101 | + # Check records |
| 102 | + assert df.iloc[0]["q"] == -100.0 |
| 103 | + assert df.iloc[1]["q"] == 50.0 |
| 104 | + |
| 105 | + |
| 106 | +def test_wel_from_dataframe(): |
| 107 | + """Test creating WEL package from DataFrame.""" |
| 108 | + df = pd.DataFrame( |
| 109 | + { |
| 110 | + "per": [0, 0], |
| 111 | + "layer": [0, 0], |
| 112 | + "row": [5, 8], |
| 113 | + "col": [5, 8], |
| 114 | + "q": [-100.0, 50.0], |
| 115 | + } |
| 116 | + ) |
| 117 | + |
| 118 | + dims = {"nper": 1, "nlay": 1, "nrow": 10, "ncol": 10, "nodes": 100} |
| 119 | + wel = Wel.from_dataframe(df, "q", dims=dims) |
| 120 | + |
| 121 | + assert wel.q is not None |
| 122 | + assert wel.q.shape == (1, 100) |
| 123 | + |
| 124 | + # Node for (0, 5, 5) = 5*10 + 5 = 55 |
| 125 | + assert wel.q[0, 55] == -100.0 |
| 126 | + # Node for (0, 8, 8) = 8*10 + 8 = 88 |
| 127 | + assert wel.q[0, 88] == 50.0 |
| 128 | + |
| 129 | + |
| 130 | +def test_drn_to_dataframe(): |
| 131 | + """Test converting DRN package to DataFrame (multi-field).""" |
| 132 | + dims = {"nper": 1, "nlay": 1, "nrow": 10, "ncol": 10, "nodes": 100} |
| 133 | + |
| 134 | + drn = Drn( |
| 135 | + dims=dims, |
| 136 | + elev={0: {(0, 7, 5): 10.0}}, |
| 137 | + cond={0: {(0, 7, 5): 1.0}}, |
| 138 | + ) |
| 139 | + |
| 140 | + # Test elevation field |
| 141 | + df_elev = drn.to_dataframe("elev") |
| 142 | + assert len(df_elev) == 1 |
| 143 | + assert df_elev.iloc[0]["elev"] == 10.0 |
| 144 | + |
| 145 | + # Test conductance field |
| 146 | + df_cond = drn.to_dataframe("cond") |
| 147 | + assert len(df_cond) == 1 |
| 148 | + assert df_cond.iloc[0]["cond"] == 1.0 |
| 149 | + |
| 150 | + |
| 151 | +def test_multi_period_dataframe(): |
| 152 | + """Test DataFrame conversion with multiple stress periods.""" |
| 153 | + dims = {"nper": 3, "nlay": 1, "nrow": 10, "ncol": 10, "nodes": 100} |
| 154 | + |
| 155 | + chd = Chd( |
| 156 | + dims=dims, |
| 157 | + head={ |
| 158 | + 0: {(0, 0, 0): 1.0}, |
| 159 | + 1: {(0, 0, 0): 0.9}, |
| 160 | + 2: {(0, 0, 0): 0.8}, |
| 161 | + }, |
| 162 | + ) |
| 163 | + |
| 164 | + df = chd.to_dataframe("head") |
| 165 | + |
| 166 | + assert len(df) == 3 |
| 167 | + assert df[df["per"] == 0].iloc[0]["head"] == 1.0 |
| 168 | + assert df[df["per"] == 1].iloc[0]["head"] == 0.9 |
| 169 | + assert df[df["per"] == 2].iloc[0]["head"] == 0.8 |
| 170 | + |
| 171 | + |
| 172 | +def test_dataframe_with_multiple_cells(): |
| 173 | + """Test DataFrame conversion with multiple cells per period.""" |
| 174 | + df = pd.DataFrame( |
| 175 | + { |
| 176 | + "per": [0, 0, 0, 1, 1, 1], |
| 177 | + "layer": [0, 0, 0, 0, 0, 0], |
| 178 | + "row": [0, 5, 9, 0, 5, 9], |
| 179 | + "col": [0, 5, 9, 0, 5, 9], |
| 180 | + "head": [1.0, 0.5, 0.0, 0.9, 0.45, 0.0], |
| 181 | + } |
| 182 | + ) |
| 183 | + |
| 184 | + dims = {"nper": 2, "nlay": 1, "nrow": 10, "ncol": 10, "nodes": 100} |
| 185 | + chd = Chd.from_dataframe(df, "head", dims=dims) |
| 186 | + |
| 187 | + # Verify period 0 |
| 188 | + assert chd.head[0, 0] == 1.0 |
| 189 | + assert chd.head[0, 55] == 0.5 |
| 190 | + assert chd.head[0, 99] == 0.0 |
| 191 | + |
| 192 | + # Verify period 1 |
| 193 | + assert chd.head[1, 0] == 0.9 |
| 194 | + assert chd.head[1, 55] == 0.45 |
| 195 | + assert chd.head[1, 99] == 0.0 |
| 196 | + |
| 197 | + |
| 198 | +def test_to_dataframe_auto_detect_field(): |
| 199 | + """Test automatic field detection in to_dataframe.""" |
| 200 | + dims = {"nper": 1, "nlay": 1, "nrow": 10, "ncol": 10, "nodes": 100} |
| 201 | + |
| 202 | + chd = Chd(dims=dims, head={0: {(0, 0, 0): 1.0}}) |
| 203 | + |
| 204 | + # Should auto-detect 'head' field |
| 205 | + df = chd.to_dataframe() |
| 206 | + assert "head" in df.columns |
| 207 | + assert len(df) == 1 |
| 208 | + |
| 209 | + |
| 210 | +def test_empty_dataframe(): |
| 211 | + """Test converting empty package to DataFrame.""" |
| 212 | + dims = {"nper": 1, "nlay": 1, "nrow": 10, "ncol": 10, "nodes": 100} |
| 213 | + |
| 214 | + chd = Chd(dims=dims) # No head data |
| 215 | + df = chd.to_dataframe("head") |
| 216 | + |
| 217 | + assert isinstance(df, pd.DataFrame) |
| 218 | + assert len(df) == 0 |
| 219 | + |
| 220 | + |
| 221 | +def test_from_dataframe_with_kwargs(): |
| 222 | + """Test from_dataframe with additional package parameters.""" |
| 223 | + df = pd.DataFrame( |
| 224 | + { |
| 225 | + "per": [0, 0], |
| 226 | + "layer": [0, 0], |
| 227 | + "row": [0, 9], |
| 228 | + "col": [0, 9], |
| 229 | + "head": [1.0, 0.0], |
| 230 | + } |
| 231 | + ) |
| 232 | + |
| 233 | + dims = {"nper": 1, "nlay": 1, "nrow": 10, "ncol": 10, "nodes": 100} |
| 234 | + chd = Chd.from_dataframe( |
| 235 | + df, "head", dims=dims, print_input=True, print_flows=True |
| 236 | + ) |
| 237 | + |
| 238 | + assert chd.print_input is True |
| 239 | + assert chd.print_flows is True |
| 240 | + assert chd.head is not None |
0 commit comments