futurize_notes - npalmer-professional/HARK-1 GitHub Wiki

Futurize Notes

Below you will find the output of running these two commands for each file:

# First command: look at the suggested changes; if reasonable run second command
futurize filename.py >> ~/workspace/HARK-1/nate-notes/futurize_notes.md
# Second command: same as above but with "-w" (write to file) to actually implement changes 
futurize -w filename.py >> ~/workspace/HARK-1/nate-notes/futurize_notes.md

IMPORTANT NOTE: If I ever see "old_div" show up in the first set of results, I remove those results and go and add "from __future__ import division" to the top of the file, then re-run the two commands above. We do not want to save the old behavior of the division function ("integer division"), which is terrible for our purposes.

One last pass will also be conducted in the entire directory to confirm that there are no occurances of "old_div:"

egrep -inIR "old.div" *  

...at the base directory for HARK-1. To not create an infinite loop, I'll manually copy the results of that command to this file (if you tell it to output to this file, you get explosive results for even a single postive result, until your HD fills up. No good!)

Results of Futurize Commands:

--- HARKcore.py (original) +++ HARKcore.py (refactored) @@ -6,7 +6,11 @@ model adds an additional layer, endogenizing some of the inputs to the micro problem by finding a general equilibrium dynamic rule. '''

+from future import print_function + +from builtins import str +from builtins import range +from builtins import object from HARKutilities import getArgNames, NullFunc from copy import copy, deepcopy import numpy as np

--- HARKestimation.py (original) +++ HARKestimation.py (refactored) @@ -5,6 +5,8 @@

The following libraries are part of the standard python distribution

from future import division # Use new division function +from future import print_function +from builtins import str import numpy as np # Numerical Python from time import time # Used to time execution from copy import deepcopy # For replicating complex objects --- HARKestimation.py (original) +++ HARKestimation.py (refactored) @@ -5,6 +5,8 @@

The following libraries are part of the standard python distribution

from future import division # Use new division function +from future import print_function +from builtins import str import numpy as np # Numerical Python from time import time # Used to time execution from copy import deepcopy # For replicating complex objects --- HARKinterpolation.py (original) +++ HARKinterpolation.py (refactored) @@ -7,6 +7,7 @@ distance method from HARKobject. ''' from future import division, print_function +from builtins import range import warnings import numpy as np from scipy.interpolate import UnivariateSpline --- HARKinterpolation.py (original) +++ HARKinterpolation.py (refactored) @@ -7,6 +7,7 @@ distance method from HARKobject. ''' from future import division, print_function +from builtins import range import warnings import numpy as np from scipy.interpolate import UnivariateSpline --- HARKsimulation.py (original) +++ HARKsimulation.py (refactored) @@ -3,6 +3,8 @@ '''

from future import division +from future import print_function +from builtins import range import warnings # A library for runtime warnings import numpy as np # Numerical Python

--- HARKsimulation.py (original) +++ HARKsimulation.py (refactored) @@ -3,6 +3,8 @@ '''

from future import division +from future import print_function +from builtins import range import warnings # A library for runtime warnings import numpy as np # Numerical Python

--- HARKutilities.py (original) +++ HARKutilities.py (refactored) @@ -5,6 +5,10 @@ '''

from future import division # Import Python 3.x division function +from future import print_function +from builtins import str +from builtins import range +from builtins import object import functools import re # Regular expression, for string cleaning import warnings @@ -68,7 +72,7 @@ return argNames

-class NullFunc(): +class NullFunc(object): ''' A trivial class that acts as a placeholder "do nothing" function. ''' --- HARKutilities.py (original) +++ HARKutilities.py (refactored) @@ -5,6 +5,10 @@ '''

from future import division # Import Python 3.x division function +from future import print_function +from builtins import str +from builtins import range +from builtins import object import functools import re # Regular expression, for string cleaning import warnings @@ -68,7 +72,7 @@ return argNames

-class NullFunc(): +class NullFunc(object): ''' A trivial class that acts as a placeholder "do nothing" function. '''

--- HARKparallel.py (original) +++ HARKparallel.py (refactored) @@ -4,6 +4,9 @@ a command prompt. ''' from future import division, print_function +from builtins import zip +from builtins import str +from builtins import range import multiprocessing import numpy as np from time import clock @@ -234,7 +237,7 @@ print('Resuming search after ' + str(iters) + ' iterations and ' + str(evals) + ' function evaluations.')

 # Initialize some inputs for the multithreader
  • j_list = range(N-P,N)
  • j_list = list(range(N-P,N)) opt_params= [r_param,c_param,e_param]

    Run the Nelder-Mead algorithm until a terminal condition is met

