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

Implement outlier detection step. #981

Merged
merged 9 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
0.13.0 (unreleased)
===================

outlier_detection
-----------------
- Implemented outlier-detection step. [#981]
mairanteodoro marked this conversation as resolved.
Show resolved Hide resolved

associations
------------

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ dependencies = [
'tweakwcs >=0.8.0',
'spherical-geometry >= 1.2.22',
'stsci.imagestats >= 1.6.3',
'drizzle >= 1.13.7',
'drizzle >= 1.14.3',
'webbpsf == 1.1.1',
]
dynamic = ['version']
Expand Down
78 changes: 78 additions & 0 deletions romancal/datamodels/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from collections.abc import Iterable, Sequence
from pathlib import Path

import numpy as np
from roman_datamodels import datamodels as rdm

from romancal.lib.basic_utils import is_association
Expand All @@ -18,6 +19,7 @@
"ModelContainer",
]

_ONE_MB = 1 << 20
RECOGNIZED_MEMBER_FIELDS = [
"tweakreg_catalog",
]
Expand Down Expand Up @@ -529,6 +531,82 @@

return crds_header

def set_buffer(self, buffer_size, overlap=None):
"""Set buffer size for scrolling section-by-section access.

Parameters
----------
buffer_size : float, None
Define size of buffer in MB for each section.
If `None`, a default buffer size of 1MB will be used.

overlap : int, optional
Define the number of rows of overlaps between sections.
If `None`, no overlap will be used.
"""
self.overlap = 0 if overlap is None else overlap
self.grow = 0

Check warning on line 548 in romancal/datamodels/container.py

View check run for this annotation

Codecov / codecov/patch

romancal/datamodels/container.py#L547-L548

Added lines #L547 - L548 were not covered by tests

with rdm.open(self._models[0]) as model:
imrows, imcols = model.data.shape
data_item_size = model.data.itemsize
data_item_type = model.data.dtype
del model

Check warning on line 554 in romancal/datamodels/container.py

View check run for this annotation

Codecov / codecov/patch

romancal/datamodels/container.py#L550-L554

Added lines #L550 - L554 were not covered by tests

min_buffer_size = imcols * data_item_size

Check warning on line 556 in romancal/datamodels/container.py

View check run for this annotation

Codecov / codecov/patch

romancal/datamodels/container.py#L556

Added line #L556 was not covered by tests

self.buffer_size = (

Check warning on line 558 in romancal/datamodels/container.py

View check run for this annotation

Codecov / codecov/patch

romancal/datamodels/container.py#L558

Added line #L558 was not covered by tests
min_buffer_size if buffer_size is None else (buffer_size * _ONE_MB)
)

section_nrows = min(imrows, int(self.buffer_size // min_buffer_size))

Check warning on line 562 in romancal/datamodels/container.py

View check run for this annotation

Codecov / codecov/patch

romancal/datamodels/container.py#L562

Added line #L562 was not covered by tests

if section_nrows == 0:
self.buffer_size = min_buffer_size
logger.warning(

Check warning on line 566 in romancal/datamodels/container.py

View check run for this annotation

Codecov / codecov/patch

romancal/datamodels/container.py#L564-L566

Added lines #L564 - L566 were not covered by tests
"WARNING: Buffer size is too small to hold a single row."
f"Increasing buffer size to {self.buffer_size / _ONE_MB}MB"
)
section_nrows = 1

Check warning on line 570 in romancal/datamodels/container.py

View check run for this annotation

Codecov / codecov/patch

romancal/datamodels/container.py#L570

Added line #L570 was not covered by tests

nbr = section_nrows - self.overlap
nsec = (imrows - self.overlap) // nbr
if (imrows - self.overlap) % nbr > 0:
nsec += 1

Check warning on line 575 in romancal/datamodels/container.py

View check run for this annotation

Codecov / codecov/patch

romancal/datamodels/container.py#L572-L575

Added lines #L572 - L575 were not covered by tests

self.n_sections = nsec
self.nbr = nbr
self.section_nrows = section_nrows
self.imrows = imrows
self.imcols = imcols
self.imtype = data_item_type

Check warning on line 582 in romancal/datamodels/container.py

View check run for this annotation

Codecov / codecov/patch

romancal/datamodels/container.py#L577-L582

Added lines #L577 - L582 were not covered by tests

def get_sections(self):
"""Iterator to return the sections from all members of the container."""

for k in range(self.n_sections):
e1 = k * self.nbr
e2 = e1 + self.section_nrows

Check warning on line 589 in romancal/datamodels/container.py

View check run for this annotation

Codecov / codecov/patch

romancal/datamodels/container.py#L587-L589

Added lines #L587 - L589 were not covered by tests

if k == self.n_sections - 1: # last section
e2 = min(e2, self.imrows)
e1 = min(e1, e2 - self.overlap - 1)

Check warning on line 593 in romancal/datamodels/container.py

View check run for this annotation

Codecov / codecov/patch

romancal/datamodels/container.py#L591-L593

Added lines #L591 - L593 were not covered by tests

data_list = np.empty(

Check warning on line 595 in romancal/datamodels/container.py

View check run for this annotation

Codecov / codecov/patch

romancal/datamodels/container.py#L595

Added line #L595 was not covered by tests
(len(self._models), e2 - e1, self.imcols), dtype=self.imtype
)
wht_list = np.empty(

Check warning on line 598 in romancal/datamodels/container.py

View check run for this annotation

Codecov / codecov/patch

romancal/datamodels/container.py#L598

Added line #L598 was not covered by tests
(len(self._models), e2 - e1, self.imcols), dtype=self.imtype
)
for i, model in enumerate(self._models):
model = rdm.open(model, memmap=self._memmap)

Check warning on line 602 in romancal/datamodels/container.py

View check run for this annotation

Codecov / codecov/patch

romancal/datamodels/container.py#L601-L602

Added lines #L601 - L602 were not covered by tests

data_list[i, :, :] = model.data[e1:e2].copy()
wht_list[i, :, :] = model.weight[e1:e2].copy()
del model

Check warning on line 606 in romancal/datamodels/container.py

View check run for this annotation

Codecov / codecov/patch

romancal/datamodels/container.py#L604-L606

Added lines #L604 - L606 were not covered by tests

yield (data_list, wht_list, (e1, e2))

Check warning on line 608 in romancal/datamodels/container.py

View check run for this annotation

Codecov / codecov/patch

romancal/datamodels/container.py#L608

Added line #L608 was not covered by tests


def make_file_with_index(file_path, idx):
"""Append an index to a filename
Expand Down
2 changes: 1 addition & 1 deletion romancal/lib/dqflags.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"GW_AFFECTED_DATA": 2**4, # Data affected by the GW read window
"PERSISTENCE": 2**5, # High persistence (was RESERVED_2)
"AD_FLOOR": 2**6, # Below A/D floor (0 DN, was RESERVED_3)
"RESERVED_4": 2**7, #
"OUTLIER": 2**7, # Flagged by outlier detection (was RESERVED_4)
mairanteodoro marked this conversation as resolved.
Show resolved Hide resolved
"UNRELIABLE_ERROR": 2**8, # Uncertainty exceeds quoted error
"NON_SCIENCE": 2**9, # Pixel not on science portion of detector
"DEAD": 2**10, # Dead pixel
Expand Down
Loading