2222
2323from modelskill .model .point import PointModelResult
2424
25- from . import Quantity , __version__ , model_result
25+ from . import Quantity
2626from .comparison import Comparer , ComparerCollection
2727from .model .dfsu import DfsuModelResult
2828from .model .dummy import DummyModelResult
2929from .model .grid import GridModelResult
3030from .model .track import TrackModelResult
31- from .obs import (
32- Observation ,
33- PointObservation ,
34- TrackObservation ,
35- observation ,
36- )
31+ from .obs import Observation , PointObservation , TrackObservation
3732from .timeseries import TimeSeries
3833from .types import Period
3934
4035TimeDeltaTypes = Union [float , int , np .timedelta64 , pd .Timedelta , timedelta ]
4136IdxOrNameTypes = Optional [Union [int , str ]]
4237GeometryTypes = Optional [Literal ["point" , "track" , "unstructured" , "grid" ]]
38+ MRTypes = Union [
39+ PointModelResult ,
40+ GridModelResult ,
41+ DfsuModelResult ,
42+ TrackModelResult ,
43+ DummyModelResult ,
44+ ]
4345MRInputType = Union [
4446 str ,
4547 Path ,
5254 xr .Dataset ,
5355 xr .DataArray ,
5456 TimeSeries ,
55- GridModelResult ,
56- DfsuModelResult ,
57- TrackModelResult ,
58- DummyModelResult ,
57+ MRTypes ,
5958]
59+ ObsTypes = Union [PointObservation , TrackObservation ]
6060ObsInputType = Union [
6161 str ,
6262 Path ,
6565 mikeio .Dfs0 ,
6666 pd .DataFrame ,
6767 pd .Series ,
68- Observation ,
68+ ObsTypes ,
6969]
7070
7171T = TypeVar ("T" , bound = "TimeSeries" )
@@ -173,28 +173,24 @@ def from_matched(
173173
174174@overload
175175def match (
176- obs : Observation ,
177- mod : Union [ MRInputType , Sequence [MRInputType ] ],
176+ obs : ObsTypes ,
177+ mod : MRTypes | Sequence [MRTypes ],
178178 * ,
179- obs_item : Optional [IdxOrNameTypes ] = None ,
180- mod_item : Optional [IdxOrNameTypes ] = None ,
181- gtype : Optional [GeometryTypes ] = None ,
182179 max_model_gap : Optional [float ] = None ,
183180 spatial_method : Optional [str ] = None ,
181+ spatial_tolerance : float = 1e-3 ,
184182 obs_no_overlap : Literal ["ignore" , "error" , "warn" ] = "error" ,
185183) -> Comparer : ...
186184
187185
188186@overload
189187def match (
190- obs : Iterable [Observation ],
191- mod : Union [ MRInputType , Sequence [MRInputType ] ],
188+ obs : Iterable [ObsTypes ],
189+ mod : MRTypes | Sequence [MRTypes ],
192190 * ,
193- obs_item : Optional [IdxOrNameTypes ] = None ,
194- mod_item : Optional [IdxOrNameTypes ] = None ,
195- gtype : Optional [GeometryTypes ] = None ,
196191 max_model_gap : Optional [float ] = None ,
197192 spatial_method : Optional [str ] = None ,
193+ spatial_tolerance : float = 1e-3 ,
198194 obs_no_overlap : Literal ["ignore" , "error" , "warn" ] = "error" ,
199195) -> ComparerCollection : ...
200196
@@ -203,9 +199,6 @@ def match(
203199 obs ,
204200 mod ,
205201 * ,
206- obs_item = None ,
207- mod_item = None ,
208- gtype = None ,
209202 max_model_gap = None ,
210203 spatial_method : Optional [str ] = None ,
211204 spatial_tolerance : float = 1e-3 ,
@@ -222,17 +215,10 @@ def match(
222215
223216 Parameters
224217 ----------
225- obs : (str, Path, pd.DataFrame, Observation, Sequence[Observation])
218+ obs : (Observation, Sequence[Observation])
226219 Observation(s) to be compared
227- mod : (str, Path, pd.DataFrame, ModelResult, Sequence[ModelResult])
220+ mod : (ModelResult, Sequence[ModelResult])
228221 Model result(s) to be compared
229- obs_item : int or str, optional
230- observation item if obs is a file/dataframe, by default None
231- mod_item : (int, str), optional
232- model item if mod is a file/dataframe, by default None
233- gtype : (str, optional)
234- Geometry type of the model result (if mod is a file/dataframe).
235- If not specified, it will be guessed.
236222 max_model_gap : (float, optional)
237223 Maximum time gap (s) in the model result (e.g. for event-based
238224 model results), by default None
@@ -266,9 +252,6 @@ def match(
266252 return _match_single_obs (
267253 obs ,
268254 mod ,
269- obs_item = obs_item ,
270- mod_item = mod_item ,
271- gtype = gtype ,
272255 max_model_gap = max_model_gap ,
273256 spatial_method = spatial_method ,
274257 spatial_tolerance = spatial_tolerance ,
@@ -303,9 +286,6 @@ def match(
303286 _match_single_obs (
304287 o ,
305288 mod ,
306- obs_item = obs_item ,
307- mod_item = mod_item ,
308- gtype = gtype ,
309289 max_model_gap = max_model_gap ,
310290 spatial_method = spatial_method ,
311291 spatial_tolerance = spatial_tolerance ,
@@ -320,52 +300,42 @@ def match(
320300
321301
322302def _match_single_obs (
323- obs : ObsInputType ,
324- mod : Union [ MRInputType , Sequence [MRInputType ] ],
303+ obs : ObsTypes ,
304+ mod : MRTypes | Sequence [MRTypes ],
325305 * ,
326- obs_item : int | str | None ,
327- mod_item : int | str | None ,
328- gtype : GeometryTypes | None ,
329306 max_model_gap : float | None ,
330307 spatial_method : str | None ,
331308 spatial_tolerance : float ,
332309 obs_no_overlap : Literal ["ignore" , "error" , "warn" ],
333310) -> Comparer | None :
334- # TODO passing gtype to this function is inconsistent with `match` docstring, where gtype is the geometry type of model result
335- observation = _parse_single_obs (obs , obs_item , gtype = gtype )
336-
337311 if isinstance (mod , get_args (MRInputType )):
338312 models : list = [mod ]
339313 else :
340314 models = mod # type: ignore
341315
342- model_results = [_parse_single_model (m , item = mod_item , gtype = gtype ) for m in models ]
343- names = [m .name for m in model_results ]
316+ names = [m .name for m in models ]
344317 if len (names ) != len (set (names )):
345318 raise ValueError (f"Duplicate model names found: { names } " )
346319
347320 raw_mod_data = {
348321 m .name : (
349- m .extract (observation , spatial_method = spatial_method )
322+ m .extract (obs , spatial_method = spatial_method )
350323 if isinstance (m , (DfsuModelResult , GridModelResult , DummyModelResult ))
351324 else m
352325 )
353- for m in model_results
326+ for m in models
354327 }
355328
356329 matched_data = _match_space_time (
357- observation = observation ,
330+ observation = obs ,
358331 raw_mod_data = raw_mod_data ,
359332 max_model_gap = max_model_gap ,
360333 obs_no_overlap = obs_no_overlap ,
361334 spatial_tolerance = spatial_tolerance ,
362335 )
363336 if matched_data is None :
364337 return None
365- matched_data .attrs ["weight" ] = observation .weight
366-
367- # TODO where does this line belong?
368- matched_data .attrs ["modelskill_version" ] = __version__
338+ matched_data .attrs ["weight" ] = obs .weight
369339
370340 return Comparer (matched_data = matched_data , raw_mod_data = raw_mod_data )
371341
@@ -429,66 +399,3 @@ def mo_kind(k: str) -> bool:
429399 data = data .dropna (dim = "time" , subset = mo_cols )
430400
431401 return data
432-
433-
434- def _parse_single_obs (
435- obs : ObsInputType ,
436- obs_item : Optional [int | str ],
437- gtype : Optional [GeometryTypes ],
438- ) -> PointObservation | TrackObservation :
439- if isinstance (obs , (PointObservation , TrackObservation )):
440- if obs_item is not None :
441- raise ValueError (
442- "obs_item argument not allowed if obs is an modelskill.Observation type"
443- )
444- return obs
445- else :
446- # observation factory can only handle track and point
447- return observation (obs , item = obs_item , gtype = gtype ) # type: ignore
448-
449-
450- def _parse_single_model (
451- mod : MRInputType ,
452- item : Optional [IdxOrNameTypes ] = None ,
453- gtype : Optional [GeometryTypes ] = None ,
454- ) -> (
455- PointModelResult
456- | TrackModelResult
457- | GridModelResult
458- | DfsuModelResult
459- | DummyModelResult
460- ):
461- if isinstance (
462- mod ,
463- (
464- str ,
465- Path ,
466- pd .DataFrame ,
467- xr .Dataset ,
468- xr .DataArray ,
469- mikeio .Dfs0 ,
470- mikeio .Dataset ,
471- mikeio .DataArray ,
472- mikeio .dfsu .Dfsu2DH ,
473- ),
474- ):
475- try :
476- return model_result (mod , item = item , gtype = gtype )
477- except ValueError as e :
478- raise ValueError (
479- f"Could not compare. Unknown model result type { type (mod )} . { str (e )} "
480- )
481- else :
482- if item is not None :
483- raise ValueError ("item argument not allowed if mod is a ModelResult type" )
484- assert isinstance (
485- mod ,
486- (
487- PointModelResult ,
488- TrackModelResult ,
489- GridModelResult ,
490- DfsuModelResult ,
491- DummyModelResult ,
492- ),
493- )
494- return mod
0 commit comments