@@ -367,15 +370,15 @@ ''' f = open(name + '.txt','rb') my_reader = csv.reader(f,delimiter=' ')

  • my_shape_txt = my_reader.next()
  • my_shape_txt = next(my_reader) shape0 = int(my_shape_txt[0]) shape1 = int(my_shape_txt[1])
  • my_nums_txt = my_reader.next()
  • my_nums_txt = next(my_reader) iters = int(my_nums_txt[0]) evals = int(my_nums_txt[1])
  • simplex_flat = np.array(my_reader.next(),dtype=float)
  • simplex_flat = np.array(next(my_reader),dtype=float) simplex = np.reshape(simplex_flat,(shape0,shape1))
  • fvals = np.array(my_reader.next(),dtype=float)
  • fvals = np.array(next(my_reader),dtype=float) f.close()

    return simplex, fvals, iters, evals --- HARKparallel.py (original) +++ HARKparallel.py (refactored) @@ -4,6 +4,9 @@ a command prompt. ''' from future import division, print_function +from builtins import zip +from builtins import str +from builtins import range import multiprocessing import numpy as np from time import clock @@ -234,7 +237,7 @@ print('Resuming search after ' + str(iters) + ' iterations and ' + str(evals) + ' function evaluations.')

    Initialize some inputs for the multithreader

  • j_list = range(N-P,N)
  • j_list = list(range(N-P,N)) opt_params= [r_param,c_param,e_param]

    Run the Nelder-Mead algorithm until a terminal condition is met

@@ -367,15 +370,15 @@ ''' f = open(name + '.txt','rb') my_reader = csv.reader(f,delimiter=' ')

  • my_shape_txt = my_reader.next()
  • my_shape_txt = next(my_reader) shape0 = int(my_shape_txt[0]) shape1 = int(my_shape_txt[1])
  • my_nums_txt = my_reader.next()
  • my_nums_txt = next(my_reader) iters = int(my_nums_txt[0]) evals = int(my_nums_txt[1])
  • simplex_flat = np.array(my_reader.next(),dtype=float)
  • simplex_flat = np.array(next(my_reader),dtype=float) simplex = np.reshape(simplex_flat,(shape0,shape1))
  • fvals = np.array(my_reader.next(),dtype=float)
  • fvals = np.array(next(my_reader),dtype=float) f.close()

    return simplex, fvals, iters, evals

--- ConsIndShockModel.py (original) +++ ConsIndShockModel.py (refactored) @@ -13,6 +13,9 @@ See HARK documentation for mathematical descriptions of the models being solved. ''' from future import division, print_function +from builtins import str +from builtins import range +from builtins import object import sys import os sys.path.insert(0, os.path.abspath('../')) --- ConsIndShockModel.py (original) +++ ConsIndShockModel.py (refactored) @@ -13,6 +13,9 @@ See HARK documentation for mathematical descriptions of the models being solved. ''' from future import division, print_function +from builtins import str +from builtins import range +from builtins import object import sys import os sys.path.insert(0, os.path.abspath('../'))

NOTE: for ConsumerParameters.py, no changes apparently needed: RefactoringTool: Skipping optional fixer: idioms RefactoringTool: Skipping optional fixer: ws_comma RefactoringTool: No changes to ConsumerParameters.py RefactoringTool: Files that need to be modified: RefactoringTool: ConsumerParameters.py

--- ConsAggShockModel.py (original) +++ ConsAggShockModel.py (refactored) @@ -5,6 +5,8 @@ used for solving "macroeconomic" models with aggregate shocks. ''' from future import division, print_function +from builtins import str +from builtins import range import sys sys.path.insert(0,'../')

