ectools/fc.py
def fitness_complexity_calculation(m, iterations, samples):
    """Returns fitness and complexity at different iteration stages for a given matrix m. m is intended to be of shape [n_countries,n_products], i.e. Fitness will be computed on the rows and Complexity on the columns of m.
    Note: this function handles all data cleaning procedures. The actual fixed point calculation is done by the function iterative_map(). iterative_map() was written for optimization with Numba, therefore its style is somewhat less than pythonic.

    Arguments:
        m: The binary matrix on which to compute the Fitness-Complexity algorithm. Must be np.ndarray of order 2
        iterations: How many iterations to do. Must be int.
        samples: How many samples to take while doing the iterations. Must be int. The sample will be equally spaced in time from 0 to `iterations`.

    Returns:
        fitness_value: A np.ndarray of size [`samples`,n_countries] containing the sampled values of Fitness for each country at different stages of iteration. For clarity, n_countries is the number of rows of `m`, the input matrix.
        fitness_normalization: A np.ndarray of size ['samples'] containing the normalization values of Fitness at different stages of iteration.
        complexity_value: A np.ndarray of size [`samples`,n_products] containing the sampled values of Complexity for each product at different stages of iteration. For clarity, n_products is the number of columns of `m`, the input matrix.
        complexity_normalization: A np.ndarray of size ['samples'] containing the normalization values of Complexity at different stages of iteration.
        sample_times: A np.ndarray of size ['samples'] containing the number of iterations for each of the samples output bu this function.

    """
    assert np.isnan(m).sum() == 0
    # if there are some products exported by nobody (!)
    noones_products = m.sum(axis=0) == 0
    noones_products_present = noones_products.sum()
    if noones_products_present:
        # remove columns corresponding to these products
        m = m[:, np.invert(noones_products)]
    # if there are some countries that export nothing (!)
    nontrade_countries = m.sum(axis=1) == 0
    nontrade_countries_present = nontrade_countries.sum()
    if nontrade_countries_present:
        # remove rows corresponding to such countries
        m = m[np.invert(nontrade_countries), :]
    # how big is the matrix
    num_countries, num_products = m.shape
    # where to take samples:
    if samples != 0:
        step = int(iterations / samples)
        sample_times = np.zeros(samples)
    else:
        step = iterations
        sample_times = np.array([0])
    # initialize fitness and complexity
    fitness = np.array(np.ones(num_countries), dtype='float128')
    complexity = np.array(np.ones(num_products), dtype='float128')
    # initialize sampling lists so that the algorithm doesn't have to do dynamic resizing of arrays
    fitness_value, fitness_normalization = \
        np.zeros([samples + 1, num_countries]), np.zeros(samples + 1)
    complexity_value, complexity_normalization = \
        np.zeros([samples + 1, num_products]), np.zeros(samples + 1)
    fitness_value, fitness_normalization, \
    complexity_value, complexity_normalization, sample_times = \
        iterative_map(m, iterations, step,
                      fitness, complexity,
                      fitness_value, fitness_normalization,
                      complexity_value, complexity_normalization,
                      sample_times)

    # values for countries that export nothing reinserted as nans:
    # (must come before reinsertion of problematic complexity values!)
    if nontrade_countries_present:
        # make empty arrays with adequate size
        fitness_value_complete = np.zeros([
            fitness_value.shape[0],
            len(nontrade_countries)])
        nan_vector = np.zeros(fitness_value_complete.shape[0])
        nan_vector[:] = np.nan
        # iteratively reinsert nontrading countries preserving original ordering
        count = 0
        for k, nontrading in enumerate(nontrade_countries):
            # if it's a trading country, put in its fitness
            if not nontrading:
                fitness_value_complete[:, k] = fitness_value[:, count]
                count += 1
            # otherwise put in a nan column
            else:
                fitness_value_complete[:, k] = nan_vector
        # substitute complete values in vector to be returned
        fitness_value = fitness_value_complete
    # values for products exported by nobody reinserted as nans:
    if noones_products_present:
        # make empty arrays with adequate size
        complexity_value_complete = np.zeros([
            complexity_value.shape[0],
            len(noones_products)])
        nan_vector = np.zeros([complexity_value_complete.shape[0]])
        nan_vector[:] = np.nan
        # iteratively reinsert noones products preserving original ordering
        count = 0
        for k, noones in enumerate(noones_products):
            # if it's not a problematic product
            if not noones:
                complexity_value_complete[:, k] = complexity_value[:, count]
                count += 1
            else:
                complexity_value_complete[:, k] = nan_vector
        # substitute complete values in vector to be returned
        complexity_value = complexity_value_complete
    return fitness_value, fitness_normalization, \
           complexity_value, complexity_normalization, sample_times
