Python functions for audit Verify if the script actually does the right job and there is no manual error introduced

In [85]:
import csv
import json
import JobsMapResultsFilesToContainerObjs as ImageMap
import importlib
importlib.reload(ImageMap)


Out[85]:
<module 'JobsMapResultsFilesToContainerObjs' from '/Users/sreejithmenon/Google Drive/PythonCode/AnimalPhotoBias/script/JobsMapResultsFilesToContainerObjs.py'>

In [6]:
jsonObj = json.load(open("../data/experiment2_gid_aid_features.json"))

In [7]:
gidSpeciesList = []
for gid in jsonObj.keys():
    if jsonObj[gid] != None:
        gidSpecies = {}
        for dct in jsonObj[gid]:
            for aid in dct.keys():
                gidSpecies[gid] = gidSpecies.get(gid,[]) + [dct[aid][2][0]]
        gidSpeciesList.append(gidSpecies)

In [12]:
for dct in gidSpeciesList:
    for speciesLst in dct.values():
        firstEle = speciesLst[0]
        for ele in speciesLst:
            if ele != firstEle:
                print(dct.keys())


dict_keys(['1041'])
dict_keys(['6962'])
dict_keys(['6962'])
dict_keys(['978'])
dict_keys(['978'])
dict_keys(['978'])
dict_keys(['1332'])
dict_keys(['1332'])
dict_keys(['5470'])

In [69]:
def extractImageFeaturesFromMap(gidAidMapFl,aidFtrMapFl,feature):    
    aidFeatureDict = ImageMap.genAidFeatureDictDict(aidFtrMapFl)
    
    gidAidDict = ImageMap.genAidGidDictFromMap(gidAidMapFl)

    gidFeatureLst = []
    for gid in gidAidDict:
        if gidAidJson[gid]!= None:
            gidFtr = {}
            for aid in gidAidJson[gid]:
                gidFtr[gid] = gidFtr.get(gid,[]) + [aidFeatureDict[str(aid)][feature]]
        gidFeatureLst.append(gidFtr)
    
    return gidFeatureLst

In [95]:
aidFeatureDict = ImageMap.genAidFeatureDictDict("../data/experiment2_aid_features.json")
gidAidJson = ImageMap.genAidGidDictFromMap("../data/experiment2_gid_aid_map.json")

featuresPerImg = ImageMap.extractImageFeaturesFromMap("../data/experiment2_gid_aid_map.json","../data/experiment2_aid_features.json","SPECIES")

In [96]:
shareCountLogic = {}
for gid in featuresPerImg.keys():
    numInds = len(featuresPerImg[ele])
    isHomogeneous = True
    firstEle = featuresPerImg[ele][0]
    for species in featuresPerImg[ele]:
        if species != firstEle:
            isHomogeneous = False
    if isHomogeneous:
        countFor = firstEle
    else:
        countFor = None
    shareCountLogic[gid] = [numInds,isHomogeneous,countFor]

In [97]:
list(filter(lambda x: not x[2],l))


Out[97]:
[('1041', 2, False, None),
 ('1332', 6, False, None),
 ('6962', 4, False, None),
 ('978', 4, False, None),
 ('5470', 2, False, None)]

In [2]:
import PopulationEstimatorFromClf as PE



In [3]:
clfTypes = ['bayesian','logistic','svm','dtree','random_forests','ada_boost']
attribTypes = ['sparse','non_sparse','non_zero','abv_mean']

for clf in clfTypes[0]:
    for attrib in attribTypes[0]:
        print("Starting to run %s classifer on test data\nAttribute Selection Method : %s" %(clf,attrib))
        clfObj,predResults = PE.trainTestClf("../FinalResults/ImgShrRnkListWithTags.csv",
                             "../data/full_gid_aid_ftr_agg.csv",
                             clf,
                             attrib,
                             "../data/infoGainsExpt2.csv")


Starting to run b classifer on test data
Attribute Selection Method : s
---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
<ipython-input-3-9053770abdf0> in <module>()
      9                              clf,
     10                              attrib,
---> 11                              "../data/infoGainsExpt2.csv")

/Users/sreejithmenon/Google Drive/Project/AnimalPhotoBias/script/PopulationEstimatorFromClf.py in trainTestClf(train_data_fl, test_data_fl, clf, attribType, infoGainFl)
     22 def trainTestClf(train_data_fl,test_data_fl,clf,attribType,infoGainFl=None):
     23         # Create training data
---> 24         allAttribs = CH.genAllAttribs(train_data_fl,attribType,infoGainFl)
     25         train_data= CH.getMasterData(train_data_fl)
     26 

/Users/sreejithmenon/Google Drive/Project/AnimalPhotoBias/script/ClassiferHelperAPI.py in genAllAttribs(masterDataFl, constraint, infoGainFlNm)
    154         allAttribs = [row[0] for row in infoGains if float(row[1]) >= avg]
    155 
--> 156     return allAttribs

UnboundLocalError: local variable 'allAttribs' referenced before assignment

In [ ]:


In [ ]: