def ascii2bin(ascii_selfilename, bin_selfilename=None, ui=textui.TextUI()): # Convert dat file ascii_datfilename = ascii_selfilename.replace(".sel", '.dat') if bin_selfilename is None: bin_selfilename = ascii_selfilename[:-4] + "_bin.sel" ui.show_message("Converting %s to %s" % (ascii_selfilename, bin_selfilename)) cython_compile.cython_import('hawc2ascii2bin.cy_dat_ascii2bin') from hawc2ascii2bin.cy_dat_ascii2bin import dat_ascii2bin_progress size = size_from_file(ascii_selfilename) scale_factors = dat_ascii2bin_progress(ascii_datfilename, bin_selfilename.replace('.sel', '.dat'), size, ui=ui) # Read, convert and write sel file with open(ascii_selfilename) as f: lines = f.readlines() #lines[1] = " Version ID : Pydap %d.%d\n" % (version.__version__[:2]) lines[5] = " Result file : %s.dat\n" % bin_selfilename[:-4] lines[8] = lines[8].replace("ASCII", 'BINARY') lines.append("Scale factors:\n") for sf in scale_factors: lines.append(" %.5E\n" % sf) with open(bin_selfilename, 'w') as f: f.writelines(lines) if ui is not None: ui.show_message("Finish converting %s to %s" % (ascii_selfilename, bin_selfilename))
def ascii2bin(ascii_selfilename, bin_selfilename=None, ui=textui.TextUI()): # Convert dat file ascii_datfilename = ascii_selfilename.replace(".sel", '.dat') if bin_selfilename is None: bin_selfilename = ascii_selfilename[:-4] + "_bin.sel" ui.show_message("Converting %s \nto\n %s" % (ascii_selfilename, bin_selfilename)) cython_compile.cython_import('hawc2ascii2bin.cy_dat_ascii2bin') from hawc2ascii2bin.cy_dat_ascii2bin import dat_ascii2bin_progress size = size_from_file(ascii_selfilename) scale_factors = dat_ascii2bin_progress(ascii_datfilename, bin_selfilename.replace( '.sel', '.dat'), size, ui=ui) # Read, convert and write sel file with open(ascii_selfilename) as f: lines = f.readlines() #lines[1] = " Version ID : Pydap %d.%d\n" % (version.__version__[:2]) lines[5] = " Result file : %s.dat\n" % bin_selfilename[:-4] lines[8] = lines[8].replace("ASCII", 'BINARY') lines.append("Scale factors:\n") for sf in scale_factors: lines.append(" %.5E\n" % sf) with open(bin_selfilename, 'w') as f: f.writelines(lines) if ui is not None: ui.show_message("Finish converting %s to %s" % (ascii_selfilename, bin_selfilename))
def rainflow_windap_wrapper(signal): levels = 255. thresshold = (255. / 50) check_signal(signal) #type <double> is reuqired by <find_extreme> and <rainflow> signal = signal - np.min(signal) if np.max(signal) > 0: gain = float(np.max(signal)) / levels signal = signal / gain signal = np.round(signal).astype(np.int) #Import peak_throug(find extremes) and pair_range(rainflowcount). #If possible the module is compiled using cython otherwise the python implementation is used cython_import('common.fatigue.peak_trough') from peak_trough import peak_trough #Convert to list of local minima/maxima where difference > thresshold sig_ext = peak_trough(signal, thresshold) import_cython("common.fatigue.pair_range") from pair_range import pair_range #rainflow count sig_rfc = pair_range(sig_ext) #This is not smart but corresponds to the implementation in Windap sig_rfc = np.array(sig_rfc) #sig_rfc = sig_rfc[np.where(sig_rfc>=thresshold)[0]] sig_rfc = np.round(sig_rfc / thresshold) * gain * thresshold #sig_rfc = np.round(np.array(sig_rfc)/thresshold)*gain*thresshold #return sig_rfc[np.where(sig_rfc>=gain*thresshold)[0]] return sig_rfc
def testMatchingCython(self): from cython_compile import cython_import cython_import('string_matching_cython') import string_matching_cython # import must be after cython_import statement d = string_matching_cython.generate_special_scores([('D', 'd', 1)]) s = string_matching_cython.score_dict("Mads", ["mads", "MaDs"], d) self.assertAlmostEqual(s['mads'] / 4, 0.975) self.assertEqual(s['MaDs'] / 4, 1)
def compare(self, module_name, func, *args): clean(module_name) with open(module_name + '.py', 'w') as fid: fid.write(inspect.getsource(func)) res1, t_py = get_time(func)(*args) cython_import(module_name) cmodule = __import__(module_name) cfunc = getattr(cmodule, func.func_name) res2, t_cy = get_time(cfunc)(*args) self.assertEqual(res1, res2, "%s - %s" % (module_name, func)) clean(module_name) return t_py, t_cy
def rainflow_astm_wrapper(signal): check_signal(signal) # type <double> is reuqired by <find_extreme> and <rainflow> signal = signal.astype(np.double) # Import find extremes and rainflow. # If possible the module is compiled using cython otherwise the python implementation is used cython_import('rainflowcount_astm') from rainflowcount_astm import find_extremes, rainflow_astm # Remove points which is not local minimum/maximum sig_ext = find_extremes(signal) # rainflow count sig_rfc = rainflow_astm(sig_ext) return np.array(sig_rfc)
if p % y == 0: return False return True @cython_compile_autodeclare def cycheck_compile_autodeclare(p): import math for i in xrange(10): for y in xrange(2, int(math.sqrt(p)) + 1): if p % y == 0: return False return True p = 17 print pycheck(p) cython_import('cycheck') import cycheck print cycheck.cycheck(p) print cycheck.cycheck_pure(p) print cycheck.cycheck_cdef(p) print cycheck_compile(p) print cycheck_compile_autodeclare(p)
for i in xrange(10): for y in xrange(2, int(math.sqrt(p)) + 1): if p % y == 0: return False return True @cython_compile_autodeclare def cycheck_compile_autodeclare(p): import math for i in xrange(10): for y in xrange(2, int(math.sqrt(p)) + 1): if p % y == 0: return False return True p = 17 print pycheck(p) cython_import('cycheck') import cycheck print cycheck.cycheck(p) print cycheck.cycheck_pure(p) print cycheck.cycheck_cdef(p) print cycheck_compile(p) print cycheck_compile_autodeclare(p)
from cython_compile import is_compiled, cython_import import cy_test cython_import("cy_test") name = 'cy_test' print cy_test.CyTest(2) #4 print is_compiled(cy_test)