Returns fitness and complexity at different iteration stages for a given matrix m. m is intended to be of shape [n_countries,n_products], i.e. Fitness will be computed on the rows and Complexity on the columns of m.
    Note: this function handles all data cleaning procedures. The actual fixed point calculation is done by the function iterative_map(). iterative_map() was written for optimization with Numba, therefore its style is somewhat less than pythonic.

    Arguments:
        m: The binary matrix on which to compute the Fitness-Complexity algorithm. Must be np.ndarray of order 2
        iterations: How many iterations to do. Must be int.
        samples: How many samples to take while doing the iterations. Must be int. The sample will be equally spaced in time from 0 to `iterations`.

    Returns:
        fitness_value: A np.ndarray of size [`samples`,n_countries] containing the sampled values of Fitness for each country at different stages of iteration. For clarity, n_countries is the number of rows of `m`, the input matrix.
        fitness_normalization: A np.ndarray of size ['samples'] containing the normalization values of Fitness at different stages of iteration.
        complexity_value: A np.ndarray of size [`samples`,n_products] containing the sampled values of Complexity for each product at different stages of iteration. For clarity, n_products is the number of columns of `m`, the input matrix.
        complexity_normalization: A np.ndarray of size ['samples'] containing the normalization values of Complexity at different stages of iteration.
        sample_times: A np.ndarray of size ['samples'] containing the number of iterations for each of the samples output bu this function.

    
Returns fitness and complexity at different iteration stages for a given matrix m. m is intended to be of shape [n_countries,n_products], i.e. Fitness will be computed on the rows and Complexity on the columns of m.
Note: this function handles all data cleaning procedures. The actual fixed point calculation is done by the function iterative_map(). iterative_map() was written for optimization with Numba, therefore its style is somewhat less than pythonic.
Returns fitness and complexity at different iteration stages for a given matrix m . m is intended to be of shape [ n_countries n_products ] i . e . Fitness will be computed on the rows and Complexity on the columns of m . Note : this function handles all data cleaning procedures . The actual fixed point calculation is done by the function iterative_map () . iterative_map () was written for optimization with Numba therefore its style is somewhat less than pythonic .
def fitness_complexity_calculation(m, iterations, samples):
    assert np.isnan(m).sum() == 0
    noones_products = m.sum(axis=0) == 0
    noones_products_present = noones_products.sum()
    if noones_products_present:
        m = m[:, np.invert(noones_products)]
    nontrade_countries = m.sum(axis=1) == 0
    nontrade_countries_present = nontrade_countries.sum()
    if nontrade_countries_present:
        m = m[np.invert(nontrade_countries), :]
    num_countries, num_products = m.shape
    if samples != 0:
        step = int(iterations / samples)
        sample_times = np.zeros(samples)
    else:
        step = iterations
        sample_times = np.array([0])
    fitness = np.array(np.ones(num_countries), dtype='float128')
    complexity = np.array(np.ones(num_products), dtype='float128')
    fitness_value, fitness_normalization = \
        np.zeros([samples + 1, num_countries]), np.zeros(samples + 1)
    complexity_value, complexity_normalization = \
        np.zeros([samples + 1, num_products]), np.zeros(samples + 1)
    fitness_value, fitness_normalization, \
    complexity_value, complexity_normalization, sample_times = \
        iterative_map(m, iterations, step,
                      fitness, complexity,
                      fitness_value, fitness_normalization,
                      complexity_value, complexity_normalization,
                      sample_times)
    if nontrade_countries_present:
        fitness_value_complete = np.zeros([
            fitness_value.shape[0],
            len(nontrade_countries)])
        nan_vector = np.zeros(fitness_value_complete.shape[0])
        nan_vector[:] = np.nan
        count = 0
        for k, nontrading in enumerate(nontrade_countries):
            if not nontrading:
                fitness_value_complete[:, k] = fitness_value[:, count]
                count += 1
            else:
                fitness_value_complete[:, k] = nan_vector
        fitness_value = fitness_value_complete
    if noones_products_present:
        complexity_value_complete = np.zeros([
            complexity_value.shape[0],
            len(noones_products)])
        nan_vector = np.zeros([complexity_value_complete.shape[0]])
        nan_vector[:] = np.nan
        count = 0
        for k, noones in enumerate(noones_products):
            if not noones:
                complexity_value_complete[:, k] = complexity_value[:, count]
                count += 1
            else:
                complexity_value_complete[:, k] = nan_vector
        complexity_value = complexity_value_complete
    return fitness_value, fitness_normalization, \
           complexity_value, complexity_normalization, sample_times
