From e4fafe72a4153e15e379c0405f50a73e3464712f Mon Sep 17 00:00:00 2001 From: Rachel Mandelbaum Date: Tue, 2 Aug 2016 10:53:35 -0400 Subject: [PATCH 1/4] code updates in sunpy__load.py for compatibility with more NumPy versions, etc. --- sunpy__load.py | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/sunpy__load.py b/sunpy__load.py index a8709f5..6ecd032 100644 --- a/sunpy__load.py +++ b/sunpy__load.py @@ -53,12 +53,24 @@ def load_broadband_image(filename,band=0, **kwargs): The band can be specified as a number or a string (must match the "band_names") """ band_images = load_all_broadband_images(filename, **kwargs) + # This code change is needed because currently load_all_broadband_images seems to have been + # changed to return a tuple, but the code later in this routine assumes that it just returns an + # array. So check for this and get the array if a tuple was returned. + if isinstance(band_images, tuple): + if isinstance(band_images[0], np.ndarray): + band_images = band_images[0] band_names = load_broadband_names(filename) if type(band) is int: return_image = band_images[band,:,:] else: - band = (((band_names == band).nonzero())[0])[0] + # The original version of this code fails for some NumPy versions. New version will work for + # more systems (compatible with original version but also uses syntax that will work for other + # NumPy versions if an error is encountered. + try: + band = (((band_names == band).nonzero())[0])[0] + except AttributeError: + band = (((np.array(band_names) == band).astype(int).nonzero())[0])[0] return_image = band_images[band,:,:] return return_image @@ -160,7 +172,13 @@ def load_broadband_effective_wavelengths(filename,band=None): name_array = name_array[band] else: band_names = load_broadband_names(filename) - band_index = (((band_names == band).nonzero())[0])[0] + # The original version of this code fails for some NumPy versions. New version will work for + # more systems (compatible with original version but also uses syntax that will work for other + # NumPy versions if an error is encountered. + try: + band_index = (((band_names == band).nonzero())[0])[0] + except AttributeError: + band_index = (((np.array(band_names) == band).astype(int).nonzero())[0])[0] name_array = name_array[band_index] band=None @@ -191,13 +209,25 @@ def load_all_broadband_images(filename,camera=0,openlist=None): def load_broadband_image(filename,band=0,camera=0): band_images = load_all_broadband_images(filename,camera=camera) + # This code change is needed because currently load_all_broadband_images seems to have been + # changed to return a tuple, but the code later in this routine assumes that it just returns an + # array. So check for this and get the array if a tuple was returned. + if isinstance(band_images, tuple): + if isinstance(band_images[0], np.ndarray): + band_images = band_images[0] band_names = load_broadband_names(filename) if type(band) is int: return_image = band_images[band,:,:] else: - band = (((band_names == band).nonzero())[0])[0] - return_image = band_images[band,:,:] + # The original version of this code fails for some NumPy versions. New version will work for + # more systems (compatible with original version but also uses syntax that will work for other + # NumPy versions if an error is encountered. + try: + band_index = (((band_names == band).nonzero())[0])[0] + except AttributeError: + band_index = (((np.array(band_names) == band).astype(int).nonzero())[0])[0] + return_image = band_images[int(band_index),:,:] return return_image From 6eaf8fcc4f98c97b6fe845ebc505d27a37c275d9 Mon Sep 17 00:00:00 2001 From: Rachel Mandelbaum Date: Tue, 2 Aug 2016 10:54:43 -0400 Subject: [PATCH 2/4] updated sunpy__plot.py to account for changes in sunpy__synthetic_image.py --- sunpy__plot.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/sunpy__plot.py b/sunpy__plot.py index 55da529..4b411b6 100644 --- a/sunpy__plot.py +++ b/sunpy__plot.py @@ -117,8 +117,11 @@ def return_synthetic_sdss_gri_img(filename, n_iter+=1 - - b_image, rp, the_used_seed,this_fail_flag = sunpy__synthetic_image.build_synthetic_image(filename, 'g_SDSS.res', + # The calls to build_synthetic_image had to be updated to account for changes in the number + # of return values (previously 4, now 6). We don't need the other 2 values here, but we + # have to catch them so the code won't complain about too many return values to unpack. + b_image, rp, the_used_seed,this_fail_flag, _, _ = \ + sunpy__synthetic_image.build_synthetic_image(filename, 'g_SDSS.res', seed=seed, r_petro_kpc=r_petro_kpc, fix_seed=False, @@ -126,7 +129,8 @@ def return_synthetic_sdss_gri_img(filename, if(this_fail_flag): fail_flag=True - g_image, dummy, the_used_seed,this_fail_flag = sunpy__synthetic_image.build_synthetic_image(filename, 'r_SDSS.res', + g_image, dummy, the_used_seed,this_fail_flag, _, _ = \ + sunpy__synthetic_image.build_synthetic_image(filename, 'r_SDSS.res', seed=the_used_seed, r_petro_kpc=rp, fix_seed=True, @@ -134,7 +138,8 @@ def return_synthetic_sdss_gri_img(filename, if(this_fail_flag): fail_flag=True - r_image, dummy, the_used_seed, this_fail_flag = sunpy__synthetic_image.build_synthetic_image(filename, 'i_SDSS.res', + r_image, dummy, the_used_seed, this_fail_flag, _, _ = \ + sunpy__synthetic_image.build_synthetic_image(filename, 'i_SDSS.res', seed=the_used_seed, r_petro_kpc=rp, fix_seed=True, From 002255b02eea5ca70dfc3d98ada18dc4b803cfe5 Mon Sep 17 00:00:00 2001 From: Rachel Mandelbaum Date: Tue, 2 Aug 2016 10:55:07 -0400 Subject: [PATCH 3/4] note problem with error message --- sunpy__synthetic_image.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sunpy__synthetic_image.py b/sunpy__synthetic_image.py index b89a776..688b53a 100644 --- a/sunpy__synthetic_image.py +++ b/sunpy__synthetic_image.py @@ -476,6 +476,8 @@ def add_background(self, seed=1, add_background=True, rebin_gz=False, n_target_p #=== load *full* bg image, and its properties ===# bg_filename = (backgrounds[self.band])[0] if not (os.path.isfile(bg_filename)): + ## Note from RM: If I try to wget the files from the URLs given below, I get + ## "Not Found" errors. So I think these are wrong and should be updated. print " Background files were not found... " print " The standard files used in Torrey al. (2015), Snyder et al., (2015) and Genel et al., (2014) ..." print " can be downloaded using the download_backgrounds routine or manually from: " From c078cb1239fbba1af1f298d6acd1b08990dd1440 Mon Sep 17 00:00:00 2001 From: Rachel Mandelbaum Date: Tue, 2 Aug 2016 11:01:58 -0400 Subject: [PATCH 4/4] fix some issues in sample_code_1.py --- examples/sample_code_1.py | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/examples/sample_code_1.py b/examples/sample_code_1.py index 5d96a8c..ed1f47f 100644 --- a/examples/sample_code_1.py +++ b/examples/sample_code_1.py @@ -10,6 +10,9 @@ import numpy as np import os +# The two lines below make it possible for people to sit in the examples directory and run the scripts. +import sys +sys.path.append('../..') import sunpy.sunpy__load as sunpy__load # import sunpy.sunpy__plot as sunpy__plot @@ -18,14 +21,28 @@ dl_base='http://www.illustris-project.org' try: - catalog = np.loadtxt('directory_catalog_135.txt', - dtype={'names' : ('subdirs', 'galaxy_numbers', 'galaxy_masses'), - 'formats': ('S3', 'i10', 'f8')}) -except: + # Depending on the NumPy version, the line below can raise a TypeError even if the file is + # present. The try/except block enables us to try loadtxt calls that work for more NumPy + # versions. + try: + catalog = np.loadtxt('directory_catalog_135.txt', + dtype={'names' : ('subdirs', 'galaxy_numbers', 'galaxy_masses'), + 'formats': ('S3', 'i10', 'f8')}) + except TypeError: + catalog = np.loadtxt('directory_catalog_135.txt', + dtype={'names' : ('subdirs', 'galaxy_numbers', 'galaxy_masses'), + 'formats': ('S3', 'i8', 'f8')}) +except IOError: + # If we don't already have the file, we have to download it and try again. os.system("wget "+dl_base+"/files/directory_catalog_135.txt") - catalog = np.loadtxt('directory_catalog_135.txt', - dtype={'names' : ('subdirs', 'galaxy_numbers', 'galaxy_masses'), - 'formats': ('S3', 'i10','f8')}) + try: + catalog = np.loadtxt('directory_catalog_135.txt', + dtype={'names' : ('subdirs', 'galaxy_numbers', 'galaxy_masses'), + 'formats': ('S3', 'i10','f8')}) + except TypeError: + catalog = np.loadtxt('directory_catalog_135.txt', + dtype={'names' : ('subdirs', 'galaxy_numbers', 'galaxy_masses'), + 'formats': ('S3', 'i8','f8')}) all_subdirs = catalog['subdirs']