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

Feature/visualize fpr value #42

Merged
merged 23 commits into from
Feb 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
7815845
added method pixels_within_region
Jammy2211 Feb 8, 2023
67ee4e1
rename pixels_within methods
Jammy2211 Feb 8, 2023
66d2e62
smallest_parallel_rows_within_ci_regions
Jammy2211 Feb 8, 2023
ada1579
added pixels_from_end to median_list_from for parallel FPR
Jammy2211 Feb 8, 2023
7b8aa4d
pixels_from_end added to std_list_from in parallel
Jammy2211 Feb 8, 2023
9a6a0e7
added pixels_from_end to median_list_of_lists_from in ParallelExtract
Jammy2211 Feb 8, 2023
0e7637d
test on pixels_from_end in std_list_of_lists
Jammy2211 Feb 8, 2023
4c1504e
Extract2DParallelFPR supports pixels_from_end
Jammy2211 Feb 8, 2023
6b1ca4f
fix tests due to pixels_from_end changes
Jammy2211 Feb 8, 2023
a7ac60b
added fpr_value to imaging data
Jammy2211 Feb 9, 2023
a9f4b98
fpr value added to imaging and tracked
Jammy2211 Feb 9, 2023
4259be6
pixels_from_end in Extract2DParallelOverscan
Jammy2211 Feb 9, 2023
a69024a
added pixels_from_end to Extract2DSerialEPER
Jammy2211 Feb 9, 2023
15bbfc9
added pixels_from_end to Extract2DSerialFPR
Jammy2211 Feb 9, 2023
c1bfbfe
added pixels_from_end to Extract2DSerialOverscan
Jammy2211 Feb 9, 2023
92c0466
added pixels_from_end to Extract2DSerialPrescan
Jammy2211 Feb 9, 2023
858accd
remove if statement in paralle abstract
Jammy2211 Feb 9, 2023
aacf6ff
pixels from end added to Extract2DSerial
Jammy2211 Feb 9, 2023
0bd214f
added test__median_list_of_lists_from__pixels_from_end
Jammy2211 Feb 9, 2023
7ec4ea9
added pixels_from_end to Extract1DFPR
Jammy2211 Feb 9, 2023
a7ad10b
added 1D EPER extract
Jammy2211 Feb 9, 2023
a2a1ae3
added 1D overscan extract
Jammy2211 Feb 9, 2023
3a1584e
pixels_from_end added everywhere
Jammy2211 Feb 9, 2023
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
18 changes: 18 additions & 0 deletions autocti/charge_injection/imaging/imaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def __init__(
layout: Layout2DCI,
cosmic_ray_map: Optional[aa.Array2D] = None,
noise_scaling_map_dict: Optional[Dict] = None,
fpr_value: Optional[float] = None,
):

super().__init__(image=image, noise_map=noise_map)
Expand All @@ -43,6 +44,22 @@ def __init__(

self.layout = layout

if fpr_value is None:

fpr_value = np.round(
np.mean(
self.layout.extract.parallel_fpr.median_list_from(
array=self.data,
pixels_from_end=min(
10, self.layout.smallest_parallel_rows_within_ci_regions
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe assign 10 to a constant so it's clear what it means rather than just being an arbitrary integer value?

),
)
),
2,
)

self.fpr_value = fpr_value

self.imaging_full = self

@property
Expand Down Expand Up @@ -107,6 +124,7 @@ def apply_mask(self, mask: mask_2d.Mask2D) -> "ImagingCI":
layout=self.layout,
cosmic_ray_map=cosmic_ray_map,
noise_scaling_map_dict=noise_scaling_map_dict,
fpr_value=self.fpr_value,
)

def apply_settings(self, settings: SettingsImagingCI):
Expand Down
5 changes: 3 additions & 2 deletions autocti/charge_injection/plot/fit_ci_plotters.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import numpy as np
from typing import Callable

