## Where communities thrive

• Join over 1.5M+ people
• Join over 100K+ communities
• Free without limits
##### Activity
Henry Schreiner
@henryiii
It’s easy without a decorator, though.
class NiceFunction:
def __init__(self, function):
self.func = function
def __repr__(self):
return f"Nice repr function of {self.func.__name__}"
def __call__(self, *args, **kargs):
return self.func(*args, **kargs)
def nice_repr(func):
return NiceFunction(func)
def f(x: float):
'Squares a float'
return x**2
ff = nice_repr(f)
f(3)
9
f
<function __main__.f(x: float)>
import pickle
pickle.dumps(ff)
b'\x80\x03c__main__\nNiceFunction\nq\x00)\x81q\x01}q\x02X\x04\x00\x00\x00funcq\x03c__main__\nf\nq\x04sb.'
Tai Sakuma
@TaiSakuma
Yes. That is true. Because this is possible, I thought it should be possible to do with a decorateor, which is just equivallent to do f = nice_repr(f).
But that seems to be actually impossible.
Luke Kreczko
@kreczko

OK, it might be simple, but I cannot see it atm.

I am looking at a dictionary of generators and want to unpack the values. The straight-forward way is to unpack the generators first and then match them up against against the keys (AFAIK the order is preserved). However, this uses two for-loops and an additional dictionary.

Is there an easy way to shorten this?

generators = dict(
t1 = range(0, 20, 2),
t2 = range(10),
t3 = range(0, 100, 10),
)
for g in six.moves.zip(*six.itervalues(generators)):
data = {}
for name, value in six.moves.zip(generators, g):
data[name] = value
print(data)
# desired output per iteration:
# {'t1':0, 't2': 0, 't3': 0}
# {'t1':2, 't2': 1, 't3': 10}
# ...
Luke Kreczko
@kreczko
In the real example the generator is quite I/O heavy so I do not want to have the full range at once
Luke Kreczko
@kreczko
thx @benkrikler - having a intermediate function as a generator (i.e. yield {name: value}) does the job without much additional time
Henry Schreiner
@henryiii
You can make it look a little shorter, but the main way to reduce the output would be to have a generator in the middle:
def iter_dict(gen):
for g in six.moves.zip(*six.itervalues(gen)):
data = {name:value for name, value in six.moves.zip(gen, g)}
yield data

for item in iter_dict(generators):
print(item)
Luke Kreczko
@kreczko
thx @henryiii !
benkrikler
@benkrikler

(Copying here from our other communication, @kreczko): I think a generator comprehension in modern python could also work nicely:

from  six.moves import zip as s_zip
out_gen = (dict(s_zip(generators, g)) for g in s_zip(*six.itervalues(generators)))

Not likely faster than Henry's code, but a bit less code.

Henry Schreiner
@henryiii
Fun qoute from the “What will not change in Python 3” PEP from many years ago: "Simple is better than complex. This idea extends to the parser. Restricting Python's grammar to an LL(1) parser is a blessing, not a curse. It puts us in handcuffs that prevent us from going overboard and ending up with funky grammar rules like some other dynamic languages that will go unnamed, such as Perl."
The same document claims "There will be no alternative binding operators such as :=.” - as you may know, that’s no longer true, Python 3.8 will have := (along with shared memory multiprocessing and positional-only parameters)
And the f-strings with = sounds very useful! print(f”{1+2=}") prints 1+2=3
Henry Schreiner
@henryiii

Example of what I’d use that for:

>>> x = 23
>>> print(f"{x=}")
x=23

This imitates the similar features in Matlab or CMake that I miss in Python.

Luke Kreczko
@kreczko
Things are getting better and I prefer old rules being broken in order to introduce useful change than adamantly refuse changes
But HEP projects still have to make the move to Python 3, yet alone Py >= 3.7
Henry Schreiner
@henryiii
I’m hoping since we’ve waited so long to move in HEP, we’ll at least move to no less than Python 3.6. That’s a really fantastic version of Python.
Luke Kreczko
@kreczko
Yes, most projects I've seen are aiming for 3.6.5 - I guess because there is nothing newer on /cvmfs/sft.cern.ch/lcg/releases/Python (3.5.2, 3.6.3 & 3.6.5)
Henry Schreiner
@henryiii
3.7 is a bit harder to support on older systems, due to the minimum OpenSSL requirement (I think), I think that’s why adoption has been a little slow - Travis on Trusty doesn’t support 3.7, for example (PSA: Trusty is being retired as the Travis default this month due to EOL)
Luke Kreczko
@kreczko
I see. So things should start to improve now as many in HEP are moving to CentOS 7
Henry Schreiner
@henryiii
Or CentOS 8 - have no idea how long the building process will take, but am quite excited to have reasonably modern GCC :)
Avoiding Python 2.6 is a good reason to get off of SLC 6, though. It’s becoming really hard to support in Plumbum.
Luke Kreczko
@kreczko
@henryiii I did not realise you are the #1 maintainer of plumbum (https://github.com/tomerfiliba/plumbum/graphs/contributors)! Thx a lot! It is a very useful library
BenGalewsky
@BenGalewsky
Does anyone have experience using the arrow serialization in awkward array? I have what should be a simple example, that I cannot get to work
Albert Puig
@apuignav