def fitness_complexity_calculation ( m , iterations , samples ) : assert np . isnan ( m ) . sum ( ) == 0 noones_products = m . sum ( axis = 0 ) == 0 noones_products_present = noones_products . sum ( ) if noones_products_present : m = m [ : , np . invert ( noones_products ) ] nontrade_countries = m . sum ( axis = 1 ) == 0 nontrade_countries_present = nontrade_countries . sum ( ) if nontrade_countries_present : m = m [ np . invert ( nontrade_countries ) , : ] num_countries , num_products = m . shape if samples != 0 : step = int ( iterations / samples ) sample_times = np . zeros ( samples ) else : step = iterations sample_times = np . array ( [ 0 ] ) fitness = np . array ( np . ones ( num_countries ) , dtype = 'float128' ) complexity = np . array ( np . ones ( num_products ) , dtype = 'float128' ) fitness_value , fitness_normalization = np . zeros ( [ samples + 1 , num_countries ] ) , np . zeros ( samples + 1 ) complexity_value , complexity_normalization = np . zeros ( [ samples + 1 , num_products ] ) , np . zeros ( samples + 1 ) fitness_value , fitness_normalization , complexity_value , complexity_normalization , sample_times = iterative_map ( m , iterations , step , fitness , complexity , fitness_value , fitness_normalization , complexity_value , complexity_normalization , sample_times ) if nontrade_countries_present : fitness_value_complete = np . zeros ( [ fitness_value . shape [ 0 ] , len ( nontrade_countries ) ] ) nan_vector = np . zeros ( fitness_value_complete . shape [ 0 ] ) nan_vector [ : ] = np . nan count = 0 for k , nontrading in enumerate ( nontrade_countries ) : if not nontrading : fitness_value_complete [ : , k ] = fitness_value [ : , count ] count += 1 else : fitness_value_complete [ : , k ] = nan_vector fitness_value = fitness_value_complete if noones_products_present : complexity_value_complete = np . zeros ( [ complexity_value . shape [ 0 ] , len ( noones_products ) ] ) nan_vector = np . zeros ( [ complexity_value_complete . shape [ 0 ] ] ) nan_vector [ : ] = np . nan count = 0 for k , noones in enumerate ( noones_products ) : if not noones : complexity_value_complete [ : , k ] = complexity_value [ : , count ] count += 1 else : complexity_value_complete [ : , k ] = nan_vector complexity_value = complexity_value_complete return fitness_value , fitness_normalization , complexity_value , complexity_normalization , sample_times
Returns fitness and complexity at different iteration stages for a given matrix m. m is intended to be of shape [n_countries,n_products], i.e.
Returns fitness and complexity at different iteration stages for a given matrix m . m is intended to be of shape [ n_countries n_products ] i . e .
"""Returns fitness and complexity at different iteration stages for a given matrix m. m is intended to be of shape [n_countries,n_products], i.e. Fitness will be computed on the rows and Complexity on the columns of m.
    Note: this function handles all data cleaning procedures. The actual fixed point calculation is done by the function iterative_map(). iterative_map() was written for optimization with Numba, therefore its style is somewhat less than pythonic.

    Arguments:
        m: The binary matrix on which to compute the Fitness-Complexity algorithm. Must be np.ndarray of order 2
        iterations: How many iterations to do. Must be int.
        samples: How many samples to take while doing the iterations. Must be int. The sample will be equally spaced in time from 0 to `iterations`.

    Returns:
        fitness_value: A np.ndarray of size [`samples`,n_countries] containing the sampled values of Fitness for each country at different stages of iteration. For clarity, n_countries is the number of rows of `m`, the input matrix.
        fitness_normalization: A np.ndarray of size ['samples'] containing the normalization values of Fitness at different stages of iteration.
        complexity_value: A np.ndarray of size [`samples`,n_products] containing the sampled values of Complexity for each product at different stages of iteration. For clarity, n_products is the number of columns of `m`, the input matrix.
        complexity_normalization: A np.ndarray of size ['samples'] containing the normalization values of Complexity at different stages of iteration.
        sample_times: A np.ndarray of size ['samples'] containing the number of iterations for each of the samples output bu this function.

    """ # if there are some products exported by nobody (!) # remove columns corresponding to these products # if there are some countries that export nothing (!) # remove rows corresponding to such countries # how big is the matrix # where to take samples: # initialize fitness and complexity # initialize sampling lists so that the algorithm doesn't have to do dynamic resizing of arrays # values for countries that export nothing reinserted as nans: # (must come before reinsertion of problematic complexity values!) # make empty arrays with adequate size # iteratively reinsert nontrading countries preserving original ordering # if it's a trading country, put in its fitness # otherwise put in a nan column # substitute complete values in vector to be returned # values for products exported by nobody reinserted as nans: # make empty arrays with adequate size # iteratively reinsert noones products preserving original ordering # if it's not a problematic product # substitute complete values in vector to be returned
