Skip to content

Conversation

HaleySchuhl
Copy link
Contributor

@HaleySchuhl HaleySchuhl commented May 5, 2025

Describe your changes
cast morphology angles to float64 so can be dumped to json

Type of update
Is this a:

  • Bug fix

Associated issues
Discovered since the save_results step at the end of the morphology tutorial started failing. Error message below:

Additional context
Add any other context about the problem here.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[33], line 4
      1 # Inputs:
      2 #   filename  = filename for saving results
      3 #   outformat = output file format: "json" (default) hierarchical format or "csv" tabular format
----> 4 pcv.outputs.save_results(filename=args.result, outformat="json")

File [~/Documents/GitHub/plantcv/build/__editable__.plantcv-4.8.dev10](http://localhost:8888/lab/workspaces/auto-3/tree/Projects/tutorials/~/Documents/GitHub/plantcv/build/__editable__.plantcv-4.8.dev10)+g6eebf6ab-py3-none-any[/plantcv/plantcv/classes.py:188](http://localhost:8888/plantcv/plantcv/classes.py#line=187), in Outputs.save_results(self, filename, outformat)
    186         hierarchical_data = {"metadata": self.metadata, "observations": self.observations}
    187     with open(filename, mode='w') as f:
--> 188         json.dump(hierarchical_data, f)
    190 elif outformat.upper() == "CSV":
    191     # Open output CSV file
    192     with open(filename, "w") as csv_table:
    193         # Gather any additional metadata

File [~/miniforge3/envs/plantcv_test/lib/python3.11/json/__init__.py:179](http://localhost:8888/lab/workspaces/auto-3/tree/Projects/tutorials/~/miniforge3/envs/plantcv_test/lib/python3.11/json/__init__.py#line=178), in dump(obj, fp, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, default, sort_keys, **kw)
    173     iterable = cls(skipkeys=skipkeys, ensure_ascii=ensure_ascii,
    174         check_circular=check_circular, allow_nan=allow_nan, indent=indent,
    175         separators=separators,
    176         default=default, sort_keys=sort_keys, **kw).iterencode(obj)
    177 # could accelerate with writelines in some versions of Python, at
    178 # a debuggability cost
--> 179 for chunk in iterable:
    180     fp.write(chunk)

File [~/miniforge3/envs/plantcv_test/lib/python3.11/json/encoder.py:432](http://localhost:8888/lab/workspaces/auto-3/tree/Projects/tutorials/~/miniforge3/envs/plantcv_test/lib/python3.11/json/encoder.py#line=431), in _make_iterencode.<locals>._iterencode(o, _current_indent_level)
    430     yield from _iterencode_list(o, _current_indent_level)
    431 elif isinstance(o, dict):
--> 432     yield from _iterencode_dict(o, _current_indent_level)
    433 else:
    434     if markers is not None:

File [~/miniforge3/envs/plantcv_test/lib/python3.11/json/encoder.py:406](http://localhost:8888/lab/workspaces/auto-3/tree/Projects/tutorials/~/miniforge3/envs/plantcv_test/lib/python3.11/json/encoder.py#line=405), in _make_iterencode.<locals>._iterencode_dict(dct, _current_indent_level)
    404         else:
    405             chunks = _iterencode(value, _current_indent_level)
--> 406         yield from chunks
    407 if newline_indent is not None:
    408     _current_indent_level -= 1

File [~/miniforge3/envs/plantcv_test/lib/python3.11/json/encoder.py:406](http://localhost:8888/lab/workspaces/auto-3/tree/Projects/tutorials/~/miniforge3/envs/plantcv_test/lib/python3.11/json/encoder.py#line=405), in _make_iterencode.<locals>._iterencode_dict(dct, _current_indent_level)
    404         else:
    405             chunks = _iterencode(value, _current_indent_level)
--> 406         yield from chunks
    407 if newline_indent is not None:
    408     _current_indent_level -= 1

    [... skipping similar frames: _make_iterencode.<locals>._iterencode_dict at line 406 (1 times)]

File [~/miniforge3/envs/plantcv_test/lib/python3.11/json/encoder.py:406](http://localhost:8888/lab/workspaces/auto-3/tree/Projects/tutorials/~/miniforge3/envs/plantcv_test/lib/python3.11/json/encoder.py#line=405), in _make_iterencode.<locals>._iterencode_dict(dct, _current_indent_level)
    404         else:
    405             chunks = _iterencode(value, _current_indent_level)
--> 406         yield from chunks
    407 if newline_indent is not None:
    408     _current_indent_level -= 1

File [~/miniforge3/envs/plantcv_test/lib/python3.11/json/encoder.py:326](http://localhost:8888/lab/workspaces/auto-3/tree/Projects/tutorials/~/miniforge3/envs/plantcv_test/lib/python3.11/json/encoder.py#line=325), in _make_iterencode.<locals>._iterencode_list(lst, _current_indent_level)
    324         else:
    325             chunks = _iterencode(value, _current_indent_level)
--> 326         yield from chunks
    327 if newline_indent is not None:
    328     _current_indent_level -= 1

File [~/miniforge3/envs/plantcv_test/lib/python3.11/json/encoder.py:439](http://localhost:8888/lab/workspaces/auto-3/tree/Projects/tutorials/~/miniforge3/envs/plantcv_test/lib/python3.11/json/encoder.py#line=438), in _make_iterencode.<locals>._iterencode(o, _current_indent_level)
    437         raise ValueError("Circular reference detected")
    438     markers[markerid] = o
--> 439 o = _default(o)
    440 yield from _iterencode(o, _current_indent_level)
    441 if markers is not None:

File [~/miniforge3/envs/plantcv_test/lib/python3.11/json/encoder.py:180](http://localhost:8888/lab/workspaces/auto-3/tree/Projects/tutorials/~/miniforge3/envs/plantcv_test/lib/python3.11/json/encoder.py#line=179), in JSONEncoder.default(self, o)
    161 def default(self, o):
    162     """Implement this method in a subclass such that it returns
    163     a serializable object for ``o``, or calls the base implementation
    164     (to raise a ``TypeError``).
   (...)    178 
    179     """
--> 180     raise TypeError(f'Object of type {o.__class__.__name__} '
    181                     f'is not JSON serializable')

TypeError: Object of type float32 is not JSON serializable

For the reviewer
See this page for instructions on how to review the pull request.

  • PR functionality reviewed in a Jupyter Notebook
  • All tests pass
  • Test coverage remains 100%
  • Documentation tested
  • New documentation pages added to plantcv/mkdocs.yml
  • Changes to function input/output signatures added to updating.md
  • Code reviewed
  • PR approved

@HaleySchuhl HaleySchuhl added this to the PlantCV v4.8 milestone May 5, 2025
@HaleySchuhl HaleySchuhl self-assigned this May 5, 2025
@HaleySchuhl HaleySchuhl added the bugfix Bug fixes label May 5, 2025
@HaleySchuhl HaleySchuhl added the update Updates an existing feature/method label May 5, 2025
@github-project-automation github-project-automation bot moved this to Pull Requests in PlantCV4 May 5, 2025
Copy link

deepsource-io bot commented May 5, 2025

Here's the code health analysis summary for commits eb7e30c..13e8603. View details on DeepSource ↗.

Analysis Summary

AnalyzerStatusSummaryLink
DeepSource Python LogoPython✅ SuccessView Check ↗
DeepSource Test coverage LogoTest coverage✅ SuccessView Check ↗

Code Coverage Report

MetricAggregatePython
Branch Coverage100%100%
Composite Coverage100%100%
Line Coverage100%100%
New Branch Coverage100%100%
New Composite Coverage100%100%
New Line Coverage100%, ✅ Above Threshold100%, ✅ Above Threshold

💡 If you’re a repository administrator, you can configure the quality gates from the settings.

@k034b363 k034b363 self-requested a review May 6, 2025 14:31
Copy link
Contributor

@k034b363 k034b363 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was able to replicate the error while on the main branch and resolve it on this branch. I checked to make sure the outputs match after the conversion and everything looks good.

@nfahlgren nfahlgren merged commit 5485b5f into main May 8, 2025
5 checks passed
@github-project-automation github-project-automation bot moved this from Pull Requests to Done in PlantCV4 May 8, 2025
@nfahlgren nfahlgren deleted the float32_segment_angles_bugfix branch May 8, 2025 14:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugfix Bug fixes ready to review update Updates an existing feature/method
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants