Giter VIP home page Giter VIP logo

Comments (5)

Cameron-Van-Eck avatar Cameron-Van-Eck commented on August 10, 2024 1

For whatever reason, I can't reproduce the behaviour on my own machine (I can reproduce @hfhsieh's example, but not within RM-Tools). But since a solution has been suggested, I've implemented it into the JSON-saving code in rmsynth1d, rmclean1d, and qufit. Could someone please try the updated version (47e8915) and confirm that this fixes the problem?

Thanks,
Cameron

from rm-tools.

Cameron-Van-Eck avatar Cameron-Van-Eck commented on August 10, 2024

Hi Ruolan:

Can you send me the data you were running that generated this error, and the settings you used? I'd like to recreate it so that I can better understand where it came from (so as to avoid similar problems in the future).

Thanks,
Cameron

from rm-tools.

AlecThomson avatar AlecThomson commented on August 10, 2024

@Cameron-Van-Eck - this has bitten me in SPICE-RACS as well. Although, in my case the JSON came from my database format, rather than writing to disk. It's strange we haven't seen this issue earlier. If it's helpful, it can be nice to use the numpy primitives to capture all float (or whatever) types. Here's an example from the SPICE-RACS pipeline:

    # Ensure JSON serializable
    for k, v in mDict.items():
        if isinstance(v, np.float_):
            mDict[k] = float(v)
        elif isinstance(v, np.int_):
            mDict[k] = int(v)
        elif isinstance(v, np.ndarray):
            mDict[k] = v.tolist()
        elif isinstance(v, np.bool_):
            mDict[k] = bool(v)

    do_RMsynth_1D.saveOutput(mDict, aDict, prefix, rm_verbose)

P.S. This can also be handled with a custom JSON encoder - but I think the amount of work is pretty much equivalent

from rm-tools.

hfhsieh avatar hfhsieh commented on August 10, 2024

@Cameron-Van-Eck I think it is because the built-in json package supports the Python basic type only, not all the numpy datatype. The example below can reproduce the this issue

import json
import numpy as np

for dtype in [np.int8, np.int16, np.int32, np.int64, np.int_]:  # np.int_ == np.int64
#for dtype in [np.float16, np.float32, np.float64, np.float128, np.float_]:    # np.float_ == np.float64
    try:
        json.dump(dtype(np.pi), open("foo", "w"))  # numeric
#        json.dump({"key": dtype(np.pi)}, open("foo", "w"))  # dict

        print("{} passed".format(dtype))

    except TypeError:
        print("{} failed".format(dtype))
#        raise

For integer variables (and values in dict), only numpy.int64 passed for Python 2.7.16, and none for Python 3.10.8. For floating number, only numpy.float64 passed for both Python 2 and 3.

I am not sure why the recipe in the SPICE-RACS pipeline works since this issue occurs when not isinstance(v, np.int_) and not isinstance(v, np.float_)

from rm-tools.

RLJin avatar RLJin commented on August 10, 2024

Can you send me the data you were running that generated this error, and the settings you used? I'd like to recreate it so that I can better understand where it came from (so as to avoid similar problems in the future).

@Cameron-Van-Eck

It was just the sample data (Soucre1.dat created using 'mk_test_ascii_data.py') you provided. XD

Ruolan

from rm-tools.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.