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

162 add docstrings to astrohack tests #166

Merged
merged 7 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
169 changes: 85 additions & 84 deletions src/astrohack/data/_dropbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,90 +7,91 @@
from astrohack._utils._logger._astrohack_logger import _get_astrohack_logger

FILE_ID = {
'ea25_cal_small_before_fixed.split.ms':
{
'file':'ea25_cal_small_before_fixed.split.ms.zip',
'id':'m2qnd2w6g9fhdxyzi6h7f',
'rlkey':'d4dgztykxpnnqrei7jhb1cu7m'
},
'ea25_cal_small_after_fixed.split.ms':
{
'file':'ea25_cal_small_after_fixed.split.ms.zip',
'id':'o3tl05e3qa440s4rk5owf',
'rlkey':'hoxte3zzeqgkju2ywnif2t7ko'
},
'J1924-2914.ms.calibrated.split.SPW3':
{
'file':'J1924-2914.ms.calibrated.split.SPW3.zip',
'id':'kyrwc5y6u7lxbmqw7fveh',
'rlkey':'r23qakcm24bid2x2cojsd96gs'
},
'extract_holog_verification.json':
{
'file': 'extract_holog_verification.json',
'id':'6pzucjd48a4n0eb74wys9',
'rlkey':'azuynw358zxvse9i225sbl59s'
},
'holog_numerical_verification.json':
{
'file':'holog_numerical_verification.json',
'id':'x69700pznt7uktwprdqpk',
'rlkey':'bxn9me7dgnxrtzvvay7xgicmi'
},
'locit-input-pha.cal':
{
'file':'locit-input-pha.cal.zip',
'id':'8fftz5my9h8ca2xdlupym',
'rlkey':'fxfid92953ycorh5wrhfgh78b'
},
'panel_cutoff_mask':
{
'file':'panel_cutoff_mask.npy',
'id':'8ta02t72vwcv4ketv8rfw',
'rlkey':'qsmos4hx2duz8upb83hghi6q8'
}


'ea25_cal_small_before_fixed.split.ms':
{
'file': 'ea25_cal_small_before_fixed.split.ms.zip',
'id': 'm2qnd2w6g9fhdxyzi6h7f',
'rlkey': 'd4dgztykxpnnqrei7jhb1cu7m'
},
'ea25_cal_small_after_fixed.split.ms':
{
'file': 'ea25_cal_small_after_fixed.split.ms.zip',
'id': 'o3tl05e3qa440s4rk5owf',
'rlkey': 'hoxte3zzeqgkju2ywnif2t7ko'
},
'J1924-2914.ms.calibrated.split.SPW3':
{
'file': 'J1924-2914.ms.calibrated.split.SPW3.zip',
'id': 'kyrwc5y6u7lxbmqw7fveh',
'rlkey': 'r23qakcm24bid2x2cojsd96gs'
},
'extract_holog_verification.json':
{
'file': 'extract_holog_verification.json',
'id': '6pzucjd48a4n0eb74wys9',
'rlkey': 'azuynw358zxvse9i225sbl59s'
},
'holog_numerical_verification.json':
{
'file': 'holog_numerical_verification.json',
'id': 'x69700pznt7uktwprdqpk',
'rlkey': 'bxn9me7dgnxrtzvvay7xgicmi'
},
'locit-input-pha.cal':
{
'file': 'locit-input-pha.cal.zip',
'id': '8fftz5my9h8ca2xdlupym',
'rlkey': 'fxfid92953ycorh5wrhfgh78b'
},
'panel_cutoff_mask':
{
'file': 'panel_cutoff_mask.npy',
'id': '8ta02t72vwcv4ketv8rfw',
'rlkey': 'qsmos4hx2duz8upb83hghi6q8'
}

}


def download(file, folder='.'):
logger = _get_astrohack_logger()

if os.path.exists('/'.join((folder, file))):
logger.info("File exists.")
return

if file not in FILE_ID.keys():
logger.info("Requested file not found")

return

fullname=FILE_ID[file]['file']
id=FILE_ID[file]['id']
rlkey=FILE_ID[file]['rlkey']

url = 'https://www.dropbox.com/scl/fi/{id}/{file}?rlkey={rlkey}'.format(id=id, file=fullname, rlkey=rlkey)

headers = {'user-agent': 'Wget/1.16 (linux-gnu)'}

r = requests.get(url, stream=True, headers=headers)
total = int(r.headers.get('content-length', 0))

fullname = '/'.join((folder, fullname))

with open(fullname, 'wb') as fd, tqdm(
desc=fullname,
total=total,
unit='iB',
unit_scale=True,
unit_divisor=1024) as bar:
for chunk in r.iter_content(chunk_size=1024):
if chunk:
size=fd.write(chunk)
bar.update(size)

if zipfile.is_zipfile(fullname):
shutil.unpack_archive(filename=fullname, extract_dir=folder)

# Let's clean up after ourselves
os.remove(fullname)
logger = _get_astrohack_logger()

if os.path.exists('/'.join((folder, file))):
logger.info("File exists.")
return

if file not in FILE_ID.keys():
logger.info("Requested file not found")
logger.info(FILE_ID.keys())

return

fullname = FILE_ID[file]['file']
id = FILE_ID[file]['id']
rlkey = FILE_ID[file]['rlkey']

url = 'https://www.dropbox.com/scl/fi/{id}/{file}?rlkey={rlkey}'.format(id=id, file=fullname, rlkey=rlkey)

headers = {'user-agent': 'Wget/1.16 (linux-gnu)'}

r = requests.get(url, stream=True, headers=headers)
total = int(r.headers.get('content-length', 0))

fullname = '/'.join((folder, fullname))

