Skip to content

Commit faed9ed

Browse files
authored
Merge pull request #1435 from danforthcenter/fix-PTC-W0010
fix-PTC-W0010
2 parents 0fc53c4 + aaa3970 commit faed9ed

File tree

3 files changed

+51
-52
lines changed

3 files changed

+51
-52
lines changed

plantcv/learn/naive_bayes.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -149,15 +149,15 @@ def naive_bayes_multiclass(samples_file, outfile, mkplots=False):
149149
for channel, cls in pdfs.items():
150150
_plot_pdf(channel, os.path.dirname(outfile), **cls)
151151
# Write the PDFs to a text file
152-
out = open(outfile, "w")
153-
# Write the column labels
154-
out.write("class\tchannel\t" + "\t".join(map(str, range(0, 256))) + "\n")
155-
# For each channel
156-
for channel, cls in pdfs.items():
157-
# For each class
158-
for class_name, pdf in cls.items():
159-
# Each row is the PDF for the given class and color channel
160-
out.write(class_name + "\t" + channel + "\t" + "\t".join(map(str, pdf)) + "\n")
152+
with open(outfile, "w") as out:
153+
# Write the column labels
154+
out.write("class\tchannel\t" + "\t".join(map(str, range(0, 256))) + "\n")
155+
# For each channel
156+
for channel, cls in pdfs.items():
157+
# For each class
158+
for class_name, pdf in cls.items():
159+
# Each row is the PDF for the given class and color channel
160+
out.write(class_name + "\t" + channel + "\t" + "\t".join(map(str, pdf)) + "\n")
161161

162162

163163
def _split_plant_background_signal(channel, mask):

plantcv/plantcv/classes.py

+33-33
Original file line numberDiff line numberDiff line change
@@ -178,39 +178,39 @@ def save_results(self, filename, outformat="json"):
178178

179179
elif outformat.upper() == "CSV":
180180
# Open output CSV file
181-
csv_table = open(filename, "w")
182-
# Gather any additional metadata
183-
metadata_key_list = list(self.metadata.keys())
184-
metadata_val_list = [val["value"] for val in self.metadata.values()]
185-
# Write the header
186-
header = metadata_key_list + ["sample", "trait", "value", "label"]
187-
csv_table.write(",".join(map(str, header)) + "\n")
188-
# Iterate over data samples
189-
for sample in self.observations:
190-
# Iterate over traits for each sample
191-
for var in self.observations[sample]:
192-
val = self.observations[sample][var]["value"]
193-
# If the data type is a list or tuple we need to unpack the data
194-
if isinstance(val, (list, tuple)):
195-
# Combine each value with its label
196-
for value, label in zip(self.observations[sample][var]["value"],
197-
self.observations[sample][var]["label"]):
198-
# Skip list of tuple data types
199-
if not isinstance(value, tuple):
200-
# Save one row per value-label
201-
row = metadata_val_list + [sample, var, value, label]
202-
csv_table.write(",".join(map(str, row)) + "\n")
203-
# If the data type is Boolean, store as a numeric 1/0 instead of True/False
204-
elif isinstance(val, bool):
205-
row = metadata_val_list + [sample, var, int(self.observations[sample][var]["value"]),
206-
self.observations[sample][var]["label"]]
207-
csv_table.write(",".join(map(str, row)) + "\n")
208-
# For all other supported data types, save one row per trait
209-
# Assumes no unusual data types are present (possibly a bad assumption)
210-
else:
211-
row = metadata_val_list + [sample, var, self.observations[sample][var]["value"],
212-
self.observations[sample][var]["label"]]
213-
csv_table.write(",".join(map(str, row)) + "\n")
181+
with open(filename, "w") as csv_table:
182+
# Gather any additional metadata
183+
metadata_key_list = list(self.metadata.keys())
184+
metadata_val_list = [val["value"] for val in self.metadata.values()]
185+
# Write the header
186+
header = metadata_key_list + ["sample", "trait", "value", "label"]
187+
csv_table.write(",".join(map(str, header)) + "\n")
188+
# Iterate over data samples
189+
for sample in self.observations:
190+
# Iterate over traits for each sample
191+
for var in self.observations[sample]:
192+
val = self.observations[sample][var]["value"]
193+
# If the data type is a list or tuple we need to unpack the data
194+
if isinstance(val, (list, tuple)):
195+
# Combine each value with its label
196+
for value, label in zip(self.observations[sample][var]["value"],
197+
self.observations[sample][var]["label"]):
198+
# Skip list of tuple data types
199+
if not isinstance(value, tuple):
200+
# Save one row per value-label
201+
row = metadata_val_list + [sample, var, value, label]
202+
csv_table.write(",".join(map(str, row)) + "\n")
203+
# If the data type is Boolean, store as a numeric 1/0 instead of True/False
204+
elif isinstance(val, bool):
205+
row = metadata_val_list + [sample, var, int(self.observations[sample][var]["value"]),
206+
self.observations[sample][var]["label"]]
207+
csv_table.write(",".join(map(str, row)) + "\n")
208+
# For all other supported data types, save one row per trait
209+
# Assumes no unusual data types are present (possibly a bad assumption)
210+
else:
211+
row = metadata_val_list + [sample, var, self.observations[sample][var]["value"],
212+
self.observations[sample][var]["label"]]
213+
csv_table.write(",".join(map(str, row)) + "\n")
214214

215215
def plot_dists(self, variable):
216216
"""Plot a distribution of data.

plantcv/utils/converters.py

+9-10
Original file line numberDiff line numberDiff line change
@@ -204,13 +204,12 @@ def tabulate_bayes_classes(input_file, output_file):
204204
class_dict[class_name].append("")
205205

206206
# Open the output file
207-
out = open(output_file, "w")
208-
# Create the output table
209-
class_names = class_dict.keys()
210-
out.write("\t".join(map(str, class_names)) + "\n")
211-
for i in range(0, total_rgb):
212-
row = []
213-
for class_name in class_names:
214-
row.append(class_dict[class_name][i])
215-
out.write("\t".join(map(str, row)) + "\n")
216-
out.close()
207+
with open(output_file, "w") as out:
208+
# Create the output table
209+
class_names = class_dict.keys()
210+
out.write("\t".join(map(str, class_names)) + "\n")
211+
for i in range(0, total_rgb):
212+
row = []
213+
for class_name in class_names:
214+
row.append(class_dict[class_name][i])
215+
out.write("\t".join(map(str, row)) + "\n")

0 commit comments

Comments
 (0)