Skip to content

Commit

Permalink
Ensure UTF-8 encoding for all file write operations (#199)
Browse files Browse the repository at this point in the history
This commit explicitly sets encoding="utf-8" for all instances of path.open("w") across the ChemEx codebase. This change addresses issues related to default system encodings that are not UTF-8, such as cp1251, preventing UnicodeEncodeErrors during file write operations. It enhances the software's compatibility and reliability across different computing environments.

Fixes #196
  • Loading branch information
gbouvignies authored Feb 8, 2024
1 parent 41b7796 commit bfb2217
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion chemex/containers/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def plot_simulation(self, path: Path):

def write(self, path: Path):
filename = (path / self.filename.name).with_suffix(".dat")
with filename.open("w") as file_dat:
with filename.open("w", encoding="utf-8") as file_dat:
file_dat.write(self.printer.header)
for profile in sorted(self.profiles):
file_dat.write(str(profile))
Expand Down
4 changes: 2 additions & 2 deletions chemex/optimize/fitting.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
def _run_statistics(
experiments: Experiments,
path: Path,
fitmethod: str | None = None,
fitmethod: str,
statistics: Statistics | None = None,
) -> None:
if statistics is None:
Expand All @@ -60,7 +60,7 @@ def _run_statistics(

print_running_statistics(method["message"])

with (path / method["filename"]).open(mode="w") as fileout:
with (path / method["filename"]).open(mode="w", encoding="utf-8") as fileout:
fileout.write(print_header(ids_vary))

try:
Expand Down
4 changes: 2 additions & 2 deletions chemex/optimize/gridding.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def run_group_grid(
group: Group,
grid: dict[str, ArrayFloat],
path: Path,
fitmethod: str | None,
fitmethod: str,
) -> GridResult:
group_ids = group.experiments.param_ids
group_params = database.build_lmfit_params(group_ids)
Expand All @@ -66,7 +66,7 @@ def run_group_grid(
best_chisqr = np.inf
best_params = group_params

with filename.open("w") as fileout:
with filename.open("w", encoding="utf-8") as fileout:
fileout.write(print_header(group_grid))

chisqr_list: list[float] = []
Expand Down
2 changes: 1 addition & 1 deletion chemex/optimize/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def _write_statistics(experiments: Experiments, path: Path) -> None:
params_lf = database.build_lmfit_params(experiments.param_ids)
stats = calculate_statistics(experiments, params_lf)
filename = path / "statistics.toml"
with filename.open(mode="w") as f:
with filename.open("w", encoding="utf-8") as f:
f.write(f"\"number of data points\" = {stats['ndata']}\n")
f.write(f"\"number of variables\" = {stats['nvarys']}\n")
f.write(f"\"chi-square\" = {stats['chisqr']: .5e}\n")
Expand Down
6 changes: 3 additions & 3 deletions chemex/plotters/cest.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ def plot(self, path: Path, profiles: list[Profile]) -> None:

with ExitStack() as stack:
file_pdf = stack.enter_context(PdfPages(str(name_pdf)))
file_calc = stack.enter_context(name_fit.open("w"))
file_exp = stack.enter_context(name_exp.open("w"))
file_calc = stack.enter_context(name_fit.open("w", encoding="utf-8"))
file_exp = stack.enter_context(name_exp.open("w", encoding="utf-8"))
for profile in sorted(profiles):
data_exp = create_plot_data_exp(profile)
data_calc = create_plot_data_calc(profile)
Expand All @@ -243,7 +243,7 @@ def plot_simulation(self, path: Path, profiles: list[Profile]) -> None:

with ExitStack() as stack:
file_pdf = stack.enter_context(PdfPages(str(name_pdf)))
file_sim = stack.enter_context(name_sim.open("w"))
file_sim = stack.enter_context(name_sim.open("w", encoding="utf-8"))
for profile in sorted(profiles):
data_exp = Data(np.array([]), np.array([]), np.array([]))
data_calc = create_plot_data_calc(profile)
Expand Down
6 changes: 3 additions & 3 deletions chemex/plotters/cpmg.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ def plot(self, path: Path, profiles: list[Profile]) -> None:

with ExitStack() as stack:
file_pdf = stack.enter_context(PdfPages(str(name_pdf)))
file_calc = stack.enter_context(name_fit.open("w"))
file_exp = stack.enter_context(name_exp.open("w"))
file_calc = stack.enter_context(name_fit.open("w", encoding="utf-8"))
file_exp = stack.enter_context(name_exp.open("w", encoding="utf-8"))
for profile in sorted(profiles):
data_exp = create_plot_data_exp(profile, self.config)
data_calc = create_plot_data_calc(profile, self.config)
Expand All @@ -188,7 +188,7 @@ def plot_simulation(self, path: Path, profiles: list[Profile]) -> None:

with ExitStack() as stack:
file_pdf = stack.enter_context(PdfPages(str(name_pdf)))
file_sim = stack.enter_context(name_sim.open("w"))
file_sim = stack.enter_context(name_sim.open("w", encoding="utf-8"))
for profile in sorted(profiles):
data_exp = Data(np.array([]), np.array([]), np.array([]))
data_calc = create_plot_data_calc(profile, self.config)
Expand Down
4 changes: 2 additions & 2 deletions chemex/plotters/exsy.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ def plot(self, path: Path, profiles: list[Profile]) -> None:

with ExitStack() as stack:
file_pdf = stack.enter_context(PdfPages(str(name_pdf)))
file_calc = stack.enter_context(name_fit.open("w"))
file_exp = stack.enter_context(name_exp.open("w"))
file_calc = stack.enter_context(name_fit.open("w", encoding="utf-8"))
file_exp = stack.enter_context(name_exp.open("w", encoding="utf-8"))
for profile in sorted(profiles):
data_exp_dict = create_plot_data_exp(profile)
data_calc_dict = create_plot_data_calc(profile)
Expand Down
6 changes: 3 additions & 3 deletions chemex/plotters/relaxation.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ def plot(self, path: Path, profiles: list[Profile]) -> None:

with ExitStack() as stack:
file_pdf = stack.enter_context(PdfPages(str(name_pdf)))
file_calc = stack.enter_context(name_fit.open("w"))
file_exp = stack.enter_context(name_exp.open("w"))
file_calc = stack.enter_context(name_fit.open("w", encoding="utf-8"))
file_exp = stack.enter_context(name_exp.open("w", encoding="utf-8"))
for profile in sorted(profiles):
data_exp = create_plot_data_exp(profile)
data_calc = create_plot_data_calc(profile)
Expand All @@ -96,7 +96,7 @@ def plot_simulation(self, path: Path, profiles: list[Profile]) -> None:

with ExitStack() as stack:
file_pdf = stack.enter_context(PdfPages(str(name_pdf)))
file_sim = stack.enter_context(name_sim.open("w"))
file_sim = stack.enter_context(name_sim.open("w", encoding="utf-8"))
for profile in sorted(profiles):
data_exp = create_plot_data_exp(profile)
data_calc = create_plot_data_calc(profile)
Expand Down
4 changes: 2 additions & 2 deletions chemex/tools/pick_cest/buttons.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ def _save(self):
fname2 = self.out / "dw_ab.toml"

with contextlib.ExitStack() as stack:
file1 = stack.enter_context(fname1.open("w"))
file2 = stack.enter_context(fname2.open("w"))
file1 = stack.enter_context(fname1.open("w", encoding="utf-8"))
file2 = stack.enter_context(fname2.open("w", encoding="utf-8"))
file1.write("[CS_A]\n")
file2.write("[DW_AB]\n")

Expand Down

0 comments on commit bfb2217

Please sign in to comment.