Skip to content

pytorch_dedispersion.boxcar_filter

BoxcarFilter

Source code in pytorch_dedispersion/boxcar_filter.py
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class BoxcarFilter:
    def __init__(self, data_tensor: torch.Tensor) -> None:
        """
        Initialize BoxcarFilter.

        Args:
            data_tensor (torch.Tensor): The dedispersed data tensor.
        """
        self.data_tensor = data_tensor

    def apply_boxcar(self, widths: Sequence[int]) -> torch.Tensor:
        """
        Apply boxcar filtering with different widths.

        Args:
            widths (list[int]): List of boxcar widths.

        Returns:
            torch.Tensor: Boxcar filtered data for each width.
        """
        boxcar_data = []
        for width in widths:
            kernel = torch.ones((width,), dtype=torch.float32, device=self.data_tensor.device) / width
            boxcar_filtered = torch.nn.functional.conv1d(self.data_tensor.unsqueeze(1), kernel.unsqueeze(0).unsqueeze(1), padding='same').squeeze(1)
            boxcar_data.append(boxcar_filtered)
        return torch.stack(boxcar_data)

__init__

__init__(data_tensor)

Initialize BoxcarFilter.

Parameters:

Name Type Description Default
data_tensor Tensor

The dedispersed data tensor.

required
Source code in pytorch_dedispersion/boxcar_filter.py
 5
 6
 7
 8
 9
10
11
12
def __init__(self, data_tensor: torch.Tensor) -> None:
    """
    Initialize BoxcarFilter.

    Args:
        data_tensor (torch.Tensor): The dedispersed data tensor.
    """
    self.data_tensor = data_tensor

apply_boxcar

apply_boxcar(widths)

Apply boxcar filtering with different widths.

Parameters:

Name Type Description Default
widths list[int]

List of boxcar widths.

required

Returns:

Type Description
Tensor

torch.Tensor: Boxcar filtered data for each width.

Source code in pytorch_dedispersion/boxcar_filter.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def apply_boxcar(self, widths: Sequence[int]) -> torch.Tensor:
    """
    Apply boxcar filtering with different widths.

    Args:
        widths (list[int]): List of boxcar widths.

    Returns:
        torch.Tensor: Boxcar filtered data for each width.
    """
    boxcar_data = []
    for width in widths:
        kernel = torch.ones((width,), dtype=torch.float32, device=self.data_tensor.device) / width
        boxcar_filtered = torch.nn.functional.conv1d(self.data_tensor.unsqueeze(1), kernel.unsqueeze(0).unsqueeze(1), padding='same').squeeze(1)
        boxcar_data.append(boxcar_filtered)
    return torch.stack(boxcar_data)