--- ConsAggShockModel.py (original) +++ ConsAggShockModel.py (refactored) @@ -5,6 +5,8 @@ used for solving "macroeconomic" models with aggregate shocks. ''' from future import division, print_function +from builtins import str +from builtins import range import sys sys.path.insert(0,'../')

--- ConsMarkovModel.py (original) +++ ConsMarkovModel.py (refactored) @@ -5,6 +5,7 @@ distribution can vary with the discrete state. ''' from future import division, print_function +from builtins import range import sys sys.path.insert(0,'../')

--- ConsMarkovModel.py (original) +++ ConsMarkovModel.py (refactored) @@ -5,6 +5,7 @@ distribution can vary with the discrete state. ''' from future import division, print_function +from builtins import range import sys sys.path.insert(0,'../')

--- TractableBufferStockModel.py (original) +++ TractableBufferStockModel.py (refactored) @@ -21,6 +21,7 @@

Import the HARK library. The assumption is that this code is in a folder

contained in the HARK folder.

from future import division, print_function +from builtins import str import sys import os import numpy as np --- TractableBufferStockModel.py (original) +++ TractableBufferStockModel.py (refactored) @@ -21,6 +21,7 @@

Import the HARK library. The assumption is that this code is in a folder

contained in the HARK folder.

from future import division, print_function +from builtins import str import sys import os import numpy as np --- ConsRepAgentModel.py (original) +++ ConsRepAgentModel.py (refactored) @@ -5,6 +5,8 @@ time invariant or exist on a short cycle; models must be infinite horizon. ''' from future import division, print_function +from builtins import str +from builtins import range import sys import os sys.path.insert(0, os.path.abspath('../')) --- ConsRepAgentModel.py (original) +++ ConsRepAgentModel.py (refactored) @@ -5,6 +5,8 @@ time invariant or exist on a short cycle; models must be infinite horizon. ''' from future import division, print_function +from builtins import str +from builtins import range import sys import os sys.path.insert(0, os.path.abspath('../')) --- ConsGenIncProcessModel.py (original) +++ ConsGenIncProcessModel.py (refactored) @@ -5,6 +5,8 @@ and allows (log) persistent income to follow an AR1 process rather than random walk. ''' from future import division, print_function +from builtins import str +from builtins import range import sys import os sys.path.insert(0, os.path.abspath('../')) --- ConsGenIncProcessModel.py (original) +++ ConsGenIncProcessModel.py (refactored) @@ -5,6 +5,8 @@ and allows (log) persistent income to follow an AR1 process rather than random walk. ''' from future import division, print_function +from builtins import str +from builtins import range import sys import os sys.path.insert(0, os.path.abspath('../')) --- ConsMedModel.py (original) +++ ConsMedModel.py (refactored) @@ -2,6 +2,8 @@ Consumption-saving models that also include medical spending. ''' from future import division, print_function +from builtins import str +from builtins import range import sys sys.path.insert(0,'../')

--- ConsMedModel.py (original) +++ ConsMedModel.py (refactored) @@ -2,6 +2,8 @@ Consumption-saving models that also include medical spending. ''' from future import division, print_function +from builtins import str +from builtins import range import sys sys.path.insert(0,'../')

--- RepAgentModel.py (original) +++ RepAgentModel.py (refactored) @@ -5,6 +5,8 @@ time invariant or exist on a short cycle. ''' from future import division, print_function +from builtins import str +from builtins import range import sys import os sys.path.insert(0, os.path.abspath('../')) --- ConsPrefShockModel.py (original) +++ ConsPrefShockModel.py (refactored) @@ -7,6 +7,8 @@ by inheriting from multiple classes. ''' from future import division, print_function +from builtins import str +from builtins import range import sys sys.path.insert(0,'../')

--- ConsPrefShockModel.py (original) +++ ConsPrefShockModel.py (refactored) @@ -7,6 +7,8 @@ by inheriting from multiple classes. ''' from future import division, print_function +from builtins import str +from builtins import range import sys sys.path.insert(0,'../')

--- Try-Alternative-Parameter-Values.py (original) +++ Try-Alternative-Parameter-Values.py (refactored) @@ -4,6 +4,8 @@

@author: [email protected] """ +from builtins import str +from builtins import range import sys
import os
import pylab # the plotting tools --- Try-Alternative-Parameter-Values.py (original) +++ Try-Alternative-Parameter-Values.py (refactored) @@ -4,6 +4,8 @@

@author: [email protected] """ +from builtins import str +from builtins import range import sys
import os
import pylab # the plotting tools

--- Fagereng_demo.py (original) +++ Fagereng_demo.py (refactored) @@ -33,6 +33,8 @@ because this target tends to push the estimate around a bit. ''' from future import division, print_function +from builtins import str +from builtins import range import sys import os sys.path.insert(0, os.path.abspath('../')) --- Fagereng_demo.py (original) +++ Fagereng_demo.py (refactored) @@ -33,6 +33,8 @@ because this target tends to push the estimate around a bit. ''' from future import division, print_function +from builtins import str +from builtins import range import sys import os sys.path.insert(0, os.path.abspath('../'))

--- Chinese_Growth.py (original) +++ Chinese_Growth.py (refactored) NOTE: For some reason, "futurize" really wants to use "old_div." We don't want that, so I'm going to manually put in place the other 2 recommended imports:

+from builtins import str +from builtins import range

--- MPC_credit_vs_MPC_income.py (original) +++ MPC_credit_vs_MPC_income.py (refactored)

...strangely enough, futurize also attempts to use "old_div" here as well. Hmmmm.

--- MPC_credit_vs_MPC_income.py (original) +++ MPC_credit_vs_MPC_income.py (refactored)

...strangely enough, futurize also attempts to use "old_div" here as well. Hmmmm.

--- NonDurables_During_Great_Recession.py (original) +++ NonDurables_During_Great_Recession.py (refactored) @@ -26,6 +26,8 @@

The first step is to be able to bring things in from different directories

from future import division, print_function +from builtins import str +from builtins import range import sys import os sys.path.insert(0, os.path.abspath('../')) #Path to ConsumptionSaving folder --- NonDurables_During_Great_Recession.py (original) +++ NonDurables_During_Great_Recession.py (refactored) @@ -26,6 +26,8 @@

The first step is to be able to bring things in from different directories

from future import division, print_function +from builtins import str +from builtins import range import sys import os sys.path.insert(0, os.path.abspath('../')) #Path to ConsumptionSaving folder --- FashionVictimParams.py (original) +++ FashionVictimParams.py (refactored) @@ -1,6 +1,7 @@ ''' Defines some default parameters for the fashion victim model. ''' +from future import print_function

DiscFac = 0.95 # Intertemporal discount factor uParamA = 1.0 # Parameter A in the utility function (pdf of the beta distribution) --- FashionVictimParams.py (original) +++ FashionVictimParams.py (refactored) @@ -1,6 +1,7 @@ ''' Defines some default parameters for the fashion victim model. ''' +from future import print_function

DiscFac = 0.95 # Intertemporal discount factor uParamA = 1.0 # Parameter A in the utility function (pdf of the beta distribution)

--- FashionVictimModel.py (original) +++ FashionVictimModel.py (refactored) @@ -94,7 +94,7 @@ self.distance_criteria = ['pNextSlope','pNextWidth','pNextIntercept']

-class FashionMarketInfo(): +class FashionMarketInfo(object): ''' A class for representing the current distribution of styles in the population. ''' --- FashionVictimModel.py (original) +++ FashionVictimModel.py (refactored) @@ -94,7 +94,7 @@ self.distance_criteria = ['pNextSlope','pNextWidth','pNextIntercept']

-class FashionMarketInfo(): +class FashionMarketInfo(object): ''' A class for representing the current distribution of styles in the population. '''

futurize TractableBufferStockModel_UnitTests.py >> ~/workspace/HARK-1/nate-notes/futurize_notes.md RefactoringTool: Skipping optional fixer: idioms RefactoringTool: Skipping optional fixer: ws_comma RefactoringTool: No changes to TractableBufferStockModel_UnitTests.py RefactoringTool: Files that need to be modified: RefactoringTool: TractableBufferStockModel_UnitTests.py

--- MultithreadDemo.py (original) +++ MultithreadDemo.py (refactored) @@ -7,6 +7,9 @@ sufficiently large amount of work for each thread to do, the maximum speedup factor seems to be around P/2, where P is the number of processors. ''' +from future import print_function +from builtins import str +from builtins import range import sys import os sys.path.insert(0, os.path.abspath('../')) --- MultithreadDemo.py (original) +++ MultithreadDemo.py (refactored) @@ -7,6 +7,9 @@ sufficiently large amount of work for each thread to do, the maximum speedup factor seems to be around P/2, where P is the number of processors. ''' +from future import print_function +from builtins import str +from builtins import range import sys import os sys.path.insert(0, os.path.abspath('../')) --- ModelTesting.py (original) +++ ModelTesting.py (refactored) @@ -3,8 +3,13 @@ for many different parameter values. It then returns as output the errors generated by solving the model for those parameter values (if any.) """ +from future import print_function

First, tell Python what directories we will be using

+from builtins import str +from builtins import zip +from builtins import range +from builtins import object import sys import os sys.path.insert(0, os.path.abspath('../')) @@ -75,7 +80,7 @@ dict_of_min_max_and_N = {key:(value-self._multipliervalue, # the min value+self._multipliervalue, # the max self.N_param_values_in_range) # number of param values to try

  •                                  for key,value in self._base_primitives.iteritems()}
    
  •                                  for key,value in self._base_primitives.items()}
    
       N_combinations = self.N_param_values_in_range**len(self._base_primitives)
    

@@ -104,12 +109,12 @@ parameterLists = [] keyOrder = [] parametersToTest = []

  •    for key,value in self.dict_of_min_max_and_N.iteritems():
    
  •    for key,value in self.dict_of_min_max_and_N.items():
           parameterRange = np.linspace(*value)
           parameterLists.append(parameterRange)
           keyOrder.append(key)
       for param_combination in itertools.product(*parameterLists):
    
  •        parametersToTest.append(dict(zip(keyOrder,param_combination)))
    
  •        parametersToTest.append(dict(list(zip(keyOrder,param_combination))))
       return parametersToTest
    

    def testParameters(self): @@ -176,7 +181,7 @@ try: Test.solve() #TODO: Insert allowed exceptions here so they don't count as errors!

  •        except Exception,e:
    
  •        except Exception as e:
               testData.errorBoolean    = True
               testData.errorCode       = str(e)
               testData._tracebackText  = sys.exc_info()
    

--- ModelTesting.py (original) +++ ModelTesting.py (refactored) @@ -3,8 +3,13 @@ for many different parameter values. It then returns as output the errors generated by solving the model for those parameter values (if any.) """ +from future import print_function

First, tell Python what directories we will be using

+from builtins import str +from builtins import zip +from builtins import range +from builtins import object import sys import os sys.path.insert(0, os.path.abspath('../')) @@ -75,7 +80,7 @@ dict_of_min_max_and_N = {key:(value-self._multipliervalue, # the min value+self._multipliervalue, # the max self.N_param_values_in_range) # number of param values to try

  •                                  for key,value in self._base_primitives.iteritems()}
    
  •                                  for key,value in self._base_primitives.items()}
    
       N_combinations = self.N_param_values_in_range**len(self._base_primitives)
    

@@ -104,12 +109,12 @@ parameterLists = [] keyOrder = [] parametersToTest = []

  •    for key,value in self.dict_of_min_max_and_N.iteritems():
    
  •    for key,value in self.dict_of_min_max_and_N.items():
           parameterRange = np.linspace(*value)
           parameterLists.append(parameterRange)
           keyOrder.append(key)
       for param_combination in itertools.product(*parameterLists):
    
  •        parametersToTest.append(dict(zip(keyOrder,param_combination)))
    
  •        parametersToTest.append(dict(list(zip(keyOrder,param_combination))))
       return parametersToTest
    

    def testParameters(self): @@ -176,7 +181,7 @@ try: Test.solve() #TODO: Insert allowed exceptions here so they don't count as errors!

  •        except Exception,e:
    
  •        except Exception as e:
               testData.errorBoolean    = True
               testData.errorCode       = str(e)
               testData._tracebackText  = sys.exc_info()
    

$ futurize HARKutilities_UnitTests.py >> ~/workspace/HARK-1/nate-notes/futurize_notes.md RefactoringTool: Skipping optional fixer: idioms RefactoringTool: Skipping optional fixer: ws_comma RefactoringTool: No changes to HARKutilities_UnitTests.py RefactoringTool: Files that need to be modified: RefactoringTool: HARKutilities_UnitTests.py

$ futurize Comparison_UnitTests.py >> ~/workspace/HARK-1/nate-notes/futurize_notes.md RefactoringTool: Skipping optional fixer: idioms RefactoringTool: Skipping optional fixer: ws_comma RefactoringTool: No changes to Comparison_UnitTests.py RefactoringTool: Files that need to be modified: RefactoringTool: Comparison_UnitTests.py

--- SetupSCFdata.py (original) +++ SetupSCFdata.py (refactored) @@ -2,6 +2,7 @@ Sets up the SCF data for use in the SolvingMicroDSOPs estimation. ''' from future import division # Use new division function +from future import print_function from builtins import zip from builtins import str from builtins import range @@ -28,7 +29,7 @@

Open the file handle and create a reader object and a csv header

infile = open(scf_data_path + '/SCFdata.csv', 'r')
csv_reader = csv.reader(infile) -data_csv_header = csv_reader.next() +data_csv_header = next(csv_reader)

Pull the column index from the data_csv_header

data_column_index = data_csv_header.index('wealth_income_ratio') # scf_w_col

--- SetupSCFdata.py (original) +++ SetupSCFdata.py (refactored) @@ -2,6 +2,7 @@ Sets up the SCF data for use in the SolvingMicroDSOPs estimation. ''' from future import division # Use new division function +from future import print_function from builtins import zip from builtins import str from builtins import range @@ -28,7 +29,7 @@

Open the file handle and create a reader object and a csv header

infile = open(scf_data_path + '/SCFdata.csv', 'r')
csv_reader = csv.reader(infile) -data_csv_header = csv_reader.next() +data_csv_header = next(csv_reader)

Pull the column index from the data_csv_header

data_column_index = data_csv_header.index('wealth_income_ratio') # scf_w_col --- EstimationParameters.py (original) +++ EstimationParameters.py (refactored) @@ -2,6 +2,7 @@ Specifies the full set of calibrated values required to estimate the SolvingMicroDSOPs model. The empirical data is stored in a separate csv file and is loaded in SetupSCFdata. ''' +from future import print_function

---------------------------------------------------------------------------------

Debugging flags

--- EstimationParameters.py (original) +++ EstimationParameters.py (refactored) @@ -2,6 +2,7 @@ Specifies the full set of calibrated values required to estimate the SolvingMicroDSOPs model. The empirical data is stored in a separate csv file and is loaded in SetupSCFdata. ''' +from future import print_function

---------------------------------------------------------------------------------

Debugging flags

--- StructEstimation.py (original) +++ StructEstimation.py (refactored) @@ -8,8 +8,11 @@ consumption-saving model with idiosyncratic shocks to permanent and transitory income as defined in ConsIndShockModel. ''' +from future import print_function

Import the HARK library. The assumption is that this code is in a folder

contained in the HARK folder.

+from builtins import str +from builtins import range import sys import os sys.path.insert(0, os.path.abspath('../')) --- StructEstimation.py (original) +++ StructEstimation.py (refactored) @@ -8,8 +8,11 @@ consumption-saving model with idiosyncratic shocks to permanent and transitory income as defined in ConsIndShockModel. ''' +from future import print_function

Import the HARK library. The assumption is that this code is in a folder

contained in the HARK folder.

+from builtins import str +from builtins import range import sys import os sys.path.insert(0, os.path.abspath('../'))

⚠️ **GitHub.com Fallback** ⚠️