Dear all,
as part of our work in zfit, we have released a standalone package for multibody phasespace generation à la TGenPhaseSpace. It's pure python, based of tensorflow. On top of simple phasespace generation, it can build more complex decay chains, handle resonances, etc, all in a very simple manner.

The package is called phasespace (https://github.com/zfit/phasespace), it's well documented and fairly easy to use, so no excuses for not trying :-)

Let us know what you think, we highly appreciate any feedback and suggestions from the software community here

Jonas+Albert

Henry Schreiner
@henryiii
Hans Dembinski
@HDembinski
Good, I guess.
For those who are forced to use Windows
Matthieu Marinangeli
@marinang
Hi, does any of you know if the "moment morphing method" described in this paper https://www.sciencedirect.com/science/article/pii/S0168900214011814 is implemented in python and outside of ROOT? This is used to interpolates pdf shape, when you want to do scans for instance for me a LLP search with different masses and lifetimes. Or maybe there is a better technique nowadays?
Hans Dembinski
@HDembinski
@marinang I don't know this method and I didn't read the paper yet, but at first glance it seems inferior to Alexander Read's interpolation https://inspirehep.net/record/501018/
Which I independently rediscovered... ten years later
For simple distributions it works very nicely
Hans Dembinski
@HDembinski
Ok, after looking into the paper, i can see that the authors claim the moment interpolation is better than the Read method
I am not aware of a Python implementation of either method
This would be a worthwhile project
Matthieu Marinangeli
@marinang
Yeah except the RooMomentMorph I haven't seen anything
A case with 1 observable and 1 morphing parameter is actually very easy to reproduce.
Nicholas Smith
@nsmith-
is that paper not describing the RooFit version? I see W. Verkerke in authors
Matthieu Marinangeli
@marinang
Yes it does, this is RooMomentMorph https://root.cern.ch/doc/master/classRooMomentMorph.html
Hans Dembinski
@HDembinski
A contributor works on ASCII display of 1D histograms for Boost.Histogram.
boostorg/histogram#74
We are discussing two different versions of the display, what is your preference?
Hans Dembinski
@HDembinski
Hey all, iminuit 1.3.7 is out! 🥳 This time really with wheels, so it is installed in a flash and you don't need a compiler. Big thanks go to @henryiii who developed the Azure Pipeline that generates the wheels, which was a big amount of work. Special thanks go to @eduardo-rodrigues who tested the packages before the release.
Luke Kreczko
@kreczko

stupid question. I have two numpy arrays, a, and b and I want to group elements in b by the unique elements in a, i.e.

a = [1, 12, 1, 50]
b = [10, 20, 30, 40]
result == [[10, 30], [20], [50]]

Is there a numpy function that does that? It seems I am missing the right keyword in my searches

Luke Kreczko
@kreczko
The current solution is using a for-loop which I would like to get rid off:
unique_a = np.unique(a)
result = []
for u in unique_a:
result.append(b[a == u].tolist())
Chris Burr
@chrisburr
@kreczko In pandas this is known as groupby:
import numpy as np
import pandas as pd
a = np.arange(10)+0.1
b = np.random.randint(4, size=10)
df = pd.DataFrame({'a': a, 'b': b})
list(df.groupby('b')['a'].apply(list))
I can't think of a numpy equivalent but I'd guess searching numpy groupby will get you there
Luke Kreczko
@kreczko
thanks @chrisburr, yes, that's what I thought at first which lead me to https://stackoverflow.com/questions/38013778/is-there-any-numpy-group-by-function which I then lead me to my for-loop solution.
pandas groupby is nice, I will need to test perfomance, I guess.
Alternatively, there is always numba
benkrikler
@benkrikler

I think awkward array might be able to help instead of pandas (not tested):

reorder = np.argsort(a)
_, counts = np.unique(a[reorder], return_counts=True)
result = awkard.JaggedArray.fromcounts(counts, b[reorder])

The only thing I'm unsure of there is the order of the unique counts, I'm assuming the unique method returns things in the order they're first seen, but I suspect that's not true.

Luke Kreczko
@kreczko
np.unique returns them sorted