def clear_log(settings): if settings['debug'] == True: log = open('.genisislog', 'w') log.write(' ===LOG CLEARED BY GENISIS ON ' + ctime(time()) + '=== \n') log.close() speak('I\'ve cleared the log.') else: speak('I\'m sorry, ' + settings['name'] + ', I can\'t let you do that')
def file_read(): # Read excel file and get sheet names. print("Input the path of file:") data_source = input() # When draw file (with space in path) into terminal, there will have " charactor in path # Delete " charactor if exist while '\"' in data_source: data_source.remove('\"') # Check the file is exist or not while (path_config.path_check(data_source) == False): print( "The file path is not exist! Please input again.\nInput the path of file:" ) data_source = input() # Delete "" charactor if exist while '\"' in data_source: data_source.remove('\"') # Loading data file, get data and sheetnames print( "Loading data from %s.\nPlease wait for a while, it may need some times depend on size of file..." % data_source) data = xlrd.open_workbook(data_source) sheetnames = data.sheet_names() print("File Loading finished. Found sheet: %s" % sheetnames) log.write("info", "Loading file %s" % data_source) log.write("info", "File includes sheet %s" % sheetnames) # Choose sheet by "Data" and output print("Choose the sheet you want to analyse:") chosen_sheet = input() # Check if the sheet exist while chosen_sheet not in sheetnames: print( "The sheet you chose is not exist. Please input again.\nChoose the sheet you want to analyse:" ) chosen_sheet = input() print("Loading sheet %s, please wait..." % chosen_sheet) datasheet = data.sheet_by_name(chosen_sheet) print("Sheet loading finished.") log.write("info", "Loading sheet %s finished" % chosen_sheet) # Get numbers of rows and output nrows = datasheet.nrows print("Sheet %s has %d rows." % (chosen_sheet, nrows)) log.write("info", "Sheet %s has %d rows" % (chosen_sheet, nrows)) # Get numbers of cols and output ncols = datasheet.ncols print("Sheet %s has %d cols.\n" % (chosen_sheet, ncols)) log.write("info", "Sheet %s has %d cols" % (chosen_sheet, ncols)) return datasheet, nrows, ncols
def output_cpk_result(para, avg, std, pos3_delta, neg3_delta, data_min, data_max, cpl, cpu, cpk, low_limit_row, upp_limit_row): # Initial file outdata = Workbook() outsheet = outdata.worksheets[0] outsheet.title = 'CPK_Result' # Set style of output data # Font Type: Times New Roman # Size: 12 # Border: Thin # Alignment: Center # Pattern Fill: Red background color when CPK<1.33 font = Font('Times New Roman', size=12) fill = PatternFill(fill_type='solid', start_color='FF0000') border = Border(left=Side(border_style='thin', color='000000'), right=Side(border_style='thin', color='000000'), top=Side(border_style='thin', color='000000'), bottom=Side(border_style='thin', color='000000')) align = Alignment(horizontal='center', vertical='center') # Write excel file words = ('Parameter', 'Upper Limit', 'Lower Limit', 'AVG', 'STD', '+3Del', '-3Del', 'Max', 'Min', 'CPL', 'CPU', 'CPK') npara = len(avg) nwords = len(words) for i in range(nwords): outsheet.cell(row=i + 2, column=2).value = words[i] for i in range(npara): outsheet.cell(row=2, column=i + 3).value = para[i] outsheet.cell(row=3, column=i + 3).value = upp_limit_row[i] outsheet.cell(row=4, column=i + 3).value = low_limit_row[i] outsheet.cell(row=5, column=i + 3).value = avg[i] outsheet.cell(row=6, column=i + 3).value = std[i] outsheet.cell(row=7, column=i + 3).value = pos3_delta[i] outsheet.cell(row=8, column=i + 3).value = neg3_delta[i] outsheet.cell(row=9, column=i + 3).value = data_max[i] outsheet.cell(row=10, column=i + 3).value = data_min[i] outsheet.cell(row=11, column=i + 3).value = cpl[i] outsheet.cell(row=12, column=i + 3).value = cpu[i] outsheet.cell(row=13, column=i + 3).value = cpk[i] if (cpk[i] != "NA" and cpk[i] < 1.33): outsheet.cell(row=13, column=i + 3).fill = fill # Apply style for sheet for i in range(nwords): for j in range(len(cpk) + 1): outsheet.cell(row=i + 2, column=j + 2).font = font outsheet.cell(row=i + 2, column=j + 2).border = border outsheet.cell(row=i + 2, column=j + 2).alignment = align print("Output cpk excel file.") log.write("info", "Output CPK Excel File.") outdata.save(result_file) result_path = path_config.get_result_path() file_operation.result_file_move(result_file, result_path)
def cpkimg_data_check(workdata): # Data check log.write("info", "CPK Image - Start checking CPK image data.") check_data = list(workdata) while '' in check_data: check_data.remove('') new_data = tuple(check_data) log.write("info", "CPK Image - Data check finished.") return new_data
def data_range(): # Need user to input an integer number print('Please input the number beginning of row:') begin_row = int(input()) - 1 print('Please input the number beginning of col:') begin_col = int(input()) - 1 print("Row begins at %d. Col begins at %d" % (begin_row + 1, begin_col + 1)) # Record information of data range log.write( "info", "Chosen Range:\nRow begins at %d\nCol begins at %d" % (begin_row + 1, begin_col + 1)) return begin_row, begin_col
def find_value_by_col(worksheet, ncols, value_name, begin_row, end_row): result_control = 0 for i in range(ncols): workdata = worksheet.col_values(i, start_rowx=begin_row, end_rowx=end_row) for j in range(len(workdata)): if (value_name == str(workdata[j])): print("Found '%s' at row %d, col %d" % (value_name, i + 1, j + 1)) log.write( "info", "Found '%s' at row %d, col %d" % (value_name, j + 1, i + 1)) result_control = 1 break if (result_control == 1): break if (result_control == 0): print("Cannot find out value %s from sheet!Please check file!" % value_name) log.write("error", "Cannot find out value %s from sheet!" % value_name) else: return j, i
def result_file_move(file_name, target_path): shutil.move(file_name, target_path) log.write("info", "File %s have been moved to path %s." % (file_name, target_path))
def cpkimg_draw(para_name, data, low_limit, upp_limit, avg, std): log.write( "info", "CPK Image - Start draw CPK distribution image for %s" % para_name) # Check data workdata = cpkimg_data_check(data) title_name = para_name[0] # Define x-axis x_step_num = 20 if (upp_limit == "NA"): true_upp_limit = max(workdata) else: true_upp_limit = upp_limit if (low_limit == "NA"): true_low_limit = min(workdata) else: true_low_limit = low_limit x_axis = np.linspace(true_low_limit, true_upp_limit, x_step_num) # Initialize image style bins = x_axis alpha = 0.5 facecolor = 'gray' edgecolor = "black" #weights=np.ones_like(x_axis)/(len(x_axis)) # Draw frequency histogram frequency_each, bins_list, patches = pyplot.hist(workdata, bins=bins, facecolor=facecolor, edgecolor=edgecolor, alpha=alpha) pyplot.title(title_name) pyplot.xlabel("Value") pyplot.ylabel("Quantity") pyplot.grid(True) # Draw upper limit & lower limit & average if (low_limit != "NA" and upp_limit != "NA"): pyplot.axvline(x=low_limit, ls="-", c="red", label="Limit") pyplot.axvline(x=upp_limit, ls="-", c="red") elif (upp_limit != "NA"): pyplot.axvline(x=upp_limit, ls="-", c="red", label="Limit") elif (low_limit != "NA"): pyplot.axvline(x=low_limit, ls="-", c="red", label="Limit") pyplot.axvline(x=avg, ls="-", c="skyblue", label="Average") # Draw normal distribution under ideal conditions mu = avg sigma = std if (std == "not use now"): norm_x = np.linspace(mu - 3 * sigma, mu + 3 * sigma, 100) pdf = normfun(norm_x, mu, sigma) pyplot.plot(norm_x, pdf, ls="-", c="gray", label="Distribution") # Draw +3sigma & -3sigma limits if (sigma != 0): pyplot.axvline(x=mu - 3 * sigma, ls="--", c="skyblue", label=r'$\pm$3$\sigma$') pyplot.axvline(x=mu + 3 * sigma, ls="--", c="skyblue") # Show image pyplot.legend() # Save img img_file = title_name + ".png" pyplot.savefig(img_file) result_path = path_config.get_result_path() file_operation.result_file_move(img_file, result_path) # Close pyplot after image saved pyplot.close() log.write( "info", "CPK Image - %s distribution image draw finished.\n=========================" % para_name)
page=args.page[0]), 'grey')) except KeyError: print(colored('Could not activate / find page', 'red'), args.page[0]) exit() changed_files, needs_rebuild_from_files = log.convert_raw_entries(files) # this would return false for ok if any file is not a pair (= missing either a meta or a page file) print('File integrity: ', colored('OK ', 'green') if ok else colored('Error!', 'red')) # Force rebuild either by files or by command line option --force-rebuild needs_rebuild = args.force_rebuild or needs_rebuild_from_files builder = Builder(changed_files if not needs_rebuild else files) builder.prepare() builder.process_text_auto() if config['templates']['build_nav'] and needs_rebuild: builder.build_nav(files, use_absolute_links=False) # Render html to Jinja template builder.build(minify_html=config['output']['minify_html']) # Update log file after the successful build log.write() print(colored('BUILD SUCCESSFUL', 'green', 'on_grey'), ' -> Build time: {0}'.format(time.time() - build_time_start))
# Create the result path path_config.result_path(program_start_time) # Create the log path path_config.log_path() # Get result path result_path=path_config.get_result_path() # Get log path and setting log style log_path=path_config.get_log_path() log.log_style() # ---------------------------------------------------------------------------- # Output program messages # Output messages in log log.write("info","\n%s %s" %(program_name,version)) log.write("info","%s" %last_edit) log.write("info","Author: %s" %author) log.write("info","Program Start!\n") # Output messages in terminal print('*'*50) print("%s %s\nAuthor: %s\n%s\n%s\n" %(program_name,version,author,attention_msg,help_msg)) print("Program Start Time: %s" %program_start_time) print("Set Result Path: %s" %result_path) print("Set Log Path: %s\n" %log_path) print('*'*50+"\n") # ---------------------------------------------------------------------------- # Program start #try:
def test_cov_cal(old_para, old_limit, new_para, new_limit): same_para = () same_limit = () diff_para = () diff_limit_old = () diff_limit_new = () update_para = () update_limit = () delete_para = () delete_limit = () # Check new parameter log.write("info", "Test Coverage - Start checking new paramter:") for i in range(len(new_para)): para_find_status = 0 limit_find_status = 0 for j in range(len(old_para)): if (new_para[i] == old_para[j]): para_find_status = 1 if (new_limit[i] == old_limit[j]): limit_find_status = 1 continue if (para_find_status == 0 and limit_find_status == 0): log.write( "info", "Test Coverage - Parameter %s is a new one" % new_para[i]) update_para = update_para + (new_para[i], ) update_limit = update_limit + (new_limit[i], ) elif (para_find_status == 1 and limit_find_status == 0): log.write( "info", "Test Coverage - New limit %s for %s" % (new_limit[i], new_para[i])) diff_para = diff_para + (new_para[i], ) diff_limit_old = diff_limit_old + (old_limit[i], ) diff_limit_new = diff_limit_new + (new_limit[i], ) elif (para_find_status == 1 and limit_find_status == 1): log.write( "info", "Test Coverage - Same parameter %s & limit %s" % (new_para[i], new_limit[i])) same_para = same_para + (new_para[i], ) same_limit = same_limit + (new_limit[i], ) else: continue log.write( "info", "Test Coverage - New parameter check finished.\n=========================" ) # Check old parameter log.write("info", "Test Coverage - Start checking old parameter:") for i in range(len(old_para)): para_find_status = 0 for j in range(len(new_para)): if (new_para[j] == old_para[i]): para_find_status = 1 continue if (para_find_status == 0): log.write( "info", "Test Coverage - Parameter %s is not exist in new file." % old_para[i]) delete_para = delete_para + (old_para[i], ) delete_limit = delete_limit + (old_limit[i], ) log.write( "info", "Test Coverage - Old parameter check finished.\n=========================" ) # Output results print("All data analysis finished!\nOutput results as excel file...") test_cov_output(same_para, same_limit, diff_para, diff_limit_old, diff_limit_new, update_para, update_limit, delete_para, delete_limit)
def test_coverage_analysis(): # Read old & new test parameter file print("Test Coverage Analysis:\n1. Input Old Test Parameter File:") log.write("info", "Test Coverage - Read old test file:") (oldsheet, old_nrows, old_ncols) = file_operation.file_read() print("\n2. Input New Test Parameter File:") log.write("info", "Test Coverage - Read new test file:") (newsheet, new_nrows, new_ncols) = file_operation.file_read() # Find parameter begin position in sheet (old_para_row, old_para_col) = file_operation.find_value_by_col(oldsheet, old_ncols, key_name, 0, None) (new_para_row, new_para_col) = file_operation.find_value_by_col(newsheet, new_ncols, key_name, 0, None) # Find upper limit & lower limit begin position in sheet (old_upp_row, old_upp_col) = file_operation.find_value_by_row(oldsheet, old_nrows, upp_limit_name, 0, None) (old_low_row, old_low_col) = file_operation.find_value_by_row(oldsheet, old_nrows, low_limit_name, 0, None) (new_upp_row, new_upp_col) = file_operation.find_value_by_row(newsheet, new_nrows, upp_limit_name, 0, None) (new_low_row, new_low_col) = file_operation.find_value_by_row(newsheet, new_nrows, low_limit_name, 0, None) # Get parameter & limit data old_para_data = file_operation.one_col_data(oldsheet, old_para_col, old_para_row + 1, None) log.write( "info", "Test Coverage - Old file has %s parameters" % (len(old_para_data))) old_upp_data = file_operation.one_col_data( oldsheet, old_upp_col, old_upp_row + 1, old_upp_row + len(old_para_data) + 1) old_low_data = file_operation.one_col_data( oldsheet, old_low_col, old_low_row + 1, old_low_row + len(old_para_data) + 1) print("Old file data loaded.") log.write("info", "Test Coverage - Old file data loaded") new_para_data = file_operation.one_col_data(newsheet, new_para_col, new_para_row + 1, None) log.write( "info", "Test Coverage - Old file has %s parameters" % (len(new_para_data))) new_upp_data = file_operation.one_col_data( newsheet, new_upp_col, new_upp_row + 1, new_upp_row + len(new_para_data) + 1) new_low_data = file_operation.one_col_data( newsheet, new_low_col, new_low_row + 1, new_low_row + len(new_para_data) + 1) print("New file data loaded.") log.write("info", "Test Coverage - New file data loaded") # Combine limits old_limit = combine_limit(old_para_data, old_low_data, old_upp_data) new_limit = combine_limit(new_para_data, new_low_data, new_upp_data) log.write("info", "Test Coverage - Limits combined.") # Test Coverage Calculating print("Start analyse test coverage, please wait...") log.write( "info", "Test Coverage - Start analyse test coverage\n=========================" ) test_cov_cal(old_para_data, old_limit, new_para_data, new_limit) print("Excel file output finished!\nProgram finished!\n") log.write( "info", "Test Coverage - All test coverage analysis steps finished!\nProgram Finished!\n\n" )
def test_cov_output(same_para, same_limit, diff_para, diff_limit_old, diff_limit_new, new_para, new_limit, delete_para, delete_limit): # Initial file outdata = Workbook() outsheet = outdata.worksheets[0] outsheet.title = 'Coverage_Result' col_begin = 2 row_begin = 2 if (same_para != ()): log.write("info", "Test Coverage - Write same parameter in file") outsheet.cell(row=row_begin, column=col_begin).value = "Same Parameter" outsheet.cell(row=row_begin, column=col_begin + 1).value = "Limit" for i in range(len(same_para)): outsheet.cell(row=i + row_begin + 1, column=col_begin).value = same_para[i] outsheet.cell(row=i + row_begin + 1, column=col_begin + 1).value = same_limit[i] apply_style(outsheet, row_begin, col_begin, len(same_para) + row_begin, col_begin + 1) col_begin = col_begin + 3 if (diff_para != ()): log.write("info", "Test Coverage - Write changed parameter in file") outsheet.cell(row=row_begin, column=col_begin).value = "Changed Parameter" outsheet.cell(row=row_begin, column=col_begin + 1).value = "Old Limit" outsheet.cell(row=row_begin, column=col_begin + 2).value = "New Limit" for i in range(len(diff_para)): outsheet.cell(row=i + row_begin + 1, column=col_begin).value = diff_para[i] outsheet.cell(row=i + row_begin + 1, column=col_begin + 1).value = diff_limit_old[i] outsheet.cell(row=i + row_begin + 1, column=col_begin + 2).value = diff_limit_new[i] apply_style(outsheet, row_begin, col_begin, len(diff_para) + row_begin, col_begin + 2) col_begin = col_begin + 4 if (new_para != ()): log.write("info", "Test Coverage - Write added parameter in file") outsheet.cell(row=row_begin, column=col_begin).value = "Added Parameter" outsheet.cell(row=row_begin, column=col_begin + 1).value = "Limit" for i in range(len(new_para)): outsheet.cell(row=i + row_begin + 1, column=col_begin).value = new_para[i] outsheet.cell(row=i + row_begin + 1, column=col_begin + 1).value = new_limit[i] apply_style(outsheet, row_begin, col_begin, len(new_para) + row_begin, col_begin + 1) col_begin = col_begin + 3 if (delete_para != ()): log.write("info", "Test Coverage - Write deleted parameter in file") outsheet.cell(row=row_begin, column=col_begin).value = "Deleted Parameter" outsheet.cell(row=row_begin, column=col_begin + 1).value = "Limit" for i in range(len(delete_para)): outsheet.cell(row=i + row_begin + 1, column=col_begin).value = delete_para[i] outsheet.cell(row=i + row_begin + 1, column=col_begin + 1).value = delete_limit[i] apply_style(outsheet, row_begin, col_begin, len(delete_para) + row_begin, col_begin + 1) col_begin = col_begin + 3 # Write file outdata.save(result_file) result_path = path_config.get_result_path() file_operation.result_file_move(result_file, result_path)
def data_cpk_cal(para_name, workdata, upp_limit_row, low_limit_row, limit_col_num, begin_col, cpk_cb): # Calculate max & min of data single_max = max(workdata) single_min = min(workdata) # Calculate average of data ndata = len(workdata) single_avg = sum(workdata) / ndata # Calculate standard deviaion of data single_std = 0.0 for i in range(ndata): single_std = single_std + math.pow(workdata[i] - single_avg, 2) single_std = math.pow(single_std / (ndata - 1), 0.5) # Calculate CPL, CPU, CPK upp_limit = upp_limit_row[limit_col_num - begin_col] low_limit = low_limit_row[limit_col_num - begin_col] if (upp_limit == "NA" and low_limit == "NA"): log.write("info", "The limit is [NA,NA]") elif (upp_limit == "NA"): log.write("info", "The limit is [%s,NA]" % low_limit) elif (low_limit == "NA"): log.write("info", "The limit is [NA,%s]" % upp_limit) else: log.write("info", "The limit is [%s,%s]" % (low_limit, upp_limit)) if (single_std == 0 or (upp_limit == "NA" and low_limit == "NA")): log.write("warning", "This parametric std is 0 or limit is NA.") single_cpl = "NA" single_cpu = "NA" single_cpk = "NA" elif (upp_limit == "NA"): log.write("warning", "This parametric has no upper limit.") single_cpl = (single_avg - low_limit) / 3 / single_std single_cpu = "NA" single_cpk = single_cpl elif (low_limit == "NA"): log.write("warning", "This parametric has no lower limit.") single_cpl = "NA" single_cpu = (upp_limit - single_avg) / 3 / single_std single_cpk = single_cpu else: single_cpl = (single_avg - low_limit) / 3 / single_std single_cpu = (upp_limit - single_avg) / 3 / single_std single_cpk = min(single_cpl, single_cpu) # Output calculate results log.write("info", "The STD of this parametric is %f" % single_std) log.write("info", "The AVG of this parametric is %f" % single_avg) if (single_cpk != "NA"): log.write("info", "The CPK of this parametric is %f" % single_cpk) else: log.write("info", "The CPK of this parametric is NA") # Check CPK value if (single_cpk == "NA"): check_cb = (cpk_cb == 1) log.write("warning", "Parametric has no CPK value.\n=========================") elif (single_cpk < 1.33): check_cb = (cpk_cb == 1 or cpk_cb == 2) log.write("warning", "Parametric CPK is not OK.\n=========================") else: check_cb = (cpk_cb == 1) log.write("info", "Parametric CPK is OK.\n=========================") # Draw images of CPK data if (check_cb == True): cpk_img.cpkimg_draw(para_name, workdata, low_limit, upp_limit, single_avg, single_std) return single_avg, single_std, single_max, single_min, single_cpl, single_cpu, single_cpk
def cpk_analysis(): print("-" * 50) print("CPK Analysis:") # Initialize variables # Save data as tuple to avoid change avg = () std = () pos3_delta = () neg3_delta = () data_min = () data_max = () cpl = () cpu = () cpk = () # Read excel file (worksheet, nrows, ncols) = file_operation.file_read() (begin_row, begin_col) = data_range() # Get upper limit & lower limit from sheet (upp_row_num, upp_col_num) = file_operation.find_value_by_row(worksheet, nrows, upp_limit_name, 0, None) (low_row_num, low_col_num) = file_operation.find_value_by_row(worksheet, nrows, low_limit_name, 0, None) upp_limit_row = file_operation.one_row_data(worksheet, upp_row_num, begin_col, None) low_limit_row = file_operation.one_row_data(worksheet, low_row_num, begin_col, None) # Get parameter data from sheet para = worksheet.row_values(0, start_colx=begin_col, end_colx=None) log.write("info", "CPK Analysis Details:\n=========================") # Get cpk image mode # cpk_cb = 0 : No distribution image output # cpk_cb = 1 : Output all CPK distribution image # cpk_cb = 2 : Output CPK<1.33 distribution image cpk_cb = 0 while (cpk_cb != 1 and cpk_cb != 2): print( "Choose the way to output CPK distribution image:\n\t0. No Distribution Image Output\n\t1. Output All CPK Distribution\n\t2. Output CPK<1.33 Distribution" ) cpk_cb = int(input()) if (cpk_cb == 0): break # Calculating by col print("Start calculating, please wait...") for i in range(ncols): if (i < begin_col): continue else: para_name = file_operation.one_col_data(worksheet, i, 0, 1) workdata = file_operation.one_col_data(worksheet, i, begin_row, None) log.write("info", "Analyze parameter: %s" % para_name) workdata = cpk_data_check(workdata) if (workdata != ''): (single_avg, single_std, single_max, single_min, single_cpl, single_cpu, single_cpk) = data_cpk_cal(para_name, workdata, upp_limit_row, low_limit_row, i, begin_col, cpk_cb) # Result data is saved as tuple lists in case of no edit by mistake avg = avg + (single_avg, ) std = std + (single_std, ) pos3_delta = pos3_delta + (single_avg + single_std * 3, ) neg3_delta = neg3_delta + (single_avg - single_std * 3, ) data_min = data_min + (single_min, ) data_max = data_max + (single_max, ) cpl = cpl + (single_cpl, ) cpu = cpu + (single_cpu, ) cpk = cpk + (single_cpk, ) else: log.write("warning", "This parameter has no test value!") continue # Print results to excel file print("All data CPK calculate finished!\nOutput results as excel file...") output_cpk_result(para, avg, std, pos3_delta, neg3_delta, data_min, data_max, cpl, cpu, cpk, low_limit_row, upp_limit_row) print("Excel output finished!\nProgram finished!\n") log.write("info", "All CPK analysis steps finished!\nProgram Finished!\n\n")