def load_test_pngs(folder: str) -> list: if not os.path.exists(folder): raise FileNotFoundError("Supplied folder not found: {}".format(folder)) return_arr = np.ndarray((28, 28, 0)) x = 0 cv.startWindowThread() for image_path in glob.glob(folder + "/*.png"): logging("Files").info("File {}: {}".format(x, image_path)) filenames.append(image_path) image = misc.imread(image_path, flatten=False) image = cv.cvtColor(image, cv.COLOR_BGR2GRAY) image = image.astype('float32') image /= 255.0 image = 1.0 - image return_arr = np.dstack((return_arr, image)) x += 1 return_arr = return_arr.swapaxes(0, 2) return_arr = return_arr.swapaxes(1, 2) return_arr = return_arr.reshape(return_arr.shape[0], 28, 28, 1) for x in range(0, 10): cv.imshow("Test", return_arr[x]) cv.waitKey() logging("TEST").info("Image Shape: {}".format(return_arr.shape)) return return_arr.astype('float32')
def __init__(self, value, min_value=None, max_value=None): """Constructor for parameter. :param value: Can be anything. :type value: Can be anything. """ self.value = value if not isinstance(value, numbers.Number) and (min_value is not None or max_value is not None): logging("Config").error('Supplied a max or min value for a non-numerical parameter')
def load(self): """Loads the configuration from disk and populates the param dict with the values. :raises OSError: If file cannot be read/found. """ try: with open(self.filename, 'r') as config_file: serialized = config_file.read() if len(serialized) < 10: self.params = {} else: self.params = yaml.load(serialized) except OSError: logging("Config").error("Error encountered reading config file: {0}".format(self.filename))
def __init__(self, name: str, root: str, directory: str =''): """Initializer for config, creates directories and loads any pre-existing configuration files associated with the name. :param str name: The name of the config file, also the name of the produced .yaml file. :param str root: The root directory for the configuration. :param str directory: The directory where the config file should be stored. Set to ".//config//" by default :raises OSError: If it cannot create a directory. """ # Test for string if not isinstance(directory, str): raise ValueError("Directory is not a string") if not isinstance(name, str): raise ValueError("Config name must be a string.") self.name = name self.root_dir = root if not os.path.exists(self.root_dir): os.mkdir(self.root_dir) if len(directory) > 1: self.directory = self.root_dir + "/" + directory + "/" else: self.directory = self.root_dir + "/config/" if not os.path.exists(self.directory): os.makedirs(self.directory) if not os.path.exists(self.directory): raise OSError("Could not create directory: " + self.directory) self.filename = self.directory + self.name + ".yaml" self.keys_added = [] if not os.path.exists(self.filename): logging("Config").info("Could not find pre-existing configuration [{}], using defaults.".format(name)) self.params = {} else: logging("Config").info("Found existing configuration [{}], loading values.".format(name)) self.load()
def save(self): """Serializes and saves the parameter dict to disk using YAML. :raises OSError: If unable to save file to disk. """ keys_to_remove = [] # Cleanup un-used params for key in self.params: if key not in self.keys_added: keys_to_remove.append(key) for value in keys_to_remove: del self.params[value] serialized_str = yaml.dump(self.params) try: with open(self.filename, 'w') as config_file: config_file.write(serialized_str) except OSError: logging("Config").error("Error encountered saving config file: {0}".format(self.filename))
def make_directory(directory: str) -> bool: """ Creates supplied directory and checks to make sure it exists. Args: directory (str): Directory to create. Return: True on success. """ if not os.path.exists(directory): # Try to make directory try: os.mkdir(str(directory)) except Exception as e: logging("Utility").error( "Could not create directory:\n\t> {}\n\t> Exception: {}". format(directory, str(e))) return False if not os.path.exists(directory): logging("Utility").error( "Could not create directory:\n\t> {}".format(directory)) return False return True
def run(self): """Runs the thread.""" logging("Thread ({})".format(self._name)).info("Starting thread...") while True: self._target() # If no kill signal is set, sleep for the interval, # If kill signal comes in while sleeping, immediately # wake up and handle is_triggerer = self._trigger.wait(self._interval) if is_triggerer: if self._kill: break else: logging("Thread ({}) has been triggered!".format( self._name)) self._trigger.clear() logging("Thread ({})".format(self._name)).info("Killing thread...")
def copy_file_to_shares(source_file: str, dest_folder: str, dest_filename: str = "") -> bool: """Copies supplied source file to destination file in shares folder. :param source_file: Source file to copy. :type source_file: str :param dest_folder: Destination folder to copy to. :type dest_folder: str Args: source_file (str): dest_folder (str): Returns: True on success. """ # Get file name from source path file_name = Path(source_file).name # Check if file exists if not file_exists(source_file): logging("Utility").warn( "Supplied file does not exist:\n\t> {}".format(source_file)) return False # Check if shares directory is set if SharesDirectory is None or len(SharesDirectory) < 3: logging("Utility").error( "SharesDirectory is not set! Cannot copy file to shares directory:\n\t> {}" .format(source_file)) return False # Check if shares directory exists or not if not make_directory(SharesDirectory): logging("Utility").error( "Could not verify shares directory exists:\n\t> {}".format( SharesDirectory)) return False # Get endpoint path using shares directory, destination folder, and file name extracted from source file. end_dir = SharesDirectory + "/" + dest_folder end_file_path = end_dir + "/" + file_name if len(dest_filename) > 3: end_file_path = end_dir + "/" + dest_filename if not make_directory(end_dir): logging("Utility").error( "Could not verify that end-point directory exists:\n\t> {}".format( end_dir)) return False try: shutil.copyfile(source_file, end_file_path) except Exception as e: logging("Utility").error( "Could not copy file to: \n\t> {} \n\t> Exception: {}".format( end_file_path, str(e))) return False logging("Uility").info( "Successfully copied local file to shares.\n\t> Local File: {}\n\t> Remote File: {}" .format(source_file, end_file_path)) return True
config.save() # ----------------------------- # Initialize TensorFlow Session # ----------------------------- tf_config = tf.ConfigProto() tf_config.gpu_options.allow_growth = True # tf_config.log_device_placement = True sess = tf.Session(config=tf_config) set_session(sess) # ----------------------------- # Initialize Data # ----------------------------- logging("Data").info("Initializing Data...") # Load pre-shuffled MNIST data (x_train, y_train), (x_test, y_test) = mnist.load_data() # Should print 60000, 28, 28 logging("Data").info("x_train shape: {}".format(x_train.shape)) logging("Data").info("Reshaping...") if K.image_data_format() == 'channels_first': x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) input_shape = (1, img_rows, img_cols) else: x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) input_shape = (img_rows, img_cols, 1)
def __init__(self, midi_root_dir: str, data_output_dir: str): self.midi_paths = [] self.midi_data = [] self.log = logging("MidiReader") self.data_out_dir = data_output_dir self.midi_root_dir = midi_root_dir
# # File Description: # Main application entry point, also used for testing. # from utils import logger from utils.logger import get_logger as logging from utils import utility from config import GeneralCfg from data_parsing.midi_reader import MidiReader if __name__ == '__main__': logger.initialize("Orchestrion", GeneralCfg.log_to_disk.value, utility.get_root_dir()) log = logging("App") log.info("Starting Orchestrion Application:\n" "\n" "Application Details:\n" "__________________________________________________\n\n" "\t> Version: {}\n" "\t> Author: {}\n" "\t> Copyright Year: {}\n" "__________________________________________________\n".format( GeneralCfg.version.value, GeneralCfg.author.value, GeneralCfg.copyright_year.value)) midi_root = utility.get_root_dir() + "/data/midis/" midi_reader = MidiReader( midi_root, "{}/data/generated".format(utility.get_root_dir())) midi_reader.add_midi_to_file_list("bach7.mid")