-
Notifications
You must be signed in to change notification settings - Fork 14
Python closure sampling (with the only python commondata branch) #1660
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
Changes from all commits
a331e04
beca837
e192f80
7e465c5
e4c8097
61a9889
7bf9018
d9c2499
c2af92b
19d1f63
0a7442d
c1e5a16
b2e3a2c
ab0ca4c
c4dc48a
cd75cc7
33d2ce9
9a859b5
eb438f9
2c1abaa
2c7f128
16760bf
9b8f5bc
a1f9689
d782d7a
0ce9470
66821be
6e86c5a
bd6be97
a8d2055
f2a9624
7e827c6
3795663
cae6083
cc868fb
2af5f39
40e6dc6
68f389c
3fb3454
ab60f9d
b4177bb
af4b97d
3e8161e
bc056bf
1f61dfc
167cbe2
804ebca
9025c95
a4e6b39
d5fb7a0
5c2aaec
0da9735
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| """ | ||
| This module contains functions to write commondata and systypes | ||
| tables to files | ||
| """ | ||
|
|
||
| def write_commondata_data(commondata, buffer): | ||
| """ | ||
| write commondata table to buffer, this can be a memory map, | ||
| compressed archive or strings (using for instance StringIO) | ||
|
|
||
|
|
||
| Parameters | ||
| ---------- | ||
|
|
||
| commondata : validphys.coredata.CommonData | ||
|
|
||
| buffer : memory map, compressed archive or strings | ||
| example: StringIO object | ||
|
|
||
|
|
||
| Example | ||
| ------- | ||
| >>> from validphys.loader import Loader | ||
| >>> from io import StringIO | ||
|
|
||
| >>> l = Loader() | ||
| >>> cd = l.check_commondata("NMC").load_commondata_instance() | ||
| >>> sio = StringIO() | ||
| >>> write_commondata_data(cd,sio) | ||
| >>> print(sio.getvalue()) | ||
|
|
||
| """ | ||
| header = f"{commondata.setname} {commondata.nsys} {commondata.ndata}\n" | ||
| buffer.write(header) | ||
| commondata.commondata_table.to_csv(buffer, sep="\t", header=None) | ||
|
|
||
|
|
||
| def write_commondata_to_file(commondata, path): | ||
| """ | ||
| write commondata table to file | ||
| """ | ||
| with open(path, "w") as file: | ||
| write_commondata_data(commondata, file) | ||
|
|
||
|
|
||
| def write_systype_data(commondata, buffer): | ||
| """ | ||
| write systype table to buffer, this can be a memory map, | ||
| compressed archive or strings (using for instance StringIO) | ||
|
|
||
|
|
||
| Parameters | ||
| ---------- | ||
|
|
||
| commondata : validphys.coredata.CommonData | ||
|
|
||
| buffer : memory map, compressed archive or strings | ||
| example: StringIO object | ||
|
|
||
|
|
||
| Example | ||
| ------- | ||
| >>> from validphys.loader import Loader | ||
| >>> from io import StringIO | ||
|
|
||
| >>> l = Loader() | ||
| >>> cd = l.check_commondata("NMC").load_commondata_instance() | ||
| >>> sio = StringIO() | ||
| >>> write_systype_data(cd,sio) | ||
| >>> print(sio.getvalue()) | ||
|
|
||
| """ | ||
| header = f"{commondata.nsys}\n" | ||
| buffer.write(header) | ||
| commondata.systype_table.to_csv(buffer, sep="\t", header=None) | ||
|
|
||
|
|
||
| def write_systype_to_file(commondata, path): | ||
| """ | ||
| write systype table to file | ||
| """ | ||
| with open(path, "w") as file: | ||
| write_systype_data(commondata, file) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,7 @@ | |
| import dataclasses | ||
| import numpy as np | ||
| import pandas as pd | ||
|
|
||
| from validphys.commondatawriter import write_systype_to_file, write_commondata_to_file | ||
| KIN_NAMES = ["kin1", "kin2", "kin3"] | ||
|
|
||
|
|
||
|
|
@@ -295,6 +295,11 @@ def get_kintable(self): | |
| def central_values(self): | ||
| return self.commondata_table["data"] | ||
|
|
||
| def with_central_value(self, cv): | ||
| tb = self.commondata_table.copy() | ||
| tb["data"] = cv | ||
| return dataclasses.replace(self, commondata_table=tb) | ||
|
|
||
| def get_cv(self): | ||
| return self.central_values.values | ||
|
|
||
|
|
@@ -364,21 +369,18 @@ def systematic_errors(self, central_values=None): | |
| converted_mult_errors = self.multiplicative_errors * central_values[:, np.newaxis] / 100 | ||
| return pd.concat((self.additive_errors, converted_mult_errors), axis=1) | ||
|
|
||
|
|
||
| def export(self, path): | ||
| """Export the data, and error types | ||
| Use the same format as libNNPDF: | ||
| Use the same format as libNNPDF: | ||
|
|
||
| - A DATA_<dataset>.dat file with the dataframe of accepted points | ||
| - A systypes/STYPES_<dataset>.dat file with the error types | ||
| """ | ||
|
|
||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd rather have all the And since they are the only part of
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I disagree that they don't belong in the same place. The place I would look for write functions is the same where the read functios are. Besides, unless we have the write functions in coredata.py its not going to be trivial to avoid circular imports.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. None of the lines added to
If there is a file called
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reader functions need to import the data definitions they are reading into. Also I don't think there is anything wrong with circular imports in this case that justifies adding files with ten lines worth of code (not that it would avoid them, as said).
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Which reader functions? I think you are thinking of a different thing?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was answering to
saying that the functions that take files and return data structures do need to import the data structure, and there would be a circular import if you want a convenience method to read.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, but I really cannot see which of the lines added to There are no reads methods in the changes introduced by this Pull Request. I just want to have a edit: i just tried it out and the tests pass without a circular import |
||
| dat_path = path / f"DATA_{self.setname}.dat" | ||
| sys_path = path / "systypes" / f"SYSTYPE_{self.setname}_DEFAULT.dat" | ||
| sys_path.parent.mkdir(exist_ok=True) | ||
|
|
||
| dat_string_raw = self.commondata_table.to_string(index=False, header=False, float_format="{:.8e}".format) | ||
| header = f"{self.setname} {self.nsys} {self.ndata}" | ||
| dat_string = "\n".join([f" {i+1} {r}" for i, r in enumerate(dat_string_raw.split("\n"))]) | ||
| dat_path.write_text(f"{header}\n{dat_string}\n") | ||
|
|
||
| sys_raw = self.systype_table.to_string(index=True, header=False, index_names=False) | ||
| sys_path.write_text(f"{self.nsys}\n{sys_raw}\n") | ||
| write_systype_to_file(self, sys_path) | ||
| write_commondata_to_file(self, dat_path) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this mean the format is no longer the same as libNNPDF and so they are not compatible?