OpenGATE / opengate

Gate 10 (beta)
http://www.opengatecollaboration.org
GNU Lesser General Public License v3.0
46 stars 40 forks source link

Replace os by pathlib to avoid OS related errors and improve code maintenance #427

Closed BishopWolf closed 3 months ago

BishopWolf commented 3 months ago

This PR fixes fresh installations errors when parent folders are not created by default giving

FileNotFoundError: [Errno 2] No such file or directory: '...

These errors were triggered by some tests and are all solved by this PR. The change ensures the folders AND all their parents are created.

dsarrut commented 3 months ago

Excellent, thanks! However, I got: AttributeError: module 'os' has no attribute 'mkdirs' maybe makedirs ?

BishopWolf commented 3 months ago

Excellent, thanks! However, I got: AttributeError: module 'os' has no attribute 'mkdirs' maybe makedirs ?

I have changed some os calls by pathlib. This is very easy to maintain as all OS related specificities are automatically handled.

BishopWolf commented 3 months ago

For reference: https://www.freecodecamp.org/news/how-to-use-pathlib-module-in-python/

BishopWolf commented 3 months ago

I am solving all errors caught by the tests

BishopWolf commented 3 months ago

There are still places expecting string paths instead of path objects. As a rule, you can't pass strings to methods expecting paths, as it's very difficult to maintain. This is why pathlib is preferred. Example:

path1 = "/some/path"
path2 = "/some/path/"

# adding a file pathlib way
file1 = Path(path1) / "myfile"
file2 = Path(path2) / "myfile"

assert(str(file1) == str(file2)) # returns true

# os way
file1 = os.path.join(path1, "myfile")
file2 = os.path.join(path2, "myfile")

assert(file1== file2) # returns false
# file1 is "/some/path/myfile" in Nix and "/some/path\myfile" in Windows
# file2 is "/some/path//myfile" in Nix and "/some/path/\myfile" in Windows
BishopWolf commented 3 months ago

I see some merge conflicts with the last pull in wget. @tbaudier may you please review?

BishopWolf commented 3 months ago

The only error remaining in test040_gan_phsp_pet_gan.py

File "/opt/hostedtoolcache/Python/3.11.9/x64/lib/python3.11/site-packages/opengate/actors/arfactors.py", line 243, in apply_with_lock
    w = self.garf.nn_predict(self.model, self.nn["model_data"], ax)
        ^^^^^^^^^^^^^^^^^^^^
AttributeError: module 'garf' has no attribute 'nn_predict'
BishopWolf commented 3 months ago

@tbaudier : all tests are passing now

BishopWolf commented 3 months ago

@tbaudier : my tests didn't show any errors, see https://github.com/BishopWolf/opengate/actions/runs/10093731109/job/27911347164

Something is weird

tbaudier commented 3 months ago

@tbaudier : my tests didn't show any errors, see https://github.com/BishopWolf/opengate/actions/runs/10093731109/job/27911347164

Something is weird

I will look at your PR, I think it's due to the rebasing of the branch. Do not modify it for the moment and I will try to do the necessary. Thomas

BishopWolf commented 3 months ago

This error is because filename shall be Path:

 File "/opt/hostedtoolcache/Python/3.11.9/x64/lib/python3.11/site-packages/opengate/engines.py", line 612, in stop_simulation
    actor.EndSimulationAction()
  File "/opt/hostedtoolcache/Python/3.11.9/x64/lib/python3.11/site-packages/opengate/actors/miscactors.py", line 126, in EndSimulationAction
    self.write(self.user_info.output)
  File "/opt/hostedtoolcache/Python/3.11.9/x64/lib/python3.11/site-packages/opengate/actors/miscactors.py", line 146, in write
    with filename.open("w") as f:
         ^^^^^^^^^^^^^
AttributeError: 'str' object has no attribute 'open'

But it's being called by image

and user_info.output is just a filename string without path