Example #1
0
def runModel(depAirportCode, departureDT, arrAirportCode, arrivalDT):
    import training as f
    depTuple = getWeather(depAirportCode, departureDT)
    arrTuple = getWeather(arrAirportCode, arrivalDT)
    depWeather = depTuple[0]
    arrWeather = arrTuple[0]

    #create the features vector
    features = []
    for attr in f.attributes:
        features.append(depWeather[mapAttribute(attr)])
    for attr in f.attributes:
        features.append(arrWeather[mapAttribute(attr)])

    #Call training handler for custom features
    s = type(
        'dummy', (object, ), {
            'departureTime': departureDT,
            'arrivalTime': arrivalDT,
            'arrivalAirportFsCode': arrAirportCode,
            'departureAirportFsCode': depAirportCode,
            'departureWeather': depWeather,
            'arrivalWeather': arrWeather
        })
    customFeaturesForRunModel = f.getTrainingHandler().customTrainingFeatures(
        s)
    for value in customFeaturesForRunModel:
        features.append(value)

    html = '<table width=100%><tr><th>' + depTuple[
        1] + '</th><th>Prediction</th><th>' + arrTuple[1] + '</th></tr>'
    html += '<tr><td>' + formatWeather(depWeather) + '</td>'
    html += '<td><ul>'
    for model in mlModels:
        label = model.__class__.__name__
        html += '<li>' + label + ': ' + f.getTrainingHandler().getClassLabel(
            model.predict(features)) + '</li>'
    html += '</ul></td>'
    html += '<td>' + formatWeather(arrWeather) + '</td>'
    html += '</tr></table>'
    html += '<div id="map" style="height:300px"></div>'

    #display map
    html += '<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBBfYX6GG1foO1l7TAPk2LQVV_nACb7T4Q&callback=renderMap" type="text/javascript"></script>'
    html += '<script type="text/javascript">'
    html += 'function renderMap() {'
    html += 'var map = new google.maps.Map(document.getElementById("map"), {zoom: 4,center: new google.maps.LatLng(40, -100),mapTypeId: google.maps.MapTypeId.TERRAIN});'
    html += 'var depAirport = new google.maps.LatLng(' + depTuple[
        2] + ',' + depTuple[3] + ');'
    html += 'var arrAirport = new google.maps.LatLng(' + arrTuple[
        2] + ',' + arrTuple[3] + ');'
    html += 'var markerP1 = new google.maps.Marker({position: depAirport, map: map});'
    html += 'var markerP2 = new google.maps.Marker({position: arrAirport, map: map});'
    html += 'var flightPlanCoordinates = [depAirport,arrAirport];'
    html += 'var flightPath = new google.maps.Polyline({path: flightPlanCoordinates,strokeColor: "#0000FF",strokeOpacity: 1.0,strokeWeight: 2});'
    html += 'flightPath.setMap(map);}'
    html += '</script>'

    display(HTML(html))
    def doRender(self, handlerId):
        f1 = "departureWeather.temp"
        f2 = "arrivalWeather.temp"
        f1 = f1.split(".")
        f2 = f2.split(".")
        handler = training.getTrainingHandler()
        darr=self.entity.map(lambda s: ( handler.computeClassification(s),(\
            reduce(lambda x,y: getattr(x,y) if isinstance(x, Row) else getattr(getattr(s,x),y), f1) if len(f1)>1 else getattr(s,f1[0]),\
            reduce(lambda x,y: getattr(x,y) if isinstance(x, Row) else getattr(getattr(s,x),y), f2) if len(f2)>1 else getattr(s,f2[0])\
            )))\
            .reduceByKey(lambda x,y: makeList(x) + makeList(y))\
            .collect()
        numClasses = handler.numClasses()
        citer = iter(cm.rainbow(np.linspace(0, 1, numClasses)))
        colors = [next(citer) for i in range(0, numClasses)]
        legends = [handler.getClassLabel(i) for i in range(0, numClasses)]
        sets = []
        fig, ax = plt.subplots(figsize=(12, 8))
        for t in darr:
            sets.append((ax.scatter([x[0] for x in t[1]], [x[1] for x in t[1]],
                                    color=colors[t[0]],
                                    alpha=0.5), legends[t[0]]))

        ax.set_ylabel("Departure Airport Temp")
        ax.set_xlabel("Arrival Airport Temp")
        ax.legend([x[0] for x in sets], [x[1] for x in sets],
                  scatterpoints=1,
                  loc='lower left',
                  ncol=numClasses,
                  fontsize=12)

        #Render the figure
        (dialogTemplate, dialogOptions) = self.getDialogInfo(handlerId)
        dialogBody = self.renderTemplate(dialogTemplate, **dialogOptions)
        self.renderFigure(fig, dialogBody)
    def doRender(self, handlerId):
        f1="departureWeather.temp"
        f2="arrivalWeather.temp"
        f1=f1.split(".")
        f2=f2.split(".")
        handler=training.getTrainingHandler()
        darr=self.entity.map(lambda s: ( handler.computeClassification(s),(\
            reduce(lambda x,y: getattr(x,y) if isinstance(x, Row) else getattr(getattr(s,x),y), f1) if len(f1)>1 else getattr(s,f1[0]),\
            reduce(lambda x,y: getattr(x,y) if isinstance(x, Row) else getattr(getattr(s,x),y), f2) if len(f2)>1 else getattr(s,f2[0])\
            )))\
            .reduceByKey(lambda x,y: makeList(x) + makeList(y))\
            .collect()
        numClasses=handler.numClasses()
        citer=iter(cm.rainbow(np.linspace(0, 1, numClasses)))
        colors = [next(citer) for i in range(0, numClasses)]
        legends= [handler.getClassLabel(i) for i in range(0,numClasses)]
        sets=[]
        fig, ax = plt.subplots(figsize=(12,8))
        for t in darr:
            sets.append((ax.scatter([x[0] for x in t[1]],[x[1] for x in t[1]],color=colors[t[0]],alpha=0.5),legends[t[0]]))

        ax.set_ylabel("Departure Airport Temp")
        ax.set_xlabel("Arrival Airport Temp")
        ax.legend([x[0] for x in sets],
                [x[1] for x in sets],
                scatterpoints=1,
                loc='lower left',
                ncol=numClasses,
                fontsize=12)

        #Render the figure
        (dialogTemplate, dialogOptions) = self.getDialogInfo(handlerId)
        dialogBody=self.renderTemplate(dialogTemplate, **dialogOptions)
        self.renderFigure(fig, dialogBody)
