def parse(_excelfile, _head_busline_str, _head_input_set, _head_output_set):
    data = fh.parseInputFile(_excelfile)
    table = data.sheets()[0]
    nrows = table.nrows
    ncols = table.ncols

    # get DEA input
    y = []
    x = []
    names = []
    rowcnt = 1  # eat first row
    while (rowcnt < nrows):
        tempX = []
        tempY = []

        colcnt = 0
        while (colcnt < ncols):
            # check bus line name
            if (str(table.cell(0, colcnt).value) == _head_busline_str):
                if (isinstance(table.cell(rowcnt, colcnt).value, float)):
                    names.append(str(int(table.cell(rowcnt, colcnt).value)))
                else:
                    names.append(str(table.cell(rowcnt, colcnt).value))
            #check input
            if (str(table.cell(0, colcnt).value) in _head_input_set):
                tempX.append(float(table.cell(rowcnt, colcnt).value))
            # check output
            if (str(table.cell(0, colcnt).value) in _head_output_set):
                tempY.append(float(table.cell(rowcnt, colcnt).value))
            colcnt += 1

        x.append(tempX)
        y.append(tempY)

        rowcnt += 1
        print names[len(names) - 1]
        print tempX
        print tempY

    print(len(names))
    return names, y, x
Beispiel #2
0
# If output file exist, rename it with "_bak" suffix
if (True == os.path.exists(output_filename)):
    os.rename(output_filename, output_filename + "_bak")

fd = fh.openFile(output_filename)
fw = fh.getFileWriter(fd, profile_fields)
fh.writeFileHeader(fw)

log_fd = fh.openLogFile(output_log_file)
fh.writeLogRow(log_fd, "Start program")

# Judge input file type
input_file_type = fh.parseInputFileType(input_file)

# Get company list
company_list, keywords_list = fh.parseInputFile(input_file_type, input_file)
company_num = len(company_list)
if (0 == company_num):
    print("No company in the list, exit...")
    sys.exit(NO_COMPANY)
else:
    print("Found following companies:")
    print(company_list)

# Open outputfile
output_file = fh.openFile(output_filename)

##ToDo: Add a loop for all the companies
#company = 'AMERICAN ELECTRIC POWER CO'

# Browser
def calDEA(_excelfile, _output_path, _head_busline_str, _head_input_set,
           _head_output_set):
    names, _Y, _X = parse(_excelfile, _head_busline_str, _head_input_set,
                          _head_output_set)
    X = np.array(_X)
    Y = np.array(_Y)
    #print(X)
    #print(Y)
    dea = DEA(X, Y)
    dea.name_units(names)
    dict = dea.getResult()

    # Write json file
    jsonStr = json.dumps(dict)
    fd = fh.openRegularFile(_output_path + outputFile)
    fh.writeRegularFile(fd, jsonStr)
    fh.closeRegularFile(fd)

    # Write CSV file
    csv_filename = _output_path + outputCSV
    csv_fd = fh.openRegularFile(csv_filename)
    csv_fw = fh.getCSVFileWriter(csv_fd,
                                 ['Bus Line', 'Operational Efficiency'])
    fh.writeCSVFileHeader(csv_fw)
    for key, value in dict.items():
        fh.writeCSVRow(csv_fw, {
            'Bus Line': key,
            'Operational Efficiency': value
        })
    fh.closeRegularFile(csv_fd)

    # Write excel
    ref_data = fh.parseInputFile(_excelfile)
    ref_table = ref_data.sheets()[0]
    ref_nrows = ref_table.nrows
    ref_ncols = ref_table.ncols
    rowcnt = 0
    outfile, outtable = fh.excelWritableCreate("result")

    # get busline id column
    colcnt = 0
    busline_column_id = 0
    while (colcnt < ref_ncols):
        if (str(ref_table.cell(rowcnt, colcnt).value) == _head_busline_str):
            busline_column_id = colcnt
            break
        colcnt += 1

    while (rowcnt < ref_nrows):
        colcnt = 0
        while (colcnt < ref_ncols):
            fh.excelWritableCell(outtable, rowcnt, colcnt,
                                 ref_table.cell(rowcnt, colcnt).value)
            colcnt += 1
        if (0 == rowcnt):
            fh.excelWritableCell(outtable, rowcnt, colcnt,
                                 'Operational Efficiency')
        else:
            cur_busline = ""
            if (isinstance(
                    ref_table.cell(rowcnt, busline_column_id).value, float)):
                cur_busline = str(
                    int(ref_table.cell(rowcnt, busline_column_id).value))
            else:
                cur_busline = str(
                    ref_table.cell(rowcnt, busline_column_id).value)
            fh.excelWritableCell(outtable, rowcnt, colcnt, dict[cur_busline])
        rowcnt += 1

    fh.excelWritableSave(outfile, _output_path + outputExcel)

    return dict
def _btn_dea_excel_setting_action():
    if (0 == len(var_dea_excel_input.get())):
        tkMessageBox.showwarning("Error",
                                 "Please choose you DEA input excel file")
        return
    data = fh.parseInputFile(var_dea_excel_input.get())
    table = data.sheets()[0]
    nrows = table.nrows
    ncols = table.ncols

    # check data validation
    if (1 >= nrows or 2 >= ncols):
        tkMessageBox.showwarning("Error", "DEA input excel file is invalid")
        return
    colcnt = 0
    input_fields = []
    while (colcnt < ncols):
        input_fields.append(str(table.cell(0, colcnt).value))
        colcnt += 1

    dea_excel_setting_root = Toplevel()
    dea_excel_setting_root.geometry(
        str(FRAME_Width / 2) + "x" + str(FRAME_Height))
    dea_excel_setting_root.configure(background='White')

    frame_col = 0
    # put busline frame
    busline_frame = LabelFrame(dea_excel_setting_root,
                               text="Select busline field",
                               padx=10,
                               pady=10,
                               width=100,
                               height=100,
                               background='white')
    busline_frame.grid(sticky='n', row=0, column=frame_col)
    frame_col += 1

    dea_busline_lb = Listbox(busline_frame, selectmode=SINGLE)
    for item in input_fields:
        dea_busline_lb.insert(END, item)
    dea_busline_lb.pack()

    def _btn_dea_busline_add_action():
        global str_dea_excel_busline
        for b in dea_busline_lb.curselection():
            str_dea_excel_busline = dea_busline_lb.get(b)
        print str_dea_excel_busline

    btn_dea_busline_add = Button(busline_frame,
                                 text='Set',
                                 command=_btn_dea_busline_add_action)
    btn_dea_busline_add.pack()

    # put input frame
    input_frame = LabelFrame(dea_excel_setting_root,
                             text="Select input field(s)",
                             padx=10,
                             pady=10,
                             width=100,
                             height=100,
                             background='white')
    input_frame.grid(sticky='n', row=0, column=frame_col)
    frame_col += 1

    dea_input_lb = Listbox(input_frame, selectmode=MULTIPLE)
    for item in input_fields:
        dea_input_lb.insert(END, item)
    dea_input_lb.pack()

    def _btn_dea_input_add_action():
        global set_dea_excel_input
        set_dea_excel_input.clear()
        for b in dea_input_lb.curselection():
            set_dea_excel_input.add(dea_input_lb.get(b))
        print set_dea_excel_input

    btn_dea_input_add = Button(input_frame,
                               text='Set',
                               command=_btn_dea_input_add_action)
    btn_dea_input_add.pack()

    # put output frame
    output_frame = LabelFrame(dea_excel_setting_root,
                              text="Select output field(s)",
                              padx=10,
                              pady=10,
                              width=100,
                              height=100,
                              background='white')
    output_frame.grid(sticky='n', row=0, column=frame_col)
    frame_col += 1

    dea_output_lb = Listbox(output_frame, selectmode=MULTIPLE)
    for item in input_fields:
        dea_output_lb.insert(END, item)
    dea_output_lb.pack()

    def _btn_dea_output_add_action():
        global set_dea_excel_output
        set_dea_excel_output.clear()
        for b in dea_output_lb.curselection():
            set_dea_excel_output.add(dea_output_lb.get(b))
        print set_dea_excel_output

    btn_dea_output_add = Button(output_frame,
                                text='Set',
                                command=_btn_dea_output_add_action)
    btn_dea_output_add.pack()

    # the Done button
    def _btn_done():
        dea_excel_setting_root.destroy()

    btn_dea_excel_setting_done = Button(dea_excel_setting_root,
                                        text='Done',
                                        command=_btn_done)
    btn_dea_excel_setting_done.grid(sticky='w', row=2, column=2)
