Giter VIP home page Giter VIP logo

ilaff's Introduction

ILAFF

Prototype of library for statistical analysis and fitting of lattice data

Installation

Should be as simple as:

pip install -e .

ilaff's People

Contributors

achambers8472 avatar finnstokes avatar robertjperry avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

ilaff's Issues

Quantity comparisons

Equality conditions on Quantity's don't work for numpy arrays of quantities.

Minimal working example to reproduce is below

from ilaff import units
import numpy as np
#A single. This works                                                                                                  
b = 1 * units.fm
if b == b:
    print('Singleton works')
#A numpy single. This works                                                                                            
b = np.arange(0,1) * units.fm    #i.e. np.array([0])*units.fm                                                          
if b == b:
    print('Numpy singleton works')
#A list of quantities. This works                                                                                                                                                                                  
b = [1*units.fm]*2
if b == b:
    print('List of quantities works')
#A nump array. This doesn't work                                                                                       
b = np.arange(0,2) * units.fm    #i.e. np.array([0,1])*units.fm                                                        
if b == b:
    print('Numpy array doesnt work')
#Error is                                                                                                              
#ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()               

I believe the desired/expected behaviour is all(). This is a simple change on the face of it, change i.e.
return self.value == other.value however this breaks the first case considered. Looking around, the two solutions to test if something is an iterable (and hence eligible for all() ) are

def iterable(obj):
    try:
        iter(obj)
    except Exception:
        return False
    else:
        return True

Or

from collections.abc import Iterable   # drop `.abc` with Python 2.7 or lower
def iterable(obj):
    return isinstance(obj, Iterable)

where apparently the first is preferred. Does anyone else have thoughts about this? Or is this something that iterably will fix @FinnStokes ?

numpy.std causes an error with Quantity class

Calculating the mean with numpy works as expected,

>>> lat=units.Lattice()
>>> test=np.array([1,2,3,4])*units.a(lat)
>>> np.mean(test)
Quantity(value=2.5, dimension=Dimension(mass_dim=-1), scale=Lattice())

But the standard deviation does not.

>>> np.std(test)
AttributeError: 'Quantity' object has no attribute 'conjugate'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<__array_function__ internals>", line 5, in std
  File "/home/rperry/anaconda2/envs/py38/lib/python3.8/site-packages/numpy/core/fromnumeric.py", line 3496, in std
    return _methods._std(a, axis=axis, dtype=dtype, out=out, ddof=ddof,
  File "/home/rperry/anaconda2/envs/py38/lib/python3.8/site-packages/numpy/core/_methods.py", line 233, in _std
    ret = _var(a, axis=axis, dtype=dtype, out=out, ddof=ddof,
  File "/home/rperry/anaconda2/envs/py38/lib/python3.8/site-packages/numpy/core/_methods.py", line 214, in _var
    x = um.multiply(x, um.conjugate(x), out=x).real
TypeError: loop of ufunc does not support argument 0 of type Quantity which has no callable conjugate method

Support numpy ufuncs for Quantities

Implement proper support for numpy's ufunc architecture by implementing __array_ufunc__. This would replace the current __add__, __mul__, etc implementations, and additionally allow using ufuncs like sin, exp, etc for unitless quantities.

License

What license do we want to use?

Support indexing and iterability of quantities

Implement __len__, __getitem__, __iter__, and __reversed__ on Quantity. The implementations of these methods should call the method on the wrapped value and wrap the returned value (or each yielded value) in a Quantity that matches the units of self. This allows for indexing and iteration of wrapped arrays.

Renaming classes in units

I know Rob was uncomfortable with the current class names in the units module, and I ran into some awkwardness when writing the documentation. Here is my proposed alternative:

  • Quantity is the main class (previously called Value). Each quantity has:
    • a value (which is a float, numpy array or similar),
    • a Dimension
    • a Scale
  • Dimension simply tracks the mass dimension of the quantity as an integer, though it could be generalised to include fractional mass dimensions other dimensionalities for to allow e.g. SI units
  • Scale indicates how the physical scale of the quantities is set, current options are:
    • Physical quantites are stored in terms of powers of GeV and are compatible with all other physical quantities (hence, all instances of Physical compare equal)
    • Lattice quantities are stored in terms of powers of the lattice spacing and are only compatible with qunatities defined on the same lattice (corresponding to an instance of the Lattice class, hence different instances of Lattice do not compare equal)
  • units are simply specific quantities that correspond to some unit of measure. Currently we define
    • fm, GeV, MeV are constants corresponding to SI units with a Physical scale
    • a is a method that returns a unit corresponding to the lattice spacing in a given Lattice scale

What do people think about this convention? Does it resolve your issues Rob?

Documentation

We should generate documentation from the docstrings in our code. The most popular option seems to be Sphinx, but there is also pdoc, which is apparently easier to set up for simpler projects. I think given the build infrasructure we already have set up, Sphinx shouldn't be too much extra burden.

I lean towards using Google or NumPy-style docstrings, which would mean using the inbuilt Napoleon extension if we use Sphinx.

Once we get it set up, it would be good to add the documentation generation to the CI and upload as build artifacts and/or push to GitHub Pages or Read the Docs

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.