A np.ndarray of size [`samples`,n_countries] containing the sampled values of Fitness for each country at different stages of iteration. For clarity, n_countries is the number of rows of `m`, the input matrix.
fitness_normalization: A np.ndarray of size ['samples'] containing the normalization values of Fitness at different stages of iteration.
complexity_value: A np.ndarray of size [`samples`,n_products] containing the sampled values of Complexity for each product at different stages of iteration. For clarity, n_products is the number of columns of `m`, the input matrix.
complexity_normalization: A np.ndarray of size ['samples'] containing the normalization values of Complexity at different stages of iteration.
sample_times: A np.ndarray of size ['samples'] containing the number of iterations for each of the samples output bu this function.
A np . ndarray of size [ ` samples ` n_countries ] containing the sampled values of Fitness for each country at different stages of iteration . For clarity n_countries is the number of rows of ` m ` the input matrix . fitness_normalization : A np . ndarray of size [ ' samples ' ] containing the normalization values of Fitness at different stages of iteration . complexity_value : A np . ndarray of size [ ` samples ` n_products ] containing the sampled values of Complexity for each product at different stages of iteration . For clarity n_products is the number of columns of ` m ` the input matrix . complexity_normalization : A np . ndarray of size [ ' samples ' ] containing the normalization values of Complexity at different stages of iteration . sample_times : A np . ndarray of size [ ' samples ' ] containing the number of iterations for each of the samples output bu this function .
fitness_value
m
The binary matrix on which to compute the Fitness-Complexity algorithm. Must be np.ndarray of order 2
The binary matrix on which to compute the Fitness - Complexity algorithm . Must be np . ndarray of order 2
iterations
None
samples
None