Repository: teamtomo/mdocfile (language: python)

Issue:
PureWindowsPath validation breaks mdocfile for linux
Hi folks - not being familiar with the codebase I'm reluctant to make changes, but I had to replace PureWindowsPath with PurePosixPath throughout data_models.py for it to work on Linux (needless to say this will break it for Windows).


Discussion / hints:
Hey Morgan, thanks for the report!

@shahpnmlab any thoughts on this? I know you pushed for the change to WindowsPath
I just tried this out (on MacOS, not linux) and don't run into the issue when constructing from a string

```ipython
In [1]: from mdocfile.data_models import MdocSectionData

In [2]: MdocSectionData(SubFramePath='bla.tif')
Out[2]: MdocSectionData(ZValue=None, MontSection=None, FrameSet=None, TiltAngle=None, PieceCoordinates=None, StagePosition=None, StageZ=None, Magnification=None, CameraLength=None, MagIndex=None, Intensity=None, SuperMontCoords=None, PixelSpacing=None, ExposureDose=None, DoseRate=None, SpotSize=None, Defocus=None, TargetDefocus=None, ImageShift=None, RotationAngle=None, ExposureTime=None, Binning=None, UsingCDS=None, CameraIndex=None, DividedBy2=None, LowDoseConSet=None, MinMaxMean=None, PriorRecordDose=None, XedgeDxy=None, YedgeDxy=None, XedgeDxyVS=None, YedgeDxyVS=None, StageOffsets=None, AlignedPieceCoords=None, AlignedPieceCoordsVS=None, SubFramePath=PureWindowsPath('bla.tif'), NumSubFrames=None, FrameDosesAndNumbers=None, DateTime=None, NavigatorLabel=None, FilterSlitAndLoss=None, ChannelName=None, MultiShotHoleAndPosition=None, CameraPixelSize=None, Voltage=None)
```

i.e. I can construct the section data and the path is coerced to a Windows path from a string

I see the issue when constructing from a path though and agree that should work

```ipython
In [3]: MdocSectionData(SubFramePath=Path('bla.tif'))
---------------------------------------------------------------------------
ValidationError                           Traceback (most recent call last)
Cell In[3], line 1
----> 1 MdocSectionData(SubFramePath=Path('bla.tif'))

File ~/mambaforge/envs/mdocfile/lib/python3.10/site-packages/pydantic/main.py:176, in BaseModel.__init__(self, **data)
    174 # `__tracebackhide__` tells pytest and some other tools to omit this function from tracebacks
    175 __tracebackhide__ = True
--> 176 self.__pydantic_validator__.validate_python(data, self_instance=self)

ValidationError: 1 validation error for MdocSectionData
SubFramePath
  Input is not a valid path [type=path_type, input_value=PosixPath('bla.tif'), input_type=PosixPath]
```

I have an idea how I might fix it so this works as expected for the future, glad you got yourself sorted for now :)

Fix patch:
diff --git a/src/mdocfile/data_models.py b/src/mdocfile/data_models.py
index d8c8e45..0bd5b84 100644
--- a/src/mdocfile/data_models.py
+++ b/src/mdocfile/data_models.py
@@ -98,7 +98,7 @@ class MdocSectionData(BaseModel):
     AlignedPieceCoords: Optional[Union[Tuple[float, float], Tuple[float, float, float]]] = None
     AlignedPieceCoordsVS: Optional[
         Union[Tuple[float, float], Tuple[float, float, float]]] = None
-    SubFramePath: Optional[PureWindowsPath] = None
+    SubFramePath: Optional[Union[PureWindowsPath, Path]] = None
     NumSubFrames: Optional[int] = None
     FrameDosesAndNumbers: Optional[Sequence[Tuple[float, int]]] = None
     DateTime: Optional[str] = None


Test patch:
diff --git a/tests/test_data_models.py b/tests/test_data_models.py
index 3b62693..50076ad 100644
--- a/tests/test_data_models.py
+++ b/tests/test_data_models.py
@@ -89,3 +89,6 @@ def test_to_string_is_valid_mdoc(tilt_series_mdoc_file):
     for (k1, v1), (k2, v2) in zip(mdoc_dict.items(), mdoc2_dict.items()):
         assert v1 == v2
         assert k1 == k2
+
+def test_section_data_from_path():
+    section = MdocSectionData(SubFramePath=Path('bla.tif'))
\ No newline at end of file