Skip to content

Commit

Permalink
more mypy + work started to fix quantization when writing
Browse files Browse the repository at this point in the history
  • Loading branch information
pravirkr committed Apr 23, 2024
1 parent 5ee4d5a commit e5b64c2
Show file tree
Hide file tree
Showing 20 changed files with 611 additions and 480 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest]
python-version: ['3.8', '3.9', '3.10']
python-version: ['3.9', '3.10', '3.11']

steps:
- uses: actions/checkout@v4
Expand All @@ -30,9 +30,11 @@ jobs:
uses: chartboost/ruff-action@v1
with:
args: "check"
continue-on-error: true
- name: Check types with mypy
run: |
mypy --strict ./sigpyproc/
continue-on-error: true
- name: Test with pytest and Generate coverage report
run: |
pytest --cov=./ --cov-report=xml
Expand Down
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ maintainers = [{ name = "Pravir Kumar", email = "pravirka@gmail.com" }]
requires-python = ">=3.8"
classifiers = [
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Operating System :: MacOS :: MacOS X",
"Operating System :: Microsoft :: Windows",
"Operating System :: POSIX :: Linux",
Expand Down Expand Up @@ -58,7 +58,6 @@ spp_decimate = "sigpyproc.apps.spp_decimate:main"
spp_extract = "sigpyproc.apps.spp_extract:main"
spp_clean = "sigpyproc.apps.spp_clean:main"


[tool.ruff]
include = ["pyproject.toml", "sigpyproc/**/*.py"]
line-length = 88
Expand Down
9 changes: 2 additions & 7 deletions sigpyproc/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
import sys
from importlib import metadata

if sys.version_info >= (3, 8):
from importlib import metadata as importlib_metadata # noqa: WPS433
else:
import importlib_metadata # noqa: WPS440, WPS433

__version__ = importlib_metadata.version(__name__) # noqa: WPS410
__version__ = metadata.version(__name__)
45 changes: 0 additions & 45 deletions sigpyproc/apps/spp_decimate.py

This file was deleted.

99 changes: 99 additions & 0 deletions sigpyproc/apps/spp_digifil.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import click

from sigpyproc.readers import
from sigpyproc.utils import get_logger


@click.command(
context_settings={"help_option_names": ["-h", "--help"], "show_default": True},
)
@click.argument("filfile", type=click.Path(exists=True))
@click.option(
"--cont",
is_flag=True,
help="Input files are contiguous (disable check)",
)
@click.option(
"-b",
"--nbits",
type=int,
default=8,
help="Number of bits per output sample",
)
@click.option(
"-B",
"--block_size",
type=int,
default=128,
help="block size in megabytes",
)
@click.option(
"-c",
"--rescale_constant",
is_flag=True,
help="Keep offset and scale constant",
)
@click.option(
"-t",
"--tscrunch_factor",
type=int,
default=1,
help="Decimate in time",
)
@click.option(
"-f",
"--fscrunch_factor",
type=int,
default=1,
help="Decimate in frequency",
)
@click.option(
"-I",
"--rescale_seconds",
type=float,
default=10.0,
help="Rescale interval in seconds (0 -> disable rescaling)",
)
@click.option(
"-s",
"--scale_fac",
type=float,
default=1.0,
help="Data scale factor to apply",
)
@click.option(
"-scloffs",
"--apply_FITS_scale_and_offset",
is_flag=True,
help="Denormalize using DAT_SCL and DAT_OFFS [PSRFITS]",
)
@click.option(
"-o",
"--output_filename",
type=click.Path(),
help="Output filename",
)
def main(
filfile,
cont,
nbits,
block_size,
rescale_constant,
tscrunch_factor,
fscrunch_factor,
rescale_seconds,
scale_fac,
apply_FITS_scale_and_offset,
):
"""Convert to sigproc output digifil style."""
logger = get_logger(__name__)
nbytes_per_sample = TODO
gulpsize = block_size * 1024 * 1024 //
logger.info(f"Reading {filfile}")
fil = FilReader(filfile)
fil.downsample(tfactor=tfactor, ffactor=ffactor, gulp=gulp, filename=outfile)



if __name__ == "__main__":
main()
38 changes: 21 additions & 17 deletions sigpyproc/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,17 +348,21 @@ def invert_freq(
if filename is None:
filename = f"{self.header.basename}_inverted.fil"

changes = {
updates = {
"fch1": self.header.fch1 + (self.header.nchans - 1) * self.header.foff,
"foff": self.header.foff * -1,
}

out_file = self.header.prep_outfile(filename, changes, nbits=self.header.nbits)
out_file = self.header.prep_outfile(
filename,
updates=updates,
nbits=self.header.nbits,
)
for nsamp, _ii, data in self.read_plan(**plan_kwargs):
out_ar = kernels.invert_freq(data, self.header.nchans, nsamp)
out_file.cwrite(out_ar)
out_file.close()
return out_file.name
return out_file.file_cur

def apply_channel_mask(
self,
Expand Down Expand Up @@ -394,7 +398,7 @@ def apply_channel_mask(
for nsamps, _ii, data in self.read_plan(**plan_kwargs):
kernels.mask_channels(data, mask, maskvalue, self.header.nchans, nsamps)
out_file.cwrite(data)
return out_file.name
return out_file.file_cur

def downsample(
self,
Expand Down Expand Up @@ -438,12 +442,12 @@ def downsample(
# Gulp must be a multiple of tfactor
gulp = int(np.ceil(gulp / tfactor) * tfactor)

changes = {
updates = {
"tsamp": self.header.tsamp * tfactor,
"nchans": self.header.nchans // ffactor,
"foff": self.header.foff * ffactor,
}
out_file = self.header.prep_outfile(filename, changes)
out_file = self.header.prep_outfile(filename, updates=updates)

for nsamps, _ii, data in self.read_plan(gulp=gulp, **plan_kwargs):
write_ar = kernels.downsample_2d(
Expand All @@ -454,7 +458,7 @@ def downsample(
nsamps,
)
out_file.cwrite(write_ar)
return out_file.name
return out_file.file_cur

def extract_samps(
self,
Expand Down Expand Up @@ -503,7 +507,7 @@ def extract_samps(
):
out_file.cwrite(data)
out_file.close()
return out_file.name
return out_file.file_cur

def extract_chans(
self,
Expand Down Expand Up @@ -543,7 +547,7 @@ def extract_chans(
out_files = [
self.header.prep_outfile(
f"{self.header.basename}_chan{chan:04d}.tim",
{"nchans": 1, "nbits": 32, "data_type": "time series"},
updates={"nchans": 1, "nbits": 32, "data_type": "time series"},
nbits=32,
)
for chan in chans
Expand All @@ -556,7 +560,7 @@ def extract_chans(
for out_file in out_files:
out_file.close()

return [out_file.name for out_file in out_files]
return [out_file.file_cur for out_file in out_files]

def extract_bands(
self,
Expand Down Expand Up @@ -615,7 +619,7 @@ def extract_bands(
out_files = [
self.header.prep_outfile(
f"{self.header.basename}_sub{isub:02d}.fil",
{
updates={
"nchans": chanpersub,
"fch1": fstart + isub * chanpersub * self.header.foff,
},
Expand All @@ -634,7 +638,7 @@ def extract_bands(
for out_file in out_files:
out_file.close()

return [out_file.name for out_file in out_files]
return [out_file.file_cur for out_file in out_files]

def requantize(
self,
Expand Down Expand Up @@ -673,11 +677,11 @@ def requantize(
if filename is None:
filename = f"{self.header.basename}_digi.fil"

out_file = self.header.prep_outfile(filename, nbits=nbits_out, quantize=True)
out_file = self.header.prep_outfile(filename, nbits=nbits_out)
for _nsamps, _ii, data in self.read_plan(**plan_kwargs):
out_file.cwrite(data)
out_file.close()
return out_file.name
return out_file.file_cur

def remove_zerodm(
self,
Expand Down Expand Up @@ -729,7 +733,7 @@ def remove_zerodm(
)
out_file.cwrite(out_ar[: nsamps * self.header.nchans])
out_file.close()
return out_file.name
return out_file.file_cur

def subband(
self,
Expand Down Expand Up @@ -768,7 +772,7 @@ def subband(
new_foff = self.header.foff * self.header.nchans // nsub
new_fch1 = self.header.ftop - new_foff / 2
chan_to_sub = np.arange(self.header.nchans, dtype="int32") // subfactor
changes = {
updates = {
"fch1": new_fch1,
"foff": new_foff,
"refdm": dm,
Expand All @@ -777,7 +781,7 @@ def subband(
}
if filename is None:
filename = f"{self.header.basename}_DM{dm:06.2f}.subbands"
out_file = self.header.prep_outfile(filename, changes, nbits=32)
out_file = self.header.prep_outfile(filename, updates=updates, nbits=32)

for nsamps, _ii, data in self.read_plan(
gulp=gulp,
Expand Down
4 changes: 2 additions & 2 deletions sigpyproc/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ def to_file(self, filename: str | None = None) -> str:
if filename is None:
mjd_after = self.header.mjd_after_nsamps(self.shape[1])
filename = f"{self.header.basename}_{self.header.tstart:.12f}_to_{mjd_after:.12f}.fil"
changes = {"nbits": 32}
out_file = self.header.prep_outfile(filename, changes, nbits=32)
updates = {"nbits": 32}
out_file = self.header.prep_outfile(filename, updates=updates, nbits=32)
out_file.cwrite(self.transpose().ravel())
return filename
3 changes: 1 addition & 2 deletions sigpyproc/core/kernels.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ def fold(
count_ar[pos2] += 1


@njit("f4[:](f4[:], f4, f4)")
@njit("f4[:](f4[:], f4, f4)", cache=True)
def resample_tim(array, accel, tsamp):
nsamps = len(array) - 1 if accel > 0 else len(array)
resampled = np.zeros(nsamps, dtype=array.dtype)
Expand Down Expand Up @@ -506,7 +506,6 @@ def sum_harms(spec_arr, sum_arr, harm_arr, fact_arr, nharms, nsamps, nfold):
for kk in range(nharms // 2):
fact_arr[kk] += 2 * kk + 1


@jitclass(
[
("nchans", types.i4),
Expand Down
Loading

0 comments on commit e5b64c2

Please sign in to comment.