def _btn_textbox_update(event, combobox):
    remain_busline_num = combobox.get()
    root = Toplevel()
    root.geometry(str(FRAME_Width) + "x" + str(FRAME_Height))
    top = Canvas(root, bg='White')
    top.pack(expand=YES, fill=BOTH)

    # Create instruction
    top.create_text(200,
                    200,
                    font=("courier", 14),
                    text="Instruction:",
                    fill='red')
    top.create_text(
        300,
        300,
        font=("courier", 12),
        text=
        "Please Click the red circle \nto see the detailed information. \nAfter clicking the circle, \nplease wait until the chart appeared. \nDo not click again before the chart appeared."
    )

    # open files to get max and min coeff / population
    coeff_min = sys.float_info.max
    coeff_max = sys.float_info.min
    population_min = sys.maxint
    population_max = 0

    coeff_set = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
    for k in coeff_set:
        parse_file = var_output_rank_input.get() + "rank_excel_top_" + str(
            remain_busline_num) + "_operational_efficiency_weight_" + str(
                k) + ".xls"
        data = fh.parseInputFile(parse_file)
        table = data.sheets()[0]
        nrows = table.nrows
        cols = table.ncols
        if (nrows <= 1):
            continue
        coeff_min = min(coeff_min, float(table.cell(nrows - 2, 1).value))
        coeff_max = max(coeff_max, float(table.cell(nrows - 2, 1).value))
        population_min = min(population_min,
                             float(table.cell(nrows - 1, 1).value))
        population_max = max(population_max,
                             float(table.cell(nrows - 1, 1).value))

    # define circle button
    def _create_circle(self, x, y, r, **kwargs):
        return self.create_oval(x - r, y - r, x + r, y + r, **kwargs)

    Canvas.create_circle = _create_circle

    def onObjectClick(event, bus_line):
        print 'Got object click, bus line set = ', bus_line_set
        gr.drawMapView(var_equ_block_shp_input.get(),
                       var_equ_busRoutes_shp_input.get(), bus_line,
                       str_equ_blck_shp_population_id,
                       str_equ_busRoutes_shp_bus_id)

    # draw coordinate
    x_x0, x_x1, x_y1, x_y2 = (1250, 460, 620, 460)
    y_x0, y_x1, y_y1, y_y2 = (620, 25, 620, 460)

    x_line = top.create_line(
        (x_x0, x_x1, x_y1, x_y2),
        arrow='first',
        arrowshape='8 10 3',
        joinstyle='miter',
    )

    y_line = top.create_line(
        (y_x0, y_x1, y_y1, y_y2),
        arrow='first',
        arrowshape='8 10 3',
        joinstyle='miter',
    )
    b = 11
    top.create_text(x_x1,
                    y_y1 - 600,
                    text='Total Operational Efficiency Score')
    population_step = (population_max - population_min) / 10.0
    for i in range(1, b + 1):
        x_x1 -= 40
        x_y2 -= 40
        if (i < b):
            top.create_line((x_x0 - 40, x_x1, x_y1, x_y2), dash=3, fill='gray')
        if (i % 2 == 1):
            top.create_text(x_x1 + 610,
                            y_y1 - 600,
                            text="%.2f" %
                            ((b - i) * population_step + population_min))

    a = 11
    top.create_text(x_x1 - 30 + 950,
                    y_y1 - 150,
                    text='Total Disadvantaged Population Served')
    coeff_step = (coeff_max - coeff_min) / 10.0
    btn_circle = list(range(12))
    y_temp = y_y1
    for i in range(1, a + 1):
        y_x0 += 40
        y_y1 += 40
        '''
        print '-------------------------------'
        print 'Coordinate : [%d] '% i
        print '    x_x1 =  %s , x_y2 =  %s ' %(x_x1,x_y2)
        print '-------------------------------'
        '''
        if (i < a):
            top.create_line((y_x0, y_x1 + 20, y_y1, y_y2), dash=3, fill='gray')

        top.create_text(x_x1 - 30 + 580,
                        y_y1 - 600,
                        text="%.2f" % ((a - i) * coeff_step + coeff_min))

    # draw circle
    btn_cnt = 0
    last_coeff = -1.0
    last_population = -1.0
    for k in coeff_set:
        parse_file = var_output_rank_input.get() + "rank_excel_top_" + str(
            remain_busline_num) + "_operational_efficiency_weight_" + str(
                k) + ".xls"
        data = fh.parseInputFile(parse_file)
        table = data.sheets()[0]
        nrows = table.nrows
        cols = table.ncols
        if (nrows <= 1):
            continue
        coeff_cur = float(table.cell(nrows - 2, 1).value)
        population_cur = float(table.cell(nrows - 1, 1).value)

        if (coeff_cur == last_coeff and population_cur == last_population):
            continue
        bus_line_set = set('')
        bus_cnt = 1
        while (bus_cnt < nrows - 3):
            bus_line_set.add(str(table.cell(bus_cnt, 0).value))
            bus_cnt += 1

        #btn_circle[btn_cnt] = top.create_circle(x_x1 + 600, y_temp - 560, 5, fill="red", outline="#DDD", width=1)
        btn_circle[btn_cnt] = top.create_circle(
            x_x1 + 600 +
            (population_cur - population_min) / population_step * 40,
            y_y2 - (coeff_cur - coeff_min) / coeff_step * 40,
            5,
            fill="red",
            outline="#DDD",
            width=1)
        top.tag_bind(btn_circle[btn_cnt],
                     '<ButtonPress-1>',
                     lambda event, arg=bus_line_set: onObjectClick(event, arg))
        btn_cnt += 1