Skip to content
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

Update __init__.py #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions Xerus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import os
from multiprocessing import Pool
import concurrent.futures
from typing import Any, List, Union

import numpy as np
Expand Down Expand Up @@ -262,7 +263,7 @@ def get_cifs(
self.cif_notsim.to_csv(name_out_notsim, index=False)
return self

def simulate_all(self, n_jobs: int = -1):
def simulate_all(self, n_jobs: int = -1, timeout_value: int = 10):
"""
Parallel simulation of XRD patterns using Modin (Ray backend)
Parameters
Expand Down Expand Up @@ -290,10 +291,25 @@ def simulate_all(self, n_jobs: int = -1):
[f] + [tmin, tmax, step, working_folder, instr_params]
for f in ciflist.full_path
]
with Pool(processes=n_jobs) as p:
paths = p.starmap(simulate_spectra, args)
p.close()
p.join()
with concurrent.futures.ProcessPoolExecutor(max_workers=n_jobs) as executor:
future_to_args = {executor.submit(simulate_spectra, *arg): arg for arg in args}
results=[]
for future in concurrent.futures.as_completed(future_to_args):
args = future_to_args[future]
try:
result = future.result(timeout=timeout_value)
results.append(result)
except concurrent.futures.TimeoutError:
print(f'Task for file {args[0]} timed out after {timeout_value} seconds')
results.append(None)
except Exception as e:
print(f'Task for file {args[0]} raised an exception: {e}')
results.append(None)

paths = [None] * len(ciflist)
for i, result in enumerate(results):
if result is not None:
paths[i] = result
print("Done. Cleaning up GSASII files.")
# Clean up
files = [
Expand All @@ -304,9 +320,9 @@ def simulate_all(self, n_jobs: int = -1):
for file in files:
# print(f"Cleaning up {file}")
os.remove(file)
ciflist["simulated_files"] = [r[0] for r in paths]
ciflist["simulated_reflects"] = [r[1] for r in paths]
ciflist["sm_ran"] = [r[2] for r in paths]
ciflist["simulated_files"] = [r[0] if r is not None else None for r in paths]
ciflist["simulated_reflects"] = [r[1] if r is not None else None for r in paths]
ciflist["sm_ran"] = [r[2] if r is not None else False for r in paths]
ciflist = ciflist[ciflist["sm_ran"]]
ciflist.drop(["sm_ran"], axis=1, inplace=True)
ciflist.reset_index(drop=True, inplace=True)
Expand Down