with open(fullname, 'wb') as fd, tqdm(
desc=fullname,
total=total,
unit='iB',
unit_scale=True,
unit_divisor=1024) as bar:
for chunk in r.iter_content(chunk_size=1024):
if chunk:
size = fd.write(chunk)
bar.update(size)

if zipfile.is_zipfile(fullname):
shutil.unpack_archive(filename=fullname, extract_dir=folder)

# Let's clean up after ourselves
os.remove(fullname)
1 change: 1 addition & 0 deletions tests/unit/test_astrohack_import_template.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest


class TestAstrohack():
@classmethod
def setup_class(cls):
Expand Down
53 changes: 34 additions & 19 deletions tests/unit/test_client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import pytest


class TestAstrohack():
@classmethod
def setup_class(cls):
Expand All @@ -23,80 +24,94 @@ def teardown_method(self):
pass

def test_client_spawn(self):
""" Test client """
"""
Run astrohack_local_client with N cores and with a memory_limit of M GB to create an instance of the
astrohack Dask client.
"""
import distributed

from astrohack.client import astrohack_local_client

DEFAULT_DASK_ADDRESS="127.0.0.1:8786"
DEFAULT_DASK_ADDRESS = "127.0.0.1:8786"

log_parms = {'log_level':'DEBUG'}
log_parms = {'log_level': 'DEBUG'}

client = astrohack_local_client(cores=2, memory_limit='8GB', log_parms=log_parms)

if not distributed.client._get_global_client():
try:
distributed.Client(DEFAULT_DASK_ADDRESS, timeout=2)

except OSError:
assert False

finally:
client.shutdown()

client.shutdown()

def test_client_dask_dir(self):
""" Test client """
"""
Run astrohack_local_client with N cores and with a memory_limit of M GB to create an instance of the
astrohack Dask client. Check that temporary files are written to dask_local_dir.
"""
import distributed

from astrohack.client import astrohack_local_client

DEFAULT_DASK_ADDRESS="127.0.0.1:8786"
DEFAULT_DASK_ADDRESS = "127.0.0.1:8786"

log_parms = {'log_level':'DEBUG'}
log_parms = {'log_level': 'DEBUG'}

client = astrohack_local_client(
cores=2,
memory_limit='8GB',
log_parms=log_parms,
dask_local_dir='./dask_test_dir'
)

client = astrohack_local_client(cores=2, memory_limit='8GB', log_parms=log_parms, dask_local_dir='./dask_test_dir')

try:
if os.path.exists('./dask_test_dir') is False:
raise FileNotFoundError

except FileNotFoundError:
assert False

finally:
client.shutdown()

def test_client_logger(self):
""" Test client """
"""
Run astrohack_local_client with N cores and with a memory_limit of M GB without any errors and the messages
will be logged in the terminal.
"""
import os
import re
import distributed

from astrohack.client import astrohack_local_client

DEFAULT_DASK_ADDRESS="127.0.0.1:8786"
DEFAULT_DASK_ADDRESS = "127.0.0.1:8786"

log_parms = {
'log_level':'DEBUG',
'log_to_file':True,
'log_level': 'DEBUG',
'log_to_file': True,
'log_file': 'astrohack_log_file'
}

client = astrohack_local_client(cores=2, memory_limit='8GB', log_parms=log_parms)

files = os.listdir(".")

try:
for file in files:
if re.match("^astrohack_log_file+[0-9].*log", file) is not None:
return

raise FileNotFoundError

except FileNotFoundError:
assert False

finally:
client.shutdown()
client.shutdown()
22 changes: 11 additions & 11 deletions tests/unit/test_dio.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from astrohack.panel import panel
from astrohack import holog


class TestAstrohackDio():
datafolder = 'dioData'
holog_mds = dict()
Expand All @@ -21,16 +22,15 @@ class TestAstrohackDio():

@classmethod
def setup_class(cls):

astrohack.data.datasets.download(file="ea25_cal_small_after_fixed.split.ms", folder=cls.datafolder)

extract_pointing(
ms_name=cls.datafolder + '/ea25_cal_small_after_fixed.split.ms',
point_name=cls.datafolder + '/ea25_cal_small_after_fixed.split.point.zarr',
parallel=True,
overwrite=True
)

cls.holog_mds = extract_holog(
ms_name=cls.datafolder + '/ea25_cal_small_after_fixed.split.ms',
point_name=cls.datafolder + '/ea25_cal_small_after_fixed.split.point.zarr',
Expand Down Expand Up @@ -58,32 +58,32 @@ def setup_class(cls):
@classmethod
def teardown_class(cls):
shutil.rmtree(cls.datafolder)

def test_open_holog(self):
'''Open a holog file and return a holog data object'''
holog_data = open_holog(self.datafolder + '/ea25_cal_small_after_fixed.split.holog.zarr')

assert holog_data == self.holog_mds

def test_open_image(self):
'''Open an image file and return an image data object'''
image_data = open_image(self.datafolder + '/ea25_cal_small_after_fixed.split.image.zarr')

assert image_data == self.image_mds

def test_open_panel(self):
'''Open a panel file and return a panel data object'''
panel_data = open_panel(self.datafolder + '/ea25_cal_small_after_fixed.split.panel.zarr')

assert panel_data == self.panel_mds

def test_open_pointing(self):
'''Open a pointing file and return a pointing data object'''
pointing_data = open_pointing(self.datafolder + '/ea25_cal_small_after_fixed.split.point.zarr')
# check if keys match expected?
# How to check xarray content...

expected_keys = ['point_meta_ds', 'ant_ea25', 'ant_ea04', 'ant_ea06']

for key in pointing_data.keys():
assert key in expected_keys
Loading