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

Config can read map and assign itself a mapping function #79

Merged
merged 20 commits into from
Jan 30, 2023

Conversation

dachengx
Copy link
Collaborator

@dachengx dachengx commented Jan 25, 2023

Users should not be required to apply interpolation functions, so we would like to make the Config class more clever. The method apply is dynamically assigned. When using points, the apply will be map_point, while using regular binning, the apply will be map_regbin. When using log-binning, we will first convert the positions to log space.

Like:

setattr(self, 'apply', self.map_point)

This will make the functionality of Config and Plugin completely de-coupled.

Finally, add a Config called SigmaMap to handle the uncertainty of efficiency maps.

Users should not be required to apply interpolation functions
"""
Map is a special config which takes input file.
The method `apply` is dynamically assigned.
When using points, the `apply` will be `map_point`,

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
W291 trailing whitespace

@@ -136,34 +143,104 @@ def build(self, llh_name: str = None):

data = load_json(self.file_path)

if data['coordinate_type'] == 'point':
coordinate_type = data['coordinate_type']
if coordinate_type == 'point' or coordinate_type == 'log_point':

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
WPS514 Found implicit in condition

self.build_point(data)
elif data['coordinate_type'] == 'regbin':
elif coordinate_type == 'regbin' or coordinate_type == 'log_regbin':

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
WPS514 Found implicit in condition

self.build_regbin(data)
else:
raise ValueError("map_type must be either 'point' or 'regbin'!")

def build_point(self, data):
"""Cache the map to jnp.array if bins_type is point"""
if data['coordinate_name'] == 'pdf' or data['coordinate_name'] == 'cdf':

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
WPS514 Found implicit in condition

self.build_regbin(data)
else:
raise ValueError("map_type must be either 'point' or 'regbin'!")

def build_point(self, data):
"""Cache the map to jnp.array if bins_type is point"""
if data['coordinate_name'] == 'pdf' or data['coordinate_name'] == 'cdf':

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
WPS204 Found overused expression: data['coordinate_name']; used 8 > 7

)
setattr(self, 'preprocess', self.log_pos)
else:
setattr(self, 'preprocess', self.linear_pos)

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
B010 Do not call setattr with a constant attribute value, it is not any safer than normal property access.

setattr(self, 'preprocess', self.log_pos)
else:
setattr(self, 'preprocess', self.linear_pos)
setattr(self, 'apply', self.map_regbin)

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
B010 Do not call setattr with a constant attribute value, it is not any safer than normal property access.

setattr(self, 'preprocess', self.linear_pos)
setattr(self, 'apply', self.map_regbin)

def map_regbin(self, pos):

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
D102 Missing docstring in public method

)
return val

def linear_pos(self, pos):

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
D102 Missing docstring in public method

def linear_pos(self, pos):
return pos

def log_pos(self, pos):

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
D102 Missing docstring in public method

@coveralls
Copy link

coveralls commented Jan 25, 2023

Pull Request Test Coverage Report for Build 4047388884

  • 112 of 141 (79.43%) changed or added relevant lines in 13 files are covered.
  • 3 unchanged lines in 3 files lost coverage.
  • Overall coverage increased (+0.02%) to 77.534%

Changes Missing Coverage Covered Lines Changed/Added Lines %
appletree/context.py 0 1 0.0%
appletree/likelihood.py 0 3 0.0%
appletree/component.py 1 10 10.0%
appletree/config.py 72 88 81.82%
Files with Coverage Reduction New Missed Lines %
appletree/component.py 1 65.77%
appletree/config.py 1 77.01%
appletree/likelihood.py 1 82.61%
Totals Coverage Status
Change from base Build 4007627283: 0.02%
Covered Lines: 1446
Relevant Lines: 1865

💛 - Coveralls

@@ -6,6 +6,8 @@

from appletree.share import _cached_configs
from appletree.utils import exporter, load_json, get_file_path, integrate_midpoint, cum_integrate_midpoint
from appletree import interpolation
from appletree.interpolation import FLOAT_POS_MIN, FLOAT_POS_MAX

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
WPS458 Found imports collision: appletree.interpolation

@@ -6,6 +6,8 @@

from appletree.share import _cached_configs
from appletree.utils import exporter, load_json, get_file_path, integrate_midpoint, cum_integrate_midpoint
from appletree import interpolation
from appletree.interpolation import FLOAT_POS_MIN, FLOAT_POS_MAX

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
WPS458 Found imports collision: appletree.interpolation

dr2 = jnp.clip(jnp.sqrt(jnp.sum((ref_pos2 - pos)**2, axis=-1)), a_min=1e-10)
dr3 = jnp.clip(jnp.sqrt(jnp.sum((ref_pos3 - pos)**2, axis=-1)), a_min=1e-10)
dr4 = jnp.clip(jnp.sqrt(jnp.sum((ref_pos4 - pos)**2, axis=-1)), a_min=1e-10)
dr1 = jnp.clip(jnp.sqrt(jnp.sum((ref_pos1 - pos)**2, axis=-1)), a_min=FLOAT_POS_MIN, a_max=FLOAT_POS_MAX)

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
WPS221 Found line with high Jones Complexity: 23 > 14

dr2 = jnp.clip(jnp.sqrt(jnp.sum((ref_pos2 - pos)**2, axis=-1)), a_min=1e-10)
dr3 = jnp.clip(jnp.sqrt(jnp.sum((ref_pos3 - pos)**2, axis=-1)), a_min=1e-10)
dr4 = jnp.clip(jnp.sqrt(jnp.sum((ref_pos4 - pos)**2, axis=-1)), a_min=1e-10)
dr1 = jnp.clip(jnp.sqrt(jnp.sum((ref_pos1 - pos)**2, axis=-1)), a_min=FLOAT_POS_MIN, a_max=FLOAT_POS_MAX)

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
E501 line too long (109 > 100 characters)

dr3 = jnp.clip(jnp.sqrt(jnp.sum((ref_pos3 - pos)**2, axis=-1)), a_min=1e-10)
dr4 = jnp.clip(jnp.sqrt(jnp.sum((ref_pos4 - pos)**2, axis=-1)), a_min=1e-10)
dr1 = jnp.clip(jnp.sqrt(jnp.sum((ref_pos1 - pos)**2, axis=-1)), a_min=FLOAT_POS_MIN, a_max=FLOAT_POS_MAX)
dr2 = jnp.clip(jnp.sqrt(jnp.sum((ref_pos2 - pos)**2, axis=-1)), a_min=FLOAT_POS_MIN, a_max=FLOAT_POS_MAX)

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
WPS221 Found line with high Jones Complexity: 23 > 14

dr3 = jnp.clip(jnp.sqrt(jnp.sum((ref_pos3 - pos)**2, axis=-1)), a_min=FLOAT_POS_MIN, a_max=FLOAT_POS_MAX)
dr4 = jnp.clip(jnp.sqrt(jnp.sum((ref_pos4 - pos)**2, axis=-1)), a_min=FLOAT_POS_MIN, a_max=FLOAT_POS_MAX)
dr5 = jnp.clip(jnp.sqrt(jnp.sum((ref_pos5 - pos)**2, axis=-1)), a_min=FLOAT_POS_MIN, a_max=FLOAT_POS_MAX)
dr6 = jnp.clip(jnp.sqrt(jnp.sum((ref_pos6 - pos)**2, axis=-1)), a_min=FLOAT_POS_MIN, a_max=FLOAT_POS_MAX)

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
E501 line too long (109 > 100 characters)

dr4 = jnp.clip(jnp.sqrt(jnp.sum((ref_pos4 - pos)**2, axis=-1)), a_min=FLOAT_POS_MIN, a_max=FLOAT_POS_MAX)
dr5 = jnp.clip(jnp.sqrt(jnp.sum((ref_pos5 - pos)**2, axis=-1)), a_min=FLOAT_POS_MIN, a_max=FLOAT_POS_MAX)
dr6 = jnp.clip(jnp.sqrt(jnp.sum((ref_pos6 - pos)**2, axis=-1)), a_min=FLOAT_POS_MIN, a_max=FLOAT_POS_MAX)
dr7 = jnp.clip(jnp.sqrt(jnp.sum((ref_pos7 - pos)**2, axis=-1)), a_min=FLOAT_POS_MIN, a_max=FLOAT_POS_MAX)

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
WPS221 Found line with high Jones Complexity: 23 > 14

dr4 = jnp.clip(jnp.sqrt(jnp.sum((ref_pos4 - pos)**2, axis=-1)), a_min=FLOAT_POS_MIN, a_max=FLOAT_POS_MAX)
dr5 = jnp.clip(jnp.sqrt(jnp.sum((ref_pos5 - pos)**2, axis=-1)), a_min=FLOAT_POS_MIN, a_max=FLOAT_POS_MAX)
dr6 = jnp.clip(jnp.sqrt(jnp.sum((ref_pos6 - pos)**2, axis=-1)), a_min=FLOAT_POS_MIN, a_max=FLOAT_POS_MAX)
dr7 = jnp.clip(jnp.sqrt(jnp.sum((ref_pos7 - pos)**2, axis=-1)), a_min=FLOAT_POS_MIN, a_max=FLOAT_POS_MAX)

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
E501 line too long (109 > 100 characters)

dr5 = jnp.clip(jnp.sqrt(jnp.sum((ref_pos5 - pos)**2, axis=-1)), a_min=FLOAT_POS_MIN, a_max=FLOAT_POS_MAX)
dr6 = jnp.clip(jnp.sqrt(jnp.sum((ref_pos6 - pos)**2, axis=-1)), a_min=FLOAT_POS_MIN, a_max=FLOAT_POS_MAX)
dr7 = jnp.clip(jnp.sqrt(jnp.sum((ref_pos7 - pos)**2, axis=-1)), a_min=FLOAT_POS_MIN, a_max=FLOAT_POS_MAX)
dr8 = jnp.clip(jnp.sqrt(jnp.sum((ref_pos8 - pos)**2, axis=-1)), a_min=FLOAT_POS_MIN, a_max=FLOAT_POS_MAX)

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
WPS221 Found line with high Jones Complexity: 23 > 14

dr5 = jnp.clip(jnp.sqrt(jnp.sum((ref_pos5 - pos)**2, axis=-1)), a_min=FLOAT_POS_MIN, a_max=FLOAT_POS_MAX)
dr6 = jnp.clip(jnp.sqrt(jnp.sum((ref_pos6 - pos)**2, axis=-1)), a_min=FLOAT_POS_MIN, a_max=FLOAT_POS_MAX)
dr7 = jnp.clip(jnp.sqrt(jnp.sum((ref_pos7 - pos)**2, axis=-1)), a_min=FLOAT_POS_MIN, a_max=FLOAT_POS_MAX)
dr8 = jnp.clip(jnp.sqrt(jnp.sum((ref_pos8 - pos)**2, axis=-1)), a_min=FLOAT_POS_MIN, a_max=FLOAT_POS_MAX)

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
E501 line too long (109 > 100 characters)

@dachengx dachengx marked this pull request as ready for review January 26, 2023 03:11
from . import nr_lyqy
from .nr_lyqy import *
from . import lyqy
from .lyqy import *

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
WPS458 Found imports collision: lyqy

def build(self, llh_name: str = None):
"""Read maps"""
if self.name in _cached_configs:
_configs = _cached_configs[self.name]

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
WPS529 Found implicit .get() dict usage

"""Read maps"""
if self.name in _cached_configs:
_configs = _cached_configs[self.name]
if isinstance(_configs, dict):

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
WPS121 Found usage of a variable marked as unused: _configs

_configs = _cached_configs[self.name]
if isinstance(_configs, dict):
try:
self._configs = _configs[llh_name]

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
WPS121 Found usage of a variable marked as unused: _configs

mesg += f'but it is {llh_name}.'
raise ValueError(mesg)
else:
self._configs = _configs

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
WPS121 Found usage of a variable marked as unused: _configs

_cached_configs.update({self.name: self._configs})
self._configs_default = self.get_default()

self.median = Map(name=self.name + '_median', default=self._configs_default[0])

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
WPS221 Found line with high Jones Complexity: 15 > 14

self.median = Map(name=self.name + '_median', default=self._configs_default[0])
_cached_configs[self.median.name] = self._configs[0]
self.median.build()
self.lower = Map(name=self.name + '_lower', default=self._configs_default[1])

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
WPS221 Found line with high Jones Complexity: 15 > 14

self.lower = Map(name=self.name + '_lower', default=self._configs_default[1])
_cached_configs[self.lower.name] = self._configs[1]
self.lower.build()
self.upper = Map(name=self.name + '_upper', default=self._configs_default[2])

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
WPS221 Found line with high Jones Complexity: 15 > 14

self.upper.build()

def apply(self, pos, sigma):
"""

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
D200 One-line docstring should fit on one line with quotes

