Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saving new measurement #147

Open
dlanci opened this issue Mar 2, 2021 · 11 comments
Open

Saving new measurement #147

dlanci opened this issue Mar 2, 2021 · 11 comments

Comments

@dlanci
Copy link

dlanci commented Mar 2, 2021

Dear mantainers,

Apologies in advance if what I'm about to ask is detailed in some guide but I couldn't find it anywhere. I would like to create a new measurement instance modifying one of the existing ones in flavio. For example, for the relevant observables I want to create an arbitrary Likelihood:

obs_space=np.linspace(0.5,1.5,1001)
logL=normal_logpdf(obs_space, 1., 0.04)
plt.plot(obs_space,np.exp(logL-logL.max()))

Then from the file measurements_test.yml that contains a dictionary that is the copy of the observable I want to edit I do:

with open('/path/to/flavio/data/measurements_test.yml') as file:

    test = yaml.load(file, Loader=yaml.FullLoader)

test2=copy.deepcopy(test)
test2['observable']['values'][0]['value']['y']=np.exp(logL)

When I want to save I do:

flavio.measurements.write_file('/path/to/flavio/data/measurements_test_1.yml',test2)

And I obtain a key error

KeyError: 'observable'

What am I missing in the definition of the measurement instance with these steps?

Thanks

@dlanci
Copy link
Author

dlanci commented Mar 2, 2021

If I run

flavio.measurements.read_file('/path/to/flavio/data/measurements_test.yml')

It outputs a list with the name of the observable as only item

['observable']

@dlanci
Copy link
Author

dlanci commented Mar 2, 2021

Ok, but then how can I edit/update the measured Likelihood for the relevant observable with the values I want to assign? i.e. how can I edit the dictionary and dump it back to the file like in:

flavio.measurements.read_file('/path/to/flavio/data/load_measurements_to_edit.yml') #this instantiate the measurements

dict['values'][0]['value']['y']=np.exp(logL) #this step would be what I'd need

flavio.measurements.write_file('/path/to/flavio/data/dump_for_edited_measurements.yml', dict)

@dlanci
Copy link
Author

dlanci commented Mar 2, 2021

Thanks. So what I now do is:

#get the measurement to edit
with open('/path/to/flavio/data/measurements_test.yml') as file:

    test = yaml.load(file, Loader=yaml.FullLoader)

#edit the likelihood y
test2=copy.deepcopy(test)
test2['observable']['values'][0]['value']['y']=np.exp(logL)

#create a measurement list  with the edited yaml dict
from flavio.classes import Measurement
measurements = [Measurement.from_yaml_dict(test2, pname='observables')]

And I obtain

KeyError: 'constraints'

I attach for clarity the measurements_test.yml I'm trying to edit

LHCb RK Example:
  experiment: LHCb
  values:
    - name: <Rmue>(B+->Kll)
      q2min: 1.1
      q2max: 6.0
      value:
        distribution: numerical
        x: [0.5  , 1.5] #dummy values
        y: [1.0  , 1.0]

@dlanci
Copy link
Author

dlanci commented Mar 2, 2021

It's ok and actually with a simple edit I could make it work. For now I just dump the modified measurement with yaml and edited this line https://github.com/flav-io/flavio/blob/master/flavio/measurements.py#L14 to:

measurements = yaml.load(obj, Loader=yaml.Loader)

And it works just fine. Thank you!

@peterstangl
Copy link
Collaborator

(Arguably, this is not an elegant implementation. It would make more sense to make pname a class attribute with value "parameters" on the base class and "observables" on the measurement class. @peterstangl?)

Yes, good point! I think in general loading and saving of measurements can be improved and I will keep this issue open as a reminder for that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants
@peterstangl @dlanci and others