New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow for user modification of the working directory (default of site-packages/covalent_ui is also maybe a bug) #1592
Comments
site-packages/covalent_ui
is unexpected
site-packages/covalent_ui
is unexpectedsite-packages/covalent_ui
is unexpected
site-packages/covalent_ui
is unexpectedsite-packages/covalent_ui
is unexpected
site-packages/covalent_ui
is unexpectedsite-packages/covalent_ui
is unexpected)
site-packages/covalent_ui
is unexpected)site-packages/covalent_ui
is unexpected)
site-packages/covalent_ui
is unexpected)site-packages/covalent_ui
is unexpected)
@santoshkumarradha --- could you switch the label to "feature?" Thanks! This is a pretty mission-critical issue to me, so I will do my best to work on a PR next week, assuming I've understood the scenario correctly. |
hey @arosen93, thanks again for this insightful issue, please keep 'em coming. Default Working Directory and Executor DependencyThe default working directory for an Electron executed locally is determined by the Best Practices for File Handling in CovalentWhen manually reading or writing files within Covalent, it's always a good practice to use absolute paths. By doing so, you can avoid potential confusion or issues related to file handling. Here's an example that demonstrates the use of absolute paths in a Covalent workflow: import covalent as ct
from pathlib import Path
@ct.electron
def job(val1, val2,file):
with open(file, "w") as w:
w.write(str(val1 + val2))
return "Done!"
@ct.lattice
def workflow(input_file, val1, val2, val3, val4):
job1 = job(val1, val2,input_file)
return job1
input_file = Path("./example1.txt").absolute()
dispatch_id = ct.dispatch(workflow)(input_file, 1, 2, 3, 4)
result = ct.get_result(dispatch_id, wait=True)
print(result) Handling Output Files generated inside electronFor chemistry codes that generate output files during calculations, it is crucial to ensure that these files are not overwritten when multiple calculations are executed simultaneously. To achieve this, you can create separate folders for each calculation and use a decorator that changes the working directory to the desired absolute path before executing the function. import covalent as ct
from pathlib import Path
import os
from functools import wraps
def change_dir_and_execute(directory):
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
current_dir = os.getcwd()
try:
os.chdir(directory)
result = func(*args, **kwargs)
finally:
os.chdir(current_dir)
return result
return wrapper
return decorator
path=Path(".").absolute()
@ct.electron
@change_dir_and_execute(path)
def job(val1, val2,file):
with open(file, "w") as w:
w.write(str(val1 + val2))
return Path(".").absolute()
@ct.lattice
def workflow(file, val1, val2, val3, val4):
job1 = job(val1, val2,file)
return job1
file="example.txt"
dispatch_id = ct.dispatch(workflow)(file, 1, 2, 3, 4)
result = ct.get_result(dispatch_id, wait=True)
print(result) In this example, the Electron is executed in the specified path (the current directory), ensuring that output files from different calculations are written to separate directories and not overwritten. I hope this explanation helps address your concerns and suggestions. If you have any specific recommendations on how we can make the documentation clearer or any other concerns, please feel free to share your thoughts. These real-world usage questions are valuable, and we have encountered similar challenges while working on our calculations as well. As this is not a technical issue but rather a pointer on best practices and usage, would it be okay to move this to the discussions section? This way, other users who might face similar questions or concerns can benefit from the information and the proposed solutions. |
Thanks for the reply, @santoshkumarradha! A few comments in return: Re: The working directory
Based on what I'm seeing, I don't think this is entirely true. I did a clean install of covalent 0.220.0 on Ubuntu and ran my sample workflow above but this time specified Here is my config just as further support:
As a result, it is not currently possible to specify or alter the current working directory. It is fixed to where the Python package for Covalent is, which I don't think is desirable. This should probably be a configuration option. Re: Best Practices for File Handling in Covalent
I definitely agree absolute paths are best! That being said, in practice, it is rare that I can control where the files are written out at runtime. That is usually specified by the external code that is being run and is often the current working directory. Otherwise, definitely! Re: Handling Output Files generated inside electron
Thank you for sharing this workaround! I appreciate it! I think it is still not fully satisfying. My main reason behind this is that, while it can be done as you suggest (which I will confirm), it is certainly not concise or clean. One of the major benefits of Covalent as a whole is that there is minimal friction to go from writing a function to running a complex workflow. Given the many foreseeable use cases (at least in my field) where significant I/O is written out to the current working directory at runtime (without this being something that can be changed), always needing this somewhat verbose approach is a bit less than ideal. I would still push for a configuration variable of sorts that the user could specify that would write out all files to something like
I think parts of this can certainly be migrated over, but I still think that an issue should remain given the points above. Happy to discuss further if you think otherwise! Thanks a ton for your answer, and I look forward to your reply! I'll continue to think about the best way to potentially address this. |
site-packages/covalent_ui
is unexpected)site-packages/covalent_ui
is also maybe a bug)
Oops, Let me get back on the first part (I may be wrong about it with the recent changes, will quickly circle with the team) But meanwhile I agree with this,
Could you let me know an ideal UX you would prefer for changing the "working" directory of the electron? (something that is consistent for both local and remote executors) Is the UX that the default execution folder is indeed a folder that is One thing that we have not worked a lot on docs is also file transfer mechanisms and adding call backs/ call befores which can run pre run and post run hooks for data transfers as well, would you think these are something that would work as well? |
site-packages/covalent_ui
is also maybe a bug)
Thanks! To make this more manageable, I have opened a new issue at #1619 for this part of my original post. The remainder of the current issue can be dedicated to the feature for creating subdirectories that would avoid file overwriting.
I think my general recommendation is that there should be a keyword argument of the type Note that the base directory for this may be distinct from the
Unfortunately, this route isn't sufficient for the problem. We want to make sure that if there are multiple ongoing calculations that write files out to the current working directory during runtime that they do not overwrite one another. Many computational chemistry codes are hard-coded to rely on being able to write and read input and output files in the current working directory throughout the calculation itself. Transferring files before or after the calculation finishes is a separate issue that should be left to the user, as you suggested. I moved this to #1628 to prevent confusion and closed this issue. |
Note: This is a feature addition, not a docs update
What should we add?
Here are my suggestions, which I motivate further below:
Introduce a configuration option in all executors to set where the base "workdir" is (like the
remote_workdir
in the SLURM plugin). --> moved to Working directory is not appropriate for local executors #1619.Perhaps more importantly, introduce a configuration option to let the user decide if they want a given calculation's "workdir" to be done in a unique subfolder (e.g. at
workdir/dispatch_id/node_number
) both for organizational purposes and to prevent overwriting. --> moved to Allow for the creation of unique subfolders in the current working directory to avoid file overwriting #1628.Why should we add it?
In this example, a file
job.txt
gets written out to the filesystem. It was a bit of an undesirable surprise to me to find out that, in my case, it was being written out to~/anaconda3/envs/covalent/lib/python3.9/site-packages/covalent_ui
. If there's already a way to modify this, it should be mentioned in the docs.~/anaconda3/envs/covalent/lib/python3.9/site-packages/covalent_ui
directory by default, as described before. In more complex examples, this can lead to unexpected errors when electrons are being executed in parallel (and also it's bad to lose calculation files in general). I will note that this is also present with other executors. For instance, with the SLURM plugin, everything is written out to theremote_workdir
in a similar way. If there is already an option to do this, it should be mentioned in the docs.The text was updated successfully, but these errors were encountered: