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

Sort before interpolating #61

Merged
merged 1 commit into from
Jun 20, 2024
Merged

Sort before interpolating #61

merged 1 commit into from
Jun 20, 2024

Conversation

zihaoxu98
Copy link
Contributor

In np.interp it is presumed that xs is sorted. This PR ensures that the xs is sorted before apply interpolation.

This solves the randomness issue in XENONnT/fuse#230. Here is the explanation:

  • If in straxen.URLconfig, '&sort=xxxx' is used, then in the MongoDB interface of rframe, it sends $sort: xxxx to MongoDB. However, if xxxx is not sufficient enough to determine the order due to duplicated values, the output of MongoDB will be random, meaning that xxxx is sorted but within each value of xxxx it's random. See the sort consistency section in the document.
  • This randomness can finally propagate to the interpolation. For example, if we require '&sort=xxxx', the time can be randomly unsorted. But this conflicts with the presumption of np.interp, which finally causes randomness of the interpolated output.

An MWE is

import xedocs
import datetime

accessor = xedocs.databases.straxen_db()['pmt_area_to_pes']
datasource = accessor.schema.default_datasource()
labels = {'detector': 'tpc', 'run_id': '046000', 'version': 'v10'}
query = accessor.schema.compile_query(accessor.storage, **labels)
index = query.index.indexes[2]

x = [{'version': 'v10',
      'created_date': datetime.datetime(2023, 10, 24, 15, 40, 47, 945000),
      'comments': '',
      'time': datetime.datetime(2022, 7, 8, 8, 46, 56),
      'detector': 'tpc',
      'pmt': 0,
      'value': 0.0079474222},
     {'version': 'v10',
      'created_date': datetime.datetime(2023, 10, 24, 15, 40, 47, 945000),
      'comments': '',
      'time': datetime.datetime(2022, 7, 19, 15, 48, 16),
      'detector': 'tpc',
      'pmt': 0,
      'value': 0.0079188448}]

y = [{'version': 'v10',
      'created_date': datetime.datetime(2023, 10, 24, 15, 40, 47, 945000),
      'comments': '',
      'time': datetime.datetime(2022, 7, 19, 15, 48, 16),
      'detector': 'tpc',
      'pmt': 0,
      'value': 0.0079188448},
     {'version': 'v10',
      'created_date': datetime.datetime(2023, 10, 24, 15, 40, 47, 945000),
      'comments': '',
      'time': datetime.datetime(2022, 7, 8, 8, 46, 56),
      'detector': 'tpc',
      'pmt': 0,
      'value': 0.0079474222}]

label = datetime.datetime(2022, 7, 16, 7, 18, 14, 927000)

print(index.reduce(x, label)[0]['value'])

print(index.reduce(y, label)[0]['value'])

Before the PR it outputs:

0.007927332994063166
0.0079474222

After the PR it outputs the same values:

0.007927332994063166
0.007927332994063166

@zihaoxu98 zihaoxu98 requested a review from dachengx June 19, 2024 11:18
Copy link
Member

@jmosbacher jmosbacher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch!

@zihaoxu98 zihaoxu98 merged commit f9fcb47 into master Jun 20, 2024
4 checks passed
@zihaoxu98 zihaoxu98 deleted the sort branch June 20, 2024 13:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants