Giter VIP home page Giter VIP logo

traittypes's People

Contributors

carreau avatar davidbrochart avatar ellisonbg avatar jamim avatar jasongrout avatar minrk avatar pgajdos avatar sylvaincorlay avatar toddrme2178 avatar vidartf avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

traittypes's Issues

DeprecationWarning: Sentinel is not a public part of the traitlets API. It was published by mistake, and may be removed in the future.

Empty = Sentinel('Empty', 'traittypes',

traittypes/__init__.py:1: in <module>
    from .traittypes import *
traittypes/traittypes.py:20: in <module>
    Empty = Sentinel('Empty', 'traittypes',
traitlets/__init__.py:24: in __init__
    warn(
traitlets/utils/warnings.py:11: in warn
    return warnings.warn(msg, category=category, stacklevel=stacklevel, source=source)
E   DeprecationWarning:
E               Sentinel is not a public part of the traitlets API.
E               It was published by mistake, and may be removed in the future.

nan support

if not np.array_equal(old_value, new_value):
will think arrays are not equal always when they contain a nan, the proper code might be sth like:

            try:
                return np.allclose(a, b, 0, 0, equal_nan=True)
            except (TypeError, ValueError):
                return False

There are more places around, like in ipywidgets

creating instance with Undefined is not meant to assign Undefined

In traitlets, the Undefined sentinel is meant to be a placeholder for no value having been specified. However, some traittypes tests explicitly pass Undefined as the default value, paradoxically explicitly stating that a value has not been specified.

In traitlets 4.3, this results (I think incorrectly) in the value Undefined being set as the actual value of the trait. In traitlets master, the result on accessing the trait with Undefined value as an error, saying the value is undefined.

I'm not sure what use case these tests are meant to represent. Is it important to be able to explicitly store Undefined in traits? If so, it should probably be a different sentinel than the one in traitlets intended to mean "this has no value yet, fail on attempts to access it".

Custom validation of single traittypes

While cross validators specified in the HasTraits instance with the @validate function are very flexible for a ad-hoc validation / coercion of proposed values, potentially based on the values of other trait attributes, we lack the ability to specify custom validator for a single attribute (without cross-validation).

In the case of the numpy array trait type, there are many possibilities that would be arguably natural but would be very likely bloating the TraitType specialization if implemented in the class.

Examples:

  • only accepting attributes with a certain shape
  • only accepting values that have less than a certain number of elements
  • bounds on the dimensionality of the array
  • requirements on dtypes (be a subdtype of a certain type, or exactly a certain dtype)
  • squeezing dimensions en length 1, as we do in bqplot

I propose that we implemented this via a new valid chaining method in the spirit of tag, at least in the case of numpy arrays first, and then maybe for other trait types upstream

Example:

class Foo(HasTraits):
    bar = Array([1, 2]).tag(config=True).valid(squeeze, shape(3, 2))

In the above example, valid would return the traittype instance (so that we can call tag and valid in any order), and take an arbitrary number of positional arguments, which would be the ordered sequence of custom validation for bar.

Here, squeeze would for example be the function that squeezes inputs like bqplot does shape would be a closure so that shape(3, 2) would return a function that raises a trait error if the shape is not (3, 2) etc.

Roadmap

Using this space for a provisional roadmap on traittypes

  1. to_json and from_json functions for ipywidgets with proposed implementation of javascript side counterparts.
  2. element_set / element_del events.

0.2.1: test suite is using deprecated `nose` module and pytest is failing in one unit

Looks like traittypes is one onf the only few modules still using nose wich is no longer maintained and should not be used with python 3.x https://nose.readthedocs.io/en/latest/

+ PYTHONPATH=/home/tkloczko/rpmbuild/BUILDROOT/python-traittypes-0.2.1-2.fc35.x86_64/usr/lib64/python3.8/site-packages:/home/tkloczko/rpmbuild/BUILDROOT/python-traittypes-0.2.1-2.fc35.x86_64/usr/lib/python3.8/site-packages
+ /usr/bin/pytest -ra
=========================================================================== test session starts ============================================================================
platform linux -- Python 3.8.13, pytest-7.1.1, pluggy-1.0.0
rootdir: /home/tkloczko/rpmbuild/BUILD/traittypes-0.2.1
collected 25 items / 1 error

================================================================================== ERRORS ==================================================================================
_________________________________________________________ ERROR collecting traittypes/tests/test_import_errors.py __________________________________________________________
ImportError while importing test module '/home/tkloczko/rpmbuild/BUILD/traittypes-0.2.1/traittypes/tests/test_import_errors.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/lib64/python3.8/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
traittypes/tests/test_import_errors.py:2: in <module>
    import nose.tools as nt
E   ModuleNotFoundError: No module named 'nose'
============================================================================= warnings summary =============================================================================
traittypes/traittypes.py:20
  /home/tkloczko/rpmbuild/BUILD/traittypes-0.2.1/traittypes/traittypes.py:20: DeprecationWarning:
              Sentinel is not a public part of the traitlets API.
              It was published by mistake, and may be removed in the future.

    Empty = Sentinel('Empty', 'traittypes',

../../../../../usr/lib/python3.8/site-packages/traitlets/tests/test_traitlets.py:1740
  /usr/lib/python3.8/site-packages/traitlets/tests/test_traitlets.py:1740: DeprecationWarning: Keyword `trait` is deprecated in traitlets 5.0, use `value_trait` instead
    value = Dict(trait=Unicode(),

../../../../../usr/lib/python3.8/site-packages/traitlets/tests/test_traitlets.py:1755
  /usr/lib/python3.8/site-packages/traitlets/tests/test_traitlets.py:1755: DeprecationWarning: Keyword `traits` is deprecated in traitlets 5.0, use `per_key_traits` instead
    value = Dict(traits={'foo': Int()},

../../../../../usr/lib/python3.8/site-packages/traitlets/tests/test_traitlets.py:1785
  /usr/lib/python3.8/site-packages/traitlets/tests/test_traitlets.py:1785: DeprecationWarning: Keyword `trait` is deprecated in traitlets 5.0, use `value_trait` instead
    value = Dict(trait=Unicode(),

../../../../../usr/lib/python3.8/site-packages/traitlets/tests/test_traitlets.py:1785
  /usr/lib/python3.8/site-packages/traitlets/tests/test_traitlets.py:1785: DeprecationWarning: Keyword `traits` is deprecated in traitlets 5.0, use `per_key_traits` instead
    value = Dict(trait=Unicode(),

traittypes/tests/test_traittypes.py:20
  /home/tkloczko/rpmbuild/BUILD/traittypes-0.2.1/traittypes/tests/test_traittypes.py:20: DeprecationWarning: `np.int` is a deprecated alias for the builtin `int`. To silence this warning, use `int` by itself. Doing this will not modify any behavior and is safe. When replacing `np.int`, you may wish to use e.g. `np.int64` or `np.int32` to specify the precision. If you wish to review your current use, check the release note link for additional information.
  Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
    value = Array().tag(dtype=np.int)

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
========================================================================= short test summary info ==========================================================================
ERROR traittypes/tests/test_import_errors.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
======================================================================= 6 warnings, 1 error in 0.68s =======================================================================

On migration from nose to pytest can be used https://github.com/schollii/nose2pytest/
In above output it is possible to as well see few warnings.

test_bad_values (traittypes.tests.test_traittypes.TestIntArray) fails for openSUSE

When running the testsuite with Python 3.7.* on openSUSE (logs from building) I get reliably this error:

[   32s] ======================================================================
[   32s] FAIL: test_bad_values (traittypes.tests.test_traittypes.TestIntArray)
[   32s] ----------------------------------------------------------------------
[   32s] Traceback (most recent call last):
[   32s]   File "/usr/lib/python2.7/site-packages/traitlets/tests/test_traitlets.py", line 1095, in test_bad_values
[   32s]     assert False, value
[   32s] AssertionError: [1, [0, 0]]
[   32s] 
[   32s] ----------------------------------------------------------------------
[   32s] Ran 26 tests in 0.028s

test fails on Debian/Testing with traitlets 5.0.4 with python 3.8

Hi, I'm trying to package traittypes for Debian.
I run into an error while running the test:

I: pybuild base:217: cd /build/traittypes-0.2.1/.pybuild/cpython3_3.8_traittypes/build; python3.8 -m pytest
============================= test session starts ==============================
platform linux -- Python 3.8.5, pytest-4.6.11, py-1.9.0, pluggy-0.13.0
rootdir: /build/traittypes-0.2.1
collected 3 items / 1 errors / 2 selected

==================================== ERRORS ====================================
_ ERROR collecting .pybuild/cpython3_3.8_traittypes/build/traittypes/tests/test_traittypes.py _
traittypes/tests/test_traittypes.py:9: in <module>
    from traitlets.tests.test_traitlets import TraitTestBase
<frozen importlib._bootstrap>:991: in _find_and_load
    ???
<frozen importlib._bootstrap>:975: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:655: in _load_unlocked
    ???
<frozen importlib._bootstrap>:618: in _load_backward_compatible
    ???
/usr/lib/python3/dist-packages/_pytest/assertion/rewrite.py:304: in load_module
    exec(co, mod.__dict__)
/usr/lib/python3/dist-packages/traitlets/tests/test_traitlets.py:1241: in <module>
    class UnionTraitTest(TraitTestBase):
/usr/lib/python3/dist-packages/traitlets/tests/test_traitlets.py:1243: in UnionTraitTest
    obj = UnionTrait(value='ipython_genutils.ipstruct.Struct')
/usr/lib/python3/dist-packages/traitlets/traitlets.py:1077: in __init__
    setattr(self, key, value)
/usr/lib/python3/dist-packages/traitlets/traitlets.py:604: in __set__
    self.set(obj, value)
/usr/lib/python3/dist-packages/traitlets/traitlets.py:578: in set
    new_value = self._validate(obj, value)
/usr/lib/python3/dist-packages/traitlets/traitlets.py:610: in _validate
    value = self.validate(obj, value)
/usr/lib/python3/dist-packages/traitlets/traitlets.py:1980: in validate
    self.error(obj, value)
/usr/lib/python3/dist-packages/traitlets/traitlets.py:690: in error
    raise TraitError(e)
E   traitlets.traitlets.TraitError: The 'value' trait of an UnionTrait instance expected a subclass of 'builtins.object' or a boolean, not the str 'ipython_genutils.ipstruct.Struct'.
!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!
=========================== 1 error in 0.36 seconds ============================
E: pybuild pybuild:352: test: plugin distutils failed with: exit code=2: cd /build/traittypes-0.2.1/.pybuild/cpython3_3.8_traittypes/build; python3.8 -m pytest
dh_auto_test: error: pybuild --test --test-pytest -i python{version} -p 3.8 returned exit code 13
make: *** [debian/rules:9: binary] Error 25

Now idea how to fix this.
Any help would be great.
Best

Can't use link with Array

Using link with Array for numpy array results in the exception:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

from traitlets import HasTraits, link
from traittypes import Array
import numpy as np

class Test(HasTraits):
    test = Array()
    test2 =Array()

test= Test()
link((test,"test"), (test,"test2"))
test.test =np.array([1,2,3])
traceback NOTE: I have edited the directories
Traceback (most recent call last):
  File "userdir\AppData\Local\Programs\Python\Python37\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "userdir\AppData\Local\Programs\Python\Python37\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "userdir\.vscode\extensions\ms-python.python-2022.0.1814523869\pythonFiles\lib\python\debugpy\__main__.py", line 45, in <module>
    cli.main()
  File "userdir\.vscode\extensions\ms-python.python-2022.0.1814523869\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 444, in main
    run()
  File "userdir\.vscode\extensions\ms-python.python-2022.0.1814523869\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 285, in run_file
    runpy.run_path(target_as_str, run_name=compat.force_str("__main__"))
  File "userdir\AppData\Local\Programs\Python\Python37\lib\runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "userdir\AppData\Local\Programs\Python\Python37\lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "userdir\AppData\Local\Programs\Python\Python37\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "projectdir\test.py", line 11, in <module>
    test.test =np.array([1,2,3])
  File "projectdir\.test_venv\lib\site-packages\traitlets\traitlets.py", line 606, in __set__
    self.set(obj, value)
  File "projectdir\.test_venv\lib\site-packages\traittypes\traittypes.py", line 112, in set
    obj._notify_trait(self.name, old_value, new_value)
  File "projectdir\.test_venv\lib\site-packages\traitlets\traitlets.py", line 1224, in _notify_trait
    type='change',
  File "projectdir\.test_venv\lib\site-packages\traitlets\traitlets.py", line 1229, in notify_change
    return self._notify_observers(change)
  File "projectdir\.test_venv\lib\site-packages\traitlets\traitlets.py", line 1266, in _notify_observers
    c(event)
  File "projectdir\.test_venv\lib\site-packages\traitlets\traitlets.py", line 296, in _update_target
    if getattr(self.source[0], self.source[1]) != change.new:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
pip list
Package             Version
------------------- ---------
anyio               3.4.0
argcomplete         1.12.3
argon2-cffi         21.1.0
attrs               21.2.0
Babel               2.9.1
backcall            0.2.0
bleach              4.1.0
certifi             2021.10.8
cffi                1.15.0
charset-normalizer  2.0.8
colorama            0.4.4
debugpy             1.5.1
decorator           5.1.0
defusedxml          0.7.1
entrypoints         0.3
idna                3.3
importlib-metadata  4.8.2
importlib-resources 5.4.0
ipykernel           6.6.0
ipython             7.30.0
ipython-genutils    0.2.0
ipywidgets          8.0.0a0
jedi                0.18.1
Jinja2              3.0.3
json5               0.9.6
jsonschema          4.2.1
jupyter-client      7.1.0
jupyter-core        4.9.1
jupyter-server      1.12.1
jupyterlab          3.2.4
jupyterlab-pygments 0.1.2
jupyterlab-server   2.8.2
jupyterlab-widgets  1.0.2
MarkupSafe          2.0.1
matplotlib-inline   0.1.3
mistune             0.8.4
nbclassic           0.3.4
nbclient            0.5.9
nbconvert           6.3.0
nbformat            5.1.3
nest-asyncio        1.5.4
notebook            6.4.6
numpy               1.21.5
packaging           21.3
pandocfilters       1.5.0
parso               0.8.3
pickleshare         0.7.5
pip                 21.3.1
prometheus-client   0.12.0
prompt-toolkit      3.0.23
pycparser           2.21
Pygments            2.10.0
pyparsing           3.0.6
pyrsistent          0.18.0
python-dateutil     2.8.2
pytz                2021.3
pywin32             302
pywinpty            1.1.6
pyzmq               22.3.0
requests            2.26.0
Send2Trash          1.8.0
setuptools          59.4.0
six                 1.16.0
sniffio             1.2.0
terminado           0.12.1
testpath            0.5.0
tornado             6.1
traitlets           5.1.1
traittypes          0.2.1
typing_extensions   4.0.1
urllib3             1.26.7
wcwidth             0.2.5
webencodings        0.5.1
websocket-client    1.2.1
widgetsnbextension  3.5.2
zipp                3.6.0

ValueError: Can't clean for JSON

I'm using DataFrame from Traittypes and I'm getting the error ValueError: Can't clean for JSON. I'm showing in the output that I have a normal dataframe df1... not sure what could be causing the issue?
image

Serializers for ndarrays

cc @jasongrout @vidartf @SylvainCorlay
I want to transfer some ndarray from python to js (python side numpy, js side scijs/ndarray). It would be nice to have a js package for this, as well as a place to put the python code.

The python code would make sense to put here I think, but also the js code (fan of monorepos!).

We (/me, @vidartf and @SylvainCorlay) also discussed using arrow to transfer tensors, which would make it easier for kernel backends in for instance c++ to handle more complex tensors.

However, I think simply transferring a memory buffer, the shape and dtype (maybe strides, but C_CONTIGUOUS is fine with me first) will handle >80% of the cases.

So is this repo the place to put a js package (name suggesions welcome, jupyter-ndarray-serialize?)

Is this repository still maintained?

I see that there has not been activities for 3 years when I stopped by to report an issue. What is the status of this package? Hope you can advise. Thanks!

Please Add LICENSE

This request is part of the JupyterCon Sprint Task to Add LICENSE files to all Jupyter repos.

DeprecationWarning

dtype should not be instantiated here but as a .tag(dtype=...)

default_value=default_value, allow_none=allow_none, dtype=dtype, **kwargs)

PR could be pretty direct but I am not sure about the implications of such changes...

PIL Image

Does it make sense to have a PIL Image traittype in here? If so, it would add a dependency. Or it could be an optional dependency.

There is no such thing in just traitlets as Sentinel

…it actually was/is in traitlets.utils.sentinel for a long time being, so the correct curated syntax, taken from line 4 would be (forgive my ignorance, but how 0.2.x releases were being made, so it has slipped through?):
from traitlets.utils.sentinel import Sentinel

Release 0.2.0

@vidartf I could check that the latest changes don't cause issues with xtensor.

Are you fine with me releasing or do you want to squeeze anything else before?

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.