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

Release 4.0 append output for save_results #1225

Open
wants to merge 6 commits into
base: release-4.0
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
Use with to open file in save_results
afinit committed Jul 8, 2023
commit 0f2c2df613a1a4849b49d95f97fe0e9ee399a920
68 changes: 34 additions & 34 deletions plantcv/plantcv/classes.py
Original file line number Diff line number Diff line change
@@ -150,40 +150,40 @@ def save_results(self, filename, outformat="json"):
json.dump(hierarchical_data, f)
elif outformat.upper() == "CSV":
# Open output CSV file
csv_table = open(filename, "w")
# Write the header
csv_table.write(",".join(map(str, ["sample", "trait", "value", "label"])) + "\n")
# Iterate over data samples
for sample in self.observations:
# Iterate over traits for each sample
for var in self.observations[sample]:
val = self.observations[sample][var]["value"]
# If the data type is a list or tuple we need to unpack the data
if isinstance(val, (list, tuple)):
# Combine each value with its label
for value, label in zip(self.observations[sample][var]["value"],
self.observations[sample][var]["label"]):
# Skip list of tuple data types
if not isinstance(value, tuple):
# Save one row per value-label
row = [sample, var, value, label]
csv_table.write(",".join(map(str, row)) + "\n")
# If the data type is Boolean, store as a numeric 1/0 instead of True/False
elif isinstance(val, bool):
row = [sample,
var,
int(self.observations[sample][var]["value"]),
self.observations[sample][var]["label"]]
csv_table.write(",".join(map(str, row)) + "\n")
# For all other supported data types, save one row per trait
# Assumes no unusual data types are present (possibly a bad assumption)
else:
row = [sample,
var,
self.observations[sample][var]["value"],
self.observations[sample][var]["label"]
]
csv_table.write(",".join(map(str, row)) + "\n")
with open(filename, "w") as csv_table:
# Write the header
csv_table.write(",".join(map(str, ["sample", "trait", "value", "label"])) + "\n")
# Iterate over data samples
for sample in self.observations:
# Iterate over traits for each sample
for var in self.observations[sample]:
val = self.observations[sample][var]["value"]
# If the data type is a list or tuple we need to unpack the data
if isinstance(val, (list, tuple)):
# Combine each value with its label
for value, label in zip(self.observations[sample][var]["value"],
self.observations[sample][var]["label"]):
# Skip list of tuple data types
if not isinstance(value, tuple):
# Save one row per value-label
row = [sample, var, value, label]
csv_table.write(",".join(map(str, row)) + "\n")
# If the data type is Boolean, store as a numeric 1/0 instead of True/False
elif isinstance(val, bool):
row = [sample,
var,
int(self.observations[sample][var]["value"]),
self.observations[sample][var]["label"]]
csv_table.write(",".join(map(str, row)) + "\n")
# For all other supported data types, save one row per trait
# Assumes no unusual data types are present (possibly a bad assumption)
else:
row = [sample,
var,
self.observations[sample][var]["value"],
self.observations[sample][var]["label"]
]
csv_table.write(",".join(map(str, row)) + "\n")

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