import autoarray.plot as aplt
Expand Down Expand Up @@ -351,7 +352,7 @@ def figures_1d_of_region(
y_extra=y_extra,
visuals_1d=self.visuals_1d,
auto_labels=AutoLabels(
title=f"Data 1D With Noise & Model-Fit [{region}]",
title=f"Data w/ Noise {region} (FPR = {self.fit.dataset.fpr_value} e-)",
ylabel="Data (e-)",
xlabel="Pixel No.",
filename=f"data_with_noise_map_{region}",
Expand All @@ -374,7 +375,7 @@ def figures_1d_of_region(
y_extra=y_extra,
visuals_1d=self.visuals_1d,
auto_labels=AutoLabels(
title=f"Data 1D With Noise & Model-Fit (log10 y axis) [{region}]",
title=f"Data w/ Noise {region} [log10 y] (FPR = {self.fit.dataset.fpr_value} e-)",
ylabel="Data (e-)",
xlabel="Pixel No.",
filename=f"data_with_noise_map_logy_{region}",
Expand Down
6 changes: 4 additions & 2 deletions autocti/charge_injection/plot/imaging_ci_plotters.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import numpy as np
from typing import Callable

import autoarray.plot as aplt

from autoarray.mask.mask_2d import Mask2D
from autoarray.plot.auto_labels import AutoLabels
from autoarray.dataset.plot.imaging_plotters import ImagingPlotterMeta

Expand Down Expand Up @@ -274,7 +276,7 @@ def figures_1d_of_region(
y_errors=y_errors,
visuals_1d=self.get_visuals_1d(),
auto_labels=AutoLabels(
title=f"Data 1D With Noise {region}",
title=f"Data w/ Noise {region} (FPR = {self.dataset.fpr_value} e-)",
ylabel="Data (e-)",
xlabel="Pixel No.",
filename=f"data_with_noise_map_{region}",
Expand All @@ -295,7 +297,7 @@ def figures_1d_of_region(
y_errors=y_errors,
visuals_1d=self.get_visuals_1d(),
auto_labels=AutoLabels(
title=f"Data 1D With Noise {region} (log10 y axis)",
title=f"Data w/ Noise {region} [log10 y] (FPR = {self.dataset.fpr_value} e-)",
ylabel="Data (e-)",
xlabel="Pixel No.",
filename=f"data_with_noise_map_logy_{region}",
Expand Down
4 changes: 4 additions & 0 deletions autocti/exc.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class ImagingCIException(Exception):
pass


class ExtractException(Exception):
pass
Comment on lines +17 to +18
Copy link
Collaborator

Choose a reason for hiding this comment

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

Docs?



class LayoutException(Exception):
pass

Expand Down
63 changes: 53 additions & 10 deletions autocti/extract/one_d/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
class Extract1D:
def __init__(
self,
shape_1d: Optional[Tuple[int]] = None,
region_list: Optional[aa.type.Region1DList] = None,
prescan: Optional[aa.type.Region1DLike] = None,
overscan: Optional[aa.type.Region1DLike] = None,
Expand All @@ -22,6 +23,9 @@ def __init__(
region_list
Integer pixel coordinates specifying the corners of signal (x0, x1).
"""

self.shape_1d = shape_1d

self.region_list = (
list(map(aa.Region1D, region_list)) if region_list is not None else None
)
Expand All @@ -42,11 +46,29 @@ def total_pixels_min(self) -> int:
"""
return np.min([region.total_pixels for region in self.region_list])

def region_list_from(self, pixels: Tuple[int, int]) -> List[aa.Region2D]:
@property
def parallel_rows_between_regions(self):
return [
self.region_list[i + 1].x0 - self.region_list[i].x1
for i in range(len(self.region_list) - 1)
]
Comment on lines +51 to +54
Copy link
Collaborator

Choose a reason for hiding this comment

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

You can also do

[second.x0 - first.x1 for first, second in zip(self.region_list, self.region_list[1:])]


@property
def trail_size_to_array_edge(self):
return self.shape_1d[0] - np.max([region.x1 for region in self.region_list])

def region_list_from(
self,
pixels: Optional[Tuple[int, int]] = None,
pixels_from_end: Optional[int] = None,
) -> List[aa.Region2D]:
raise NotImplementedError

def array_1d_list_from(
self, array: aa.Array1D, pixels: Tuple[int, int]
self,
array: aa.Array1D,
pixels: Optional[Tuple[int, int]] = None,
pixels_from_end: Optional[int] = None,
) -> List[aa.Array1D]:
"""
Extract a specific region from every region on the line dataset and return as a list of 1D arrays.
Expand All @@ -66,11 +88,16 @@ def array_1d_list_from(

arr_list = [
array.native[region.slice]
for region in self.region_list_from(pixels=pixels)
for region in self.region_list_from(
pixels=pixels, pixels_from_end=pixels_from_end
)
]

mask_1d_list = [
array.mask[region.slice] for region in self.region_list_from(pixels=pixels)
array.mask[region.slice]
for region in self.region_list_from(
pixels=pixels, pixels_from_end=pixels_from_end
)
]

return [
Expand All @@ -79,7 +106,10 @@ def array_1d_list_from(
]

def stacked_array_1d_from(
self, array: aa.Array1D, pixels: Tuple[int, int]
self,
array: aa.Array1D,
pixels: Optional[Tuple[int, int]] = None,
pixels_from_end: Optional[int] = None,
) -> aa.Array1D:
"""
Extract a region (e.g. the FPR) of every region on the line dataset and stack them by taking their mean.
Expand All @@ -102,13 +132,18 @@ def stacked_array_1d_from(

arr_list = [
np.ma.array(data=array.native[region.slice], mask=array.mask[region.slice])
for region in self.region_list_from(pixels=pixels)
for region in self.region_list_from(
pixels=pixels, pixels_from_end=pixels_from_end
)
]

stacked_array_1d = np.ma.mean(np.ma.asarray(arr_list), axis=0)

mask_1d_list = [
array.mask[region.slice] for region in self.region_list_from(pixels=pixels)
array.mask[region.slice]
for region in self.region_list_from(
pixels=pixels, pixels_from_end=pixels_from_end
)
]

return aa.Array1D(
Expand All @@ -117,7 +152,11 @@ def stacked_array_1d_from(
).native

def add_to_array(
self, new_array: aa.Array1D, array: aa.Array1D, pixels: Tuple[int, int]
self,
new_array: aa.Array1D,
array: aa.Array1D,
pixels: Optional[Tuple[int, int]] = None,
pixels_from_end: Optional[int] = None,
Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess that decorator I mentioned in the other PR would also be applicable here

) -> aa.Array1D:
"""
Extracts the region (e.g. the FPRs) from the line dataset and adds them to a line dataset.
Expand All @@ -132,9 +171,13 @@ def add_to_array(
The row pixel index which determines the region extracted (e.g. `pixels=(0, 3)` will compute the region
corresponding to the 1st, 2nd and 3rd pixels).
"""
region_list = self.region_list_from(pixels=pixels)
region_list = self.region_list_from(
pixels=pixels, pixels_from_end=pixels_from_end
)

array_1d_list = self.array_1d_list_from(array=array, pixels=pixels)
array_1d_list = self.array_1d_list_from(
array=array, pixels=pixels, pixels_from_end=pixels_from_end
)

for arr, region in zip(array_1d_list, region_list):
new_array[region.x0 : region.x1] += arr
Expand Down
32 changes: 24 additions & 8 deletions autocti/extract/one_d/eper.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import numpy as np
from typing import List, Tuple
from typing import List, Optional, Tuple

import autoarray as aa

from autocti.extract.one_d.abstract import Extract1D


class Extract1DEPER(Extract1D):
def region_list_from(self, pixels: Tuple[int, int]) -> List[aa.Region1D]:
def region_list_from(
self,
pixels: Optional[Tuple[int, int]] = None,
pixels_from_end: Optional[int] = None,
) -> List[aa.Region1D]:
"""
Returns a list of the (x0, x1) regions containing the EPERs of a 1D CTI dataset.

Expand All @@ -22,12 +26,24 @@ def region_list_from(self, pixels: Tuple[int, int]) -> List[aa.Region1D]:
The row indexes to extract the trails between (e.g. pixels(0, 3) extracts the 1st, 2nd and 3rd pixels)
"""

return list(
map(
lambda region: region.trailing_region_from(pixels=pixels),
self.region_list,
)
)
region_list = []

for i, region in enumerate(self.region_list):

if pixels_from_end is not None:

parallel_row_spaces = self.parallel_rows_between_regions + [
self.trail_size_to_array_edge
]

pixels = (
parallel_row_spaces[i] - pixels_from_end,
parallel_row_spaces[i],
)

region_list.append(region.trailing_region_from(pixels=pixels))

return region_list

def array_1d_from(self, array: aa.Array1D) -> aa.Array1D:
"""
Expand Down
13 changes: 10 additions & 3 deletions autocti/extract/one_d/fpr.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
from typing import List, Tuple
from typing import List, Optional, Tuple

import autoarray as aa

from autocti.extract.one_d.abstract import Extract1D


class Extract1DFPR(Extract1D):
def region_list_from(self, pixels: Tuple[int, int]) -> List[aa.Region1D]:
def region_list_from(
self,
pixels: Optional[Tuple[int, int]] = None,
pixels_from_end: Optional[int] = None,
) -> List[aa.Region1D]:
"""
Returns a list of the (x0, x1) regions containing the FPRs of a 1D CTI dataset.

Expand All @@ -22,6 +26,9 @@ def region_list_from(self, pixels: Tuple[int, int]) -> List[aa.Region1D]:
"""
return list(
map(
lambda region: region.front_region_from(pixels=pixels), self.region_list
lambda region: region.front_region_from(
pixels=pixels, pixels_from_end=pixels_from_end
),
self.region_list,
)
)
19 changes: 14 additions & 5 deletions autocti/extract/one_d/master.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Tuple
from typing import Optional, Tuple

from autocti.extract.one_d.overscan import Extract1DOverscan
from autocti.extract.one_d.fpr import Extract1DFPR
Expand All @@ -11,8 +11,9 @@ class Extract1DMaster:
def __init__(
self,
region_list,
prescan: aa.type.Region1DLike = None,
overscan: aa.type.Region1DLike = None,
shape_1d: Optional[Tuple[int]] = None,
prescan: Optional[aa.type.Region1DLike] = None,
overscan: Optional[aa.type.Region1DLike] = None,
):
"""
Abstract class containing methods for extracting regions from a 1D line dataset which contains some sort of
Expand All @@ -34,19 +35,27 @@ def __init__(
list(map(aa.Region1D, region_list)) if region_list is not None else None
)

self.shape_1d = shape_1d

self._prescan = prescan
self._overscan = overscan

@property
def fpr(self):
return Extract1DFPR(
region_list=self.region_list, prescan=self._prescan, overscan=self._overscan
shape_1d=self.shape_1d,
region_list=self.region_list,
prescan=self._prescan,
overscan=self._overscan,
)

@property
def eper(self):
return Extract1DEPER(
region_list=self.region_list, prescan=self._prescan, overscan=self._overscan
shape_1d=self.shape_1d,
region_list=self.region_list,
prescan=self._prescan,
overscan=self._overscan,
)

@property
Expand Down
Loading