Skip to content

Commit

Permalink
Merge pull request #84 from syasini/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
syasini committed Oct 19, 2020
2 parents 3bde3b0 + f16e083 commit e0551ab
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 40 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/python-package.yml
@@ -0,0 +1,39 @@
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: Python package

on:
push:
branches: [ develop, master ]
pull_request:
branches: [ develop, master ]

jobs:
build:

runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.6, 3.7, 3.8]

steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: |
pytest
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -5,7 +5,7 @@ _A python package for painting the sky_

[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/syasini/AstroPaint/master?filepath=tutorial.ipynb)
[![Documentation Status](https://readthedocs.org/projects/astropaint/badge/?version=master)](https://astropaint.readthedocs.io/en/master/?badge=master)

![Python package](https://github.com/syasini/AstroPaint/workflows/Python%20package/badge.svg?branch=develop&event=push)

You can install **AstroPaint** by running the following in the command line:

Expand Down Expand Up @@ -126,7 +126,7 @@ canvas.show_map("cartview", lonra=[0,10], latra=[0,10])
_Voila!_

You can use the `n_cpus` argument in the spray function to paint in parallel and speed things up!
The default value `n_cpus=-1` uses all the available cpus.
Setting `n_cpus=-1` uses all the available cpus.

<p align="center">
<img src="images/parallel.gif" alt="parallel" width="450"/>
Expand Down Expand Up @@ -236,7 +236,7 @@ Let's paint this on a 5x5 sqr deg patch of the WebSky catalog with a mass
cut of 8E13 M_sun.

```python
catalog = Catalog("websky_lite_redshift")
catalog = Catalog("WebSky_lite")
catalog.cut_lon_lat(lon_range=[5,10], lat_range=[5,10])
catalog.cut_M_200c(8E13)

Expand Down
86 changes: 86 additions & 0 deletions astropaint/lib/datasets.py
@@ -0,0 +1,86 @@
# @purpose: Get the necessary DataSets

__author__ = "Siavash Yasini, Shobeir K. S. Mazinani"
__email__ = "yasini@usc.edu , shobeir.mazinani@gmail.com"
__credits__ = ['Siavash Yasini', 'Shobeir K. S. Mazinani']


import requests
from tqdm import tqdm
import os



#########################################################
# Get the necessary datasets
#########################################################


def _get_dataset(file_location, ds_url, dataset_name=None):
"""
@purpose: Connect and Download the necessary files and show progressbar
@input:
file_location: str: file location to download dataset
ds_url: str: link to the datasets
dataset_name: str: name of the dataset
"""

response = requests.get(ds_url, stream=True)

if dataset_name is None:
dataset_name = file_location

# Download and write the file in chuncks and show progressbar
with tqdm.wrapattr(open(file_location, "wb"), "write", miniters=1,
total=int(response.headers.get('content-length', 0)),
desc=dataset_name) as fout:
for chunk in response.iter_content(chunk_size=4096):
fout.write(chunk)


def download(dataset_name:str='All', overwrite = True):
"""
@purpose: Download datasets mannualy if not exists into data folder.
@Input: Input a string:
"All" : Downloads all the relevant datasets.
"WebSky_lite" : 'https://media.githubusercontent.com/media/syasini/AstroPaint/master/astropaint/data/WebSky_lite.csv',
"WebSky_2x2" : 'https://media.githubusercontent.com/media/syasini/AstroPaint/master/astropaint/data/WebSky_2x2.csv',
"Sehgal" : 'https://media.githubusercontent.com/media/syasini/AstroPaint/master/astropaint/data/Sehgal.csv',
"MICE" : 'https://media.githubusercontent.com/media/syasini/AstroPaint/master/astropaint/data/MICE.csv'
"""
datasets_url_dict = {
"WebSky_lite" : 'https://media.githubusercontent.com/media/syasini/AstroPaint/master/astropaint/data/WebSky_lite.csv',
"WebSky_2x2" : 'https://media.githubusercontent.com/media/syasini/AstroPaint/master/astropaint/data/WebSky_2x2.csv',
"Sehgal" : 'https://media.githubusercontent.com/media/syasini/AstroPaint/master/astropaint/data/Sehgal.csv',
"MICE" : 'https://media.githubusercontent.com/media/syasini/AstroPaint/master/astropaint/data/MICE.csv',
}

# make a list of all the datasets to be downloaded
download_list = []

# Generate a list of datasets that needs to be downloaded

if dataset_name == 'All':
download_list = [ds_name for ds_name in datasets_url_dict.keys()]
elif type(dataset_name) == list:
download_list = dataset_name
elif type(dataset_name) == str:
download_list.append(dataset_name)
else:
print("Please check the list of available options and try again")
raise NameError(f'{dataset_name} is not valid!')

for dataset in download_list:
file_location = os.path.join(os.path.dirname(__file__), os.path.pardir,'data',f'{dataset}.csv')
if overwrite:
_get_dataset(file_location, datasets_url_dict[dataset], dataset)
else:
if os.path.getsize(file_location) >= 1000000:
_get_dataset(file_location, datasets_url_dict[dataset], dataset)


print('Done!')

48 changes: 26 additions & 22 deletions astropaint/paint_bucket.py
Expand Up @@ -1174,7 +1174,7 @@ def find_discs_2center_vec(self):

def gen_center_ipix(self, halo_list="All"):
"""generate ipix of the halo centers"""
if halo_list is "All":
if halo_list =="All":
halo_list = range(self.catalog.size)

assert hasattr(halo_list, '__iter__')
Expand All @@ -1188,7 +1188,7 @@ def gen_center_ang(self, halo_list="All", snap2pixel=False):
"""generate the angle (theta, phi) of the halo centers
if snap2pixel is True, the angular coordinate of the halo center pixel will be
returned"""
if halo_list is "All":
if halo_list == "All":
halo_list = range(self.catalog.size)

assert hasattr(halo_list, '__iter__')
Expand All @@ -1203,7 +1203,7 @@ def gen_center_ang(self, halo_list="All", snap2pixel=False):
self.catalog.data.phi[halo])

def gen_center_vec(self, halo_list="All"):
if halo_list is "All":
if halo_list == "All":
halo_list = range(self.catalog.size)

assert hasattr(halo_list, '__iter__')
Expand All @@ -1213,7 +1213,7 @@ def gen_center_vec(self, halo_list="All"):
self.catalog.data.phi[halo])

def gen_pixel_index(self, halo_list="All"):
if halo_list is "All":
if halo_list == "All":
halo_list = range(self.catalog.size)

assert hasattr(halo_list, '__iter__')
Expand All @@ -1229,7 +1229,7 @@ def gen_pixel_index(self, halo_list="All"):
)

def gen_pixel_ang(self, halo_list="All"):
if halo_list is "All":
if halo_list == "All":
halo_list = range(self.catalog.size)

assert hasattr(halo_list, '__iter__')
Expand All @@ -1245,7 +1245,7 @@ def gen_pixel_vec(self, halo_list="All"):
-------
None
"""
if halo_list is "All":
if halo_list == "All":
halo_list = range(self.catalog.size)

assert hasattr(halo_list, '__iter__')
Expand All @@ -1254,7 +1254,7 @@ def gen_pixel_vec(self, halo_list="All"):
yield np.asarray(hp.pix2vec(self.nside, index)).T

def gen_cent2pix_rad(self, halo_list="All"):
if halo_list is "All":
if halo_list == "All":
halo_list = range(self.catalog.size)

assert hasattr(halo_list, '__iter__')
Expand All @@ -1264,7 +1264,7 @@ def gen_cent2pix_rad(self, halo_list="All"):
yield hp.rotator.angdist(np.squeeze(pixel_ang), center_ang)

def gen_cent2pix_mpc(self, halo_list="All"):
if halo_list is "All":
if halo_list == "All":
halo_list = range(self.catalog.size)

assert hasattr(halo_list, '__iter__')
Expand All @@ -1273,15 +1273,15 @@ def gen_cent2pix_mpc(self, halo_list="All"):
yield self.center_D_a[halo] * pix2cent_rad

def gen_cent2pix_hat(self, halo_list="All"):
if halo_list is "All":
if halo_list == "All":
halo_list = range(self.catalog.size)

for (pix_vec, cent_vec) in zip(self.gen_pixel_vec(halo_list),
self.gen_center_vec(halo_list)):
yield Canvas._normalize_vec(pix_vec - cent_vec)

def gen_cent2pix_mpc_vec(self, halo_list="All"):
if halo_list is "All":
if halo_list == "All":
halo_list = range(self.catalog.size)

for (halo, pix_vec, cent_vec) in zip(halo_list,
Expand Down Expand Up @@ -1446,7 +1446,8 @@ def set_to_1(disc):

[set_to_1(disc) for disc in self.discs.gen_pixel_index()]

if graticule: hp.graticule()
if graticule:
hp.graticule()

self._viewer(junk_pixels,
projection=projection,
Expand Down Expand Up @@ -1662,8 +1663,11 @@ def linear_transform(patch, slope, intercept):

if lat_range is None:
lat_range = lon_range
if halo_list is "all":
halo_list = range(self.catalog.size)
try:
if halo_list == "all":
halo_list = range(self.catalog.size)
except ValueError:
...
#pdb.set_trace()

# make sure that apply func and func_kwargs are lists for consistency
Expand Down Expand Up @@ -1813,7 +1817,7 @@ def linear_transform(patch, slope, intercept):

# None values of lat_range will be fixed in cutouts()

if halo_list is "all":
if halo_list == "all":
halo_list = range(self.catalog.size)

if ypix is None:
Expand Down Expand Up @@ -2014,7 +2018,7 @@ def add_cmb(self,

if lmax is None:
lmax = 3 * self.nside -1
if Cl is "LCDM":
if Cl == "LCDM":
# TODO: add lmax implementation
Cl_file = utils.get_CMB_Cl(lmax=lmax, mode=mode)
Cl = Cl_file
Expand Down Expand Up @@ -2292,7 +2296,7 @@ def template(self, val):
def spray(self,
canvas,
distance_units="Mpc",
n_cpus=-1,
n_cpus=1,
cache=False,
lazy=False,
lazy_grid=None,
Expand Down Expand Up @@ -2339,14 +2343,14 @@ def spray(self,

R_mode = self.template_args_list[self.R_arg_indx]

if R_mode is "R":
if R_mode == "R":
R_pix2cent = canvas.discs.gen_cent2pix_mpc
if R_mode is "R_vec":
if R_mode == "R_vec":
R_pix2cent = canvas.discs.gen_cent2pix_mpc_vec


if cache:
assert R_mode is "R", "cache method is not available for profiles that " \
assert R_mode == "R", "cache method is not available for profiles that " \
"use 'R_vec'"

from joblib import Memory
Expand All @@ -2369,9 +2373,9 @@ def snap2grid(array, grid):

spray_df.loc[:, column] = snap2grid(data, column_grid)

if n_cpus is 0:
if n_cpus == 0:
raise ValueError(f"n_cpus = {n_cpus} is not valid. Please enter a value > 0 for n_cpus or -1 to run on all cores.")
if n_cpus is 1:
if n_cpus == 1:

for halo, R, pixel_index in tqdm(zip(range(canvas.catalog.size),
R_pix2cent(),
Expand All @@ -2395,7 +2399,7 @@ def snap2grid(array, grid):
print("Spraying in parallel...")

# count the number of available cpus
if n_cpus is -1 or n_cpus>(os.cpu_count()):
if n_cpus == -1 or n_cpus > (os.cpu_count()):
n_cpus = (os.cpu_count())

canvas_memory_size = getsizeof(canvas.pixels)
Expand Down
4 changes: 2 additions & 2 deletions docs/index.rst
Expand Up @@ -148,7 +148,7 @@ with. Let's spray ths canvas now:
*Voila!*

You can use the `n_cpus` argument in the spray function to paint in parallel and speed things up!
The default value `n_cpus=-1` uses all the available cpus.
Setting `n_cpus=-1` uses all the available cpus.



Expand Down Expand Up @@ -281,7 +281,7 @@ cut of 8E13 M_sun.

.. code-block:: python
catalog = Catalog("websky_lite_redshift")
catalog = Catalog("WebSky_lite")
catalog.cut_lon_lat(lon_range=[5,10], lat_range=[5,10])
catalog.cut_M_200c(8E13)
Expand Down
28 changes: 15 additions & 13 deletions requirements.txt
@@ -1,14 +1,16 @@
astropy==4.0.0
matplotlib==3.1.2
numpy==1.19.1
pytest==5.3.1
scipy==1.3.0
pandas==1.0.1
tqdm==4.48.0
pyyaml==5.1.2
decorator==4.4.1
seaborn==0.9.0
joblib==0.14.0
requests==2.24.0
astropy>=4.0.0
matplotlib>=3.1.2
numpy>=1.19.1
pytest>=5.3.1
scipy>=1.3.0
pandas>=1.0.1
tqdm>=4.48.0
pyyaml>=5.1.2
decorator>=4.4.1
seaborn>=0.9.0
joblib>=0.14.0
requests>=2.24.0
jupyter
healpy==1.13.0
requests>=2.24.0
healpy==1.13.0

0 comments on commit e0551ab

Please sign in to comment.