from appletree.plugin import Plugin
from appletree.config import Map
from appletree.config import Map, SigmaMap

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
F401 'appletree.config.Map' imported but unused

@dachengx
Copy link
Collaborator Author

@xzh19980906 There is a mess. But the most important file is appletree/config.py. I think the functionality of ti the most important thing, other files are already covered in autotest.


maps = {}
for i, sigma in enumerate(['median', 'lower', 'upper']):
maps[sigma] = Map(

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
WPS204 Found overused expression: maps[sigma]; used 5 > 4

maps[sigma] = Map(
name=self.name + f'_{sigma}',
default=self._configs_default[i])
if not maps[sigma].name in _cached_configs.keys():

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
WPS508 Found incorrect not with compare usage

# Sanity check
if isinstance(self.default, dict):
raise ValueError(
f"Do not set {self.name}'s default value as dict!"

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
C812 missing trailing comma

if not self.name in _cached_configs:
_cached_configs.update({self.name: self.get_default()})
if self.name in _cached_configs:
value = _cached_configs[self.name]

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
WPS529 Found implicit .get() dict usage

else:
value = self.get_default()
# Update values to sharing dictionary
_cached_configs[self.name] = {llh_name: value}

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
WPS529 Found implicit .get() dict usage

@@ -120,7 +138,8 @@ def build(self, llh_name: str = None):
file_path = _cached_configs[self.name]
else:
file_path = get_file_path(self.get_default())
_cached_configs.update({self.name: file_path})
# Update values to sharing dictionary
_cached_configs[self.name] = {llh_name: file_path}

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
WPS529 Found implicit .get() dict usage

else:
self._configs = self.get_default()
# Update values to sharing dictionary
_cached_configs[self.name] = {llh_name: self._configs}

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
WPS529 Found implicit .get() dict usage

# Update values to sharing dictionary
_cached_configs[self.name] = {llh_name: self._configs}

if isinstance(_configs, dict):

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
WPS121 Found usage of a variable marked as unused: _configs


if isinstance(_configs, dict):
try:
self._configs = _configs[llh_name]

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
WPS121 Found usage of a variable marked as unused: _configs

mesg += f'but it is {llh_name}.'
raise ValueError(mesg)
else:
self._configs = _configs

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
WPS121 Found usage of a variable marked as unused: _configs

if self.name in _cached_configs:
_configs = _cached_configs[self.name]
else:
_configs = self.get_default()

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
WPS121 Found usage of a variable marked as unused: _configs

else:
_configs = self.get_default()
# Update values to sharing dictionary
_cached_configs[self.name] = {llh_name: _configs}

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
WPS529 Found implicit .get() dict usage

else:
_configs = self.get_default()
# Update values to sharing dictionary
_cached_configs[self.name] = {llh_name: _configs}

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
WPS121 Found usage of a variable marked as unused: _configs

else:
value = self.get_default()
# Update values to sharing dictionary
_cached_configs[self.name] = value

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
WPS529 Found implicit .get() dict usage

@@ -120,7 +137,8 @@ def build(self, llh_name: str = None):
file_path = _cached_configs[self.name]
else:
file_path = get_file_path(self.get_default())
_cached_configs.update({self.name: file_path})
# Update values to sharing dictionary
_cached_configs[self.name] = file_path

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
WPS529 Found implicit .get() dict usage

else:
_configs = self.get_default()
# Update values to sharing dictionary
_cached_configs[self.name] = _configs

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
WPS529 Found implicit .get() dict usage

else:
_configs = self.get_default()
# Update values to sharing dictionary
_cached_configs[self.name] = _configs

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
WPS121 Found usage of a variable marked as unused: _configs

@@ -48,9 +48,12 @@ def __init__(self, name: str = None, **config):
warning = f'The usage of meshgrid binning is highly discouraged.'
warn(warning)
self.component_bins_type = 'meshgrid'
x_bins = jnp.linspace(*config['x_clip'], self._bins[0] + 1)

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
WPS221 Found line with high Jones Complexity: 15 > 14

@@ -48,9 +48,12 @@ def __init__(self, name: str = None, **config):
warning = f'The usage of meshgrid binning is highly discouraged.'
warn(warning)
self.component_bins_type = 'meshgrid'
x_bins = jnp.linspace(*config['x_clip'], self._bins[0] + 1)
y_bins = jnp.linspace(*config['y_clip'], self._bins[1] + 1)

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
WPS221 Found line with high Jones Complexity: 15 > 14

maps = {}
sigmas = ['median', 'lower', 'upper']
for i, sigma in enumerate(sigmas):
maps[sigma] = Map(

Choose a reason for hiding this comment

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

[pep8] reported by reviewdog 🐶
WPS204 Found overused expression: maps[sigma]; used 6 > 4

@dachengx dachengx merged commit b4930fb into master Jan 30, 2023
@dachengx dachengx deleted the interp_by_map branch January 30, 2023 19:40
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.

None yet

2 participants