# Path: astroNN/config.py
# def config_path(flag=None):
# def magic_num_reader():
# def multiprocessing_flag_reader():
# def envvar_warning_flag_reader():
# def custom_model_path_reader():
# def cpu_gpu_reader():
# def cpu_gpu_check():
# def tf_patch():
# def tf_unpatch():
# def __tf_patches(method='patch'):
#     def __master_patch(path, diffpatch):
# MAGIC_NUMBER = magic_num_reader()
# MULTIPROCESS_FLAG = multiprocessing_flag_reader()
# ENVVAR_WARN_FLAG = envvar_warning_flag_reader()
# CUSTOM_MODEL_PATH = custom_model_path_reader()
# 
# Path: astroNN/shared/downloader_tools.py
# class TqdmUpTo(tqdm):
#     """
#     NAME:
#         sha256_checksum
#     PURPOSE:
#         Provides `update_to(n)` which uses `tqdm.update(delta_n)`.
#     INPUT:
#     OUTPUT:
#     HISTORY:
#         2017-Oct-25 - Written - Henry Leung (University of Toronto)
#     """
# 
#     def update_to(self, b=1, bsize=1, tsize=None):
#         """
#         b  : int, optional
#             Number of blocks transferred so far [default: 1].
#         bsize  : int, optional
#             Size of each block (in tqdm units) [default: 1].
#         tsize  : int, optional
#             Total size (in tqdm units). If [default: None] remains unchanged.
#         """
#         if tsize is not None:
#             self.total = tsize
#         self.update(b * bsize - self.n)  # will also set self.n = b * bsize
# 
# def filehash(filename, block_size=65536, algorithm='sha256'):
#     """
#     Computes the hash value for a file by using a specified hash algorithm.
# 
#     :param filename: filename
#     :type filename: str
#     :param block_size: blocksize used to compute file hash
#     :type block_size: int
#     :param algorithm: hash algorithms like 'sha256' or 'md5' etc.
#     :type algorithm: str
#     :return: None
#     :History: 2019-Mar-12 - Written - Henry Leung (University of Toronto)
#     """
#     algorithm = algorithm.lower()
#     if algorithm not in hashlib.algorithms_guaranteed:
#         raise ValueError(f"{algorithm} is an unsupported hashing algorithm")
# 
#     func_algorithm = getattr(hashlib, algorithm)()
#     with open(filename, 'rb') as f:
#         for block in iter(lambda: f.read(block_size), b''):
#             func_algorithm.update(block)
#     return func_algorithm.hexdigest()


import os
import urllib.request
import h5py
import numpy as np
    import pylab as plt
from astroNN.config import astroNN_CACHE_DIR
from astroNN.shared.downloader_tools import TqdmUpTo, filehash
        y (ndarray): An array of answer
    HISTORY:
        2021-Mar-24 - Written - Henry Leung (University of Toronto)
    """

    filename = 'Galaxy10_DECals.h5'

    complete_url = _G10_ORIGIN + filename

    datadir = os.path.join(astroNN_CACHE_DIR, 'datasets')
    file_hash = '19AEFC477C41BB7F77FF07599A6B82A038DC042F889A111B0D4D98BB755C1571'  # SHA256

    # Notice python expect sha256 in lowercase

    if not os.path.exists(datadir):
        os.makedirs(datadir)
    fullfilename = os.path.join(datadir, filename)

    # Check if files exists
    if os.path.isfile(fullfilename) and flag is None:
        checksum = filehash(fullfilename, algorithm='sha256')
        if checksum != file_hash.lower():
            print('File corruption detected, astroNN is attempting to download again')
            load_data(flag=1)
        else:
            print(fullfilename + ' was found!')
    elif not os.path.isfile(fullfilename) or flag == 1:
        with TqdmUpTo(unit='B', unit_scale=True, miniters=1, desc=complete_url.split('/')[-1]) as t:
            urllib.request.urlretrieve(complete_url, fullfilename, reporthook=t.update_to)
            print(f'Downloaded Galaxy10 successfully to {fullfilename}')

# Path: astroNN/config.py
# def config_path(flag=None):
# def magic_num_reader():
# def multiprocessing_flag_reader():
# def envvar_warning_flag_reader():
# def custom_model_path_reader():
# def cpu_gpu_reader():
# def cpu_gpu_check():
# def tf_patch():
# def tf_unpatch():
# def __tf_patches(method='patch'):
#     def __master_patch(path, diffpatch):
# MAGIC_NUMBER = magic_num_reader()
# MULTIPROCESS_FLAG = multiprocessing_flag_reader()
# ENVVAR_WARN_FLAG = envvar_warning_flag_reader()
# CUSTOM_MODEL_PATH = custom_model_path_reader()
# 
# Path: astroNN/shared/downloader_tools.py
# class TqdmUpTo(tqdm):
#     """
#     NAME:
#         sha256_checksum
#     PURPOSE:
#         Provides `update_to(n)` which uses `tqdm.update(delta_n)`.
#     INPUT:
#     OUTPUT:
#     HISTORY:
#         2017-Oct-25 - Written - Henry Leung (University of Toronto)
#     """
# 
#     def update_to(self, b=1, bsize=1, tsize=None):
#         """
#         b  : int, optional
#             Number of blocks transferred so far [default: 1].
#         bsize  : int, optional
#             Size of each block (in tqdm units) [default: 1].
#         tsize  : int, optional
#             Total size (in tqdm units). If [default: None] remains unchanged.
#         """
#         if tsize is not None:
#             self.total = tsize
#         self.update(b * bsize - self.n)  # will also set self.n = b * bsize
# 
# def filehash(filename, block_size=65536, algorithm='sha256'):
#     """
#     Computes the hash value for a file by using a specified hash algorithm.
# 
#     :param filename: filename
#     :type filename: str
#     :param block_size: blocksize used to compute file hash
#     :type block_size: int
#     :param algorithm: hash algorithms like 'sha256' or 'md5' etc.
#     :type algorithm: str
#     :return: None
#     :History: 2019-Mar-12 - Written - Henry Leung (University of Toronto)
#     """
#     algorithm = algorithm.lower()
#     if algorithm not in hashlib.algorithms_guaranteed:
#         raise ValueError(f"{algorithm} is an unsupported hashing algorithm")
# 
#     func_algorithm = getattr(hashlib, algorithm)()
#     with open(filename, 'rb') as f:
#         for block in iter(lambda: f.read(block_size), b''):
#             func_algorithm.update(block)
#     return func_algorithm.hexdigest()


# Path: astroNN/datasets/galaxy10.py
import os
import urllib.request
import h5py
import numpy as np
    import pylab as plt
from astroNN.config import astroNN_CACHE_DIR
from astroNN.shared.downloader_tools import TqdmUpTo, filehash

        y (ndarray): An array of answer
    HISTORY:
        2021-Mar-24 - Written - Henry Leung (University of Toronto)
    """

    filename = 'Galaxy10_DECals.h5'

    complete_url = _G10_ORIGIN + filename

    datadir = os.path.join(astroNN_CACHE_DIR, 'datasets')
    file_hash = '19AEFC477C41BB7F77FF07599A6B82A038DC042F889A111B0D4D98BB755C1571'  # SHA256

    # Notice python expect sha256 in lowercase

    if not os.path.exists(datadir):
        os.makedirs(datadir)
    fullfilename = os.path.join(datadir, filename)

    # Check if files exists
    if os.path.isfile(fullfilename) and flag is None:
        checksum = filehash(fullfilename, algorithm='sha256')
        if checksum != file_hash.lower():
            print('File corruption detected, astroNN is attempting to download again')
            load_data(flag=1)
        else:
            print(fullfilename + ' was found!')
    elif not os.path.isfile(fullfilename) or flag == 1:
        with TqdmUpTo(unit='B', unit_scale=True, miniters=1, desc=complete_url.split('/')[-1]) as t:
            urllib.request.urlretrieve(complete_url, fullfilename, reporthook=t.update_to)
            print(f'Downloaded Galaxy10 successfully to {fullfilename}')

            checksum = filehash(fullfilename, algorithm='sha256')