def runModel(depAirportCode, departureDT, arrAirportCode, arrivalDT):
    import training as f
    depTuple = getWeather(depAirportCode, departureDT)
    arrTuple = getWeather(arrAirportCode, arrivalDT)
    depWeather=depTuple[0]
    arrWeather=arrTuple[0]

    #create the features vector
    features=[]
    for attr in f.attributes:
        features.append(depWeather[mapAttribute(attr)])
    for attr in f.attributes:
        features.append(arrWeather[mapAttribute(attr)])
    
    #Call training handler for custom features
    s=type('dummy', (object,), {'departureTime':departureDT, 'arrivalTime':arrivalDT, 'arrivalAirportFsCode': arrAirportCode, 
                    'departureAirportFsCode':depAirportCode,'departureWeather': depWeather, 'arrivalWeather': arrWeather})
    customFeaturesForRunModel=f.getTrainingHandler().customTrainingFeatures(s) 
    for value in customFeaturesForRunModel:
        features.append( value )

    html='<table width=100%><tr><th>'+depTuple[1]+'</th><th>Prediction</th><th>'+arrTuple[1]+'</th></tr>'
    html+='<tr><td>'+formatWeather(depWeather)+'</td>'
    html+='<td><ul>'
    for model in mlModels:
        label= model.__class__.__name__
        html+='<li>' + label + ': ' + f.getTrainingHandler().getClassLabel(model.predict(features)) + '</li>'
    html+='</ul></td>'
    html+='<td>'+formatWeather(arrWeather)+'</td>'
    html+='</tr></table>'
    html+='<div id="map" style="height:300px"></div>'
    
    #display map
    html+='<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBBfYX6GG1foO1l7TAPk2LQVV_nACb7T4Q&callback=renderMap" type="text/javascript"></script>'
    html+='<script type="text/javascript">'
    html+='function renderMap() {'
    html+='var map = new google.maps.Map(document.getElementById("map"), {zoom: 4,center: new google.maps.LatLng(40, -100),mapTypeId: google.maps.MapTypeId.TERRAIN});'
    html+='var depAirport = new google.maps.LatLng(' + depTuple[2] + ',' + depTuple[3] + ');'
    html+='var arrAirport = new google.maps.LatLng(' + arrTuple[2] + ',' + arrTuple[3] + ');'
    html+='var markerP1 = new google.maps.Marker({position: depAirport, map: map});'
    html+='var markerP2 = new google.maps.Marker({position: arrAirport, map: map});'
    html+='var flightPlanCoordinates = [depAirport,arrAirport];'
    html+='var flightPath = new google.maps.Polyline({path: flightPlanCoordinates,strokeColor: "#0000FF",strokeOpacity: 1.0,strokeWeight: 2});'
    html+='flightPath.setMap(map);}'
    html+='</script>'

    display(HTML(html))