Skip to content

Commit

Permalink
Fixing skipped lines in csv writing when using a windows computer (#406)
Browse files Browse the repository at this point in the history
* Remove skip line error in csv writing on windows from make_predictions

* Remove skip line error in csv writing on windows from save_smiles_splits

* Add newline option for dictwriter
  • Loading branch information
cjmcgill committed Jun 13, 2023
1 parent 256fbd9 commit 0c3f334
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion chemprop/sklearn_predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def predict_sklearn(args: SklearnPredictArgs) -> None:
datapoint.row[pred_name] = pred

# Save
with open(args.preds_path, 'w') as f:
with open(args.preds_path, 'w', newline="") as f:
writer = csv.DictWriter(f, fieldnames=data[0].row.keys())
writer.writeheader()

Expand Down
4 changes: 2 additions & 2 deletions chemprop/train/make_predictions.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ def predict_and_save(
datapoint.row[pred_name + f"_model_{idx}"] = pred

# Save
with open(args.preds_path, 'w') as f:
with open(args.preds_path, 'w', newline="") as f:
writer = csv.DictWriter(f, fieldnames=full_data[0].row.keys())
writer.writeheader()

Expand All @@ -311,7 +311,7 @@ def predict_and_save(
print(f"Saving uncertainty evaluations to {args.evaluation_scores_path}")
if args.dataset_type == "multiclass":
task_names = original_task_names
with open(args.evaluation_scores_path, "w") as f:
with open(args.evaluation_scores_path, "w", newline="") as f:
writer = csv.writer(f)
writer.writerow(["evaluation_method"] + task_names)
for i, evaluation_method in enumerate(args.evaluation_methods):
Expand Down
2 changes: 1 addition & 1 deletion chemprop/train/molecule_fingerprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def molecule_fingerprint(args: FingerprintArgs,
datapoint.row[fingerprint_columns[i]] = preds[i]

# Write predictions
with open(args.preds_path, 'w') as f:
with open(args.preds_path, 'w', newline="") as f:
writer = csv.DictWriter(f, fieldnames=args.smiles_columns+fingerprint_columns,extrasaction='ignore')
writer.writeheader()
for datapoint in full_data:
Expand Down
10 changes: 5 additions & 5 deletions chemprop/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ def save_smiles_splits(
if dataset is None:
continue

with open(os.path.join(save_dir, f"{name}_smiles.csv"), "w") as f:
with open(os.path.join(save_dir, f"{name}_smiles.csv"), "w", newline="") as f:
writer = csv.writer(f)
if smiles_columns[0] == "":
writer.writerow(["smiles"])
Expand All @@ -684,7 +684,7 @@ def save_smiles_splits(
for smiles in dataset.smiles():
writer.writerow(smiles)

with open(os.path.join(save_dir, f"{name}_full.csv"), "w") as f:
with open(os.path.join(save_dir, f"{name}_full.csv"), "w", newline="") as f:
writer = csv.writer(f)
writer.writerow(smiles_columns + task_names)
dataset_targets = dataset.targets()
Expand All @@ -695,7 +695,7 @@ def save_smiles_splits(
if features_path is not None:
dataset_features = dataset.features()
if extension_sets == {'.csv'}:
with open(os.path.join(save_dir, f"{name}_features.csv"), "w") as f:
with open(os.path.join(save_dir, f"{name}_features.csv"), "w", newline="") as f:
writer = csv.writer(f)
writer.writerow(features_header)
writer.writerows(dataset_features)
Expand All @@ -704,7 +704,7 @@ def save_smiles_splits(

if constraints_path is not None:
dataset_constraints = [d.raw_constraints for d in dataset._data]
with open(os.path.join(save_dir, f"{name}_constraints.csv"), "w") as f:
with open(os.path.join(save_dir, f"{name}_constraints.csv"), "w", newline="") as f:
writer = csv.writer(f)
writer.writerow(constraints_header)
writer.writerows(dataset_constraints)
Expand All @@ -729,7 +729,7 @@ def save_smiles_splits(
if name == "train":
data_weights = dataset.data_weights()
if any([w != 1 for w in data_weights]):
with open(os.path.join(save_dir, f"{name}_weights.csv"), "w") as f:
with open(os.path.join(save_dir, f"{name}_weights.csv"), "w", newline="") as f:
writer = csv.writer(f)
writer.writerow(["data weights"])
for weight in data_weights:
Expand Down
2 changes: 1 addition & 1 deletion scripts/find_similar_mols.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def save_similar_mols(test_path: str,
# Save results
makedirs(save_path, isfile=True)

with open(save_path, 'w') as f:
with open(save_path, 'w', newline="") as f:
writer = csv.DictWriter(f, fieldnames=similar_mols[0].keys())
writer.writeheader()
for row in similar_mols:
Expand Down

0 comments on commit 0c3f334

Please sign in to comment.