Skip to content

Commit

Permalink
Add option to skip final COOLEST instance validation
Browse files Browse the repository at this point in the history
  • Loading branch information
aymgal committed Jun 4, 2024
1 parent a28a36f commit 9f9ce3c
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
4 changes: 2 additions & 2 deletions coolest/template/classes/likelihood.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ def check_consistency_with_observation(self, observation):
width = abs(self.mask.field_of_view_x[1] - self.mask.field_of_view_x[0])
height = abs(self.mask.field_of_view_y[1] - self.mask.field_of_view_y[0])
num_pix_ra = round(width / observation.pixels.pixel_size)
error_message_ra = f"Field-of-view along RA is inconsistent (data: {num_pix_ra}, likelihood mask: {self.mask.num_pix_x})."
error_message_ra = f"Number of pixels along x is inconsistent (data: {num_pix_ra}, likelihood mask: {self.mask.num_pix_x})."
assert self.mask.num_pix_x == num_pix_ra, error_message_ra
num_pix_dec = round(height / observation.pixels.pixel_size)
error_message_dec = f"Field-of-view along Dec is inconsistent (data: {num_pix_dec}, likelihood mask: {self.mask.num_pix_y})."
error_message_dec = f"Number of pixels along y is inconsistent (data: {num_pix_dec}, likelihood mask: {self.mask.num_pix_y})."
assert self.mask.num_pix_y == num_pix_dec, error_message_dec
# TODO: check pixel size value?
4 changes: 2 additions & 2 deletions coolest/template/classes/observation.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ def check_consistency_with_instrument(self, instrument):
width = abs(self.pixels.field_of_view_x[1] - self.pixels.field_of_view_x[0])
height = abs(self.pixels.field_of_view_y[1] - self.pixels.field_of_view_y[0])
num_pix_ra = round(width / instrument.pixel_size)
error_message_ra = f"Field-of-view along RA is inconsistent (data: {self.pixels.num_pix_x}, instrument: {num_pix_ra})."
error_message_ra = f"Number of pixels along x is inconsistent (data: {self.pixels.num_pix_x}, instrument: {num_pix_ra})."
assert self.pixels.num_pix_x == num_pix_ra, error_message_ra
num_pix_dec = round(height / instrument.pixel_size)
error_message_dec = f"Field-of-view along Dec is inconsistent (data: {self.pixels.num_pix_y}, instrument: {num_pix_dec})."
error_message_dec = f"Number of pixels along y is inconsistent (data: {self.pixels.num_pix_y}, instrument: {num_pix_dec})."
assert self.pixels.num_pix_y == num_pix_dec, error_message_dec
# TODO: check pixel size value?

Expand Down
13 changes: 7 additions & 6 deletions coolest/template/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def dump_jsonpickle(self):
with open(json_path, 'w') as f:
f.write(result)

def load(self, skip_jsonpickle=False, verbose=True):
def load(self, skip_jsonpickle=False, validate=True, verbose=True):
"""Read the JSON template file and build up the corresponding COOLEST object.
It will first try to load the '_pyAPI' template if it exists using `jsonpickle`,
otherwise it will fall back to reading the pure json template.
Expand All @@ -114,11 +114,11 @@ def load(self, skip_jsonpickle=False, verbose=True):
else:
if verbose:
print(f"Template file '{jsonpickle_path}' not found, now trying to read '{json_path}'.")
instance = self.load_simple(json_path, as_object=True)
instance = self.load_simple(json_path, as_object=True, validate=validate)
assert isinstance(instance, COOLEST)
return instance

def load_simple(self, json_path, as_object=True):
def load_simple(self, json_path, as_object=True, validate=True):
"""Read the JSON template file and build up the corresponding COOLEST object.
Parameters
Expand All @@ -137,7 +137,7 @@ def load_simple(self, json_path, as_object=True):
content = json.loads(f.read())
if not as_object:
return content # dictionary
return self._json_to_coolest(content) # COOLEST object
return self._json_to_coolest(content, validate) # COOLEST object

def load_jsonpickle(self, jsonpickle_path):
"""Read the JSON template file and build up the corresponding COOLEST object
Expand All @@ -157,7 +157,7 @@ def load_jsonpickle(self, jsonpickle_path):
content = jsonpickle.decode(f.read())
return content # COOLEST object

def _json_to_coolest(self, json_content):
def _json_to_coolest(self, json_content, validate):
"""Creates from scratch a COOLEST instance based on the content of a JSON
file, given as a nested dictionnary.
Expand Down Expand Up @@ -208,7 +208,8 @@ def _json_to_coolest(self, json_content):
metadata=metadata)

# check consistency across the whole coolest object
self._validate_global(coolest)
if validate:
self._validate_global(coolest)
return coolest

@staticmethod
Expand Down
2 changes: 1 addition & 1 deletion test/template/json_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def test_dump_and_read(self):
# and btw we also instantiate another JSONSerializer as a test
serializer_2 = JSONSerializer(template_path, obj=None,
check_external_files=self.check_files)
coolest_3 = serializer_2.load(skip_jsonpickle=True)
coolest_3 = serializer_2.load(skip_jsonpickle=True, validate=False) # no validation as it is a dummy json file
assert isinstance(coolest_3, COOLEST)

# test that the content of the new json file is *exactly* the same as the original one
Expand Down

0 comments on commit 9f9ce3c

Please sign in to comment.