Giter VIP home page Giter VIP logo

Comments (2)

zakv avatar zakv commented on August 15, 2024

I dug through the git history a bit and it looks like safe_squeeze() was added in 97d5b23 and then removed in 9835e3f. Then safe_cast_to_array() was added in 3bc0374. It looks like the only difference is that safe_squeeze() gave the option to specify the dtype of the returned array, and ensured it was float by default. I've copy/pasted the code for each function below for reference.

def safe_cast_to_array(in_array):
    '''
    Attempts to safely cast the input to an array. Takes care of border cases
    
    Args:
        in_array (array or equivalent): The array (or otherwise) to be converted to a list.
    
    Returns:
        array : array that has been squeezed and 0-D cases change to 1-D cases
    
    '''
    
    out_array = np.squeeze(np.array(in_array))
    
    if out_array.shape == ():
        out_array = np.array([out_array[()]]) 
    
    return out_array
def safe_squeeze(in_array, set_dtype = float):
    '''
    Attempts to squeeze an array, but has a different behavior for arrays with only a single value. 
    
    Args:
        in_array (array): The array to be squeezed
    
    Returns:
        array: Array.
    '''
    
    out_array = np.squeeze(np.array(in_array, dtype=set_dtype))
    if out_array.shape == ():
        out_array = np.array([out_array[()]])
    
    return out_array

Experimentally I've tried monkey patching things to replace the calls to safe_squeeze() with calls to safe_cast_to_array() and things worked fine. Example code below (file names and types are set elsewhere):

import mloop.visualizations as mlv

# Monkey patch safe_cast_to_array() in place of safe_squeeze() in neural net visualizer code.
mlv.mlu.safe_squeeze = mlv.mlu.safe_cast_to_array

# Make the plots.
mlv.configure_plots()
mlv.create_controller_visualizations(controller_archive, file_type=file_type)
mlv.create_neural_net_learner_visualizations(learner_archive, file_type=file_type)

@michaelhush @charmasaur I can go ahead and replace the calls to safe_squeeze() with calls to safe_cast_to_array() and submit a pull request. Alternatively, I could add the code for safe_squeeze() back in to utilities.py.

from m-loop.

charmasaur avatar charmasaur commented on August 15, 2024

Thanks for the sleuthing! Migrating safe_squeeze() calls to safe_cast_to_array() sounds good to me.

(incidentally, the revert commit was when I accidentally merged some development work to master... I guess I must have messed something up, and accidentally also reverted some stuff that was already in master)

from m-loop.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.