コード例 #1
0
def printError(my_dpi, data):
    """
    All data contanining the stock price info are retrieved from the database given the stock name
    :param my_dpi: dpi screen
    :param data: data to be plot
    :param max_error: maximum error allowed
    """

    try:
        stockFile = []
        try:
            for eachLine in data:
                splitLine = eachLine.split(',')
                if len(splitLine) == 2:
                    if 'values' not in eachLine:
                        stockFile.append(eachLine)
        except Exception as e:
            print(str(e), 'failed to organize pulled data.')
    except Exception as e:
        print(str(e), 'failed to pull pricing data')

    try:
        date, closep_raw = np.loadtxt(
            stockFile,
            delimiter=',',
            unpack=True,
            converters={0: mdates.bytespdate2num('%Y%m%d')})
        closep = closep_raw[::-1]

        max_error = 0.1
        while max_error < float(2 * max(closep)):
            segments1 = segment.slidingwindowsegment(closep, fit.interpolate,
                                                     fit.sumsquared_error,
                                                     max_error)
            segments2 = segment.slidingwindowsegment(closep, fit.regression,
                                                     fit.sumsquared_error,
                                                     max_error)
            err1 = str(evaluate_MSE(closep, segments1))
            err4 = str(evaluate_MSE(closep, segments2))
            segments1 = segment.topdownsegment(closep, fit.interpolate,
                                               fit.sumsquared_error, max_error)
            segments2 = segment.topdownsegment(closep, fit.regression,
                                               fit.sumsquared_error, max_error)
            err2 = str(evaluate_MSE(closep, segments1))
            err5 = str(evaluate_MSE(closep, segments2))
            segments1 = segment.bottomupsegment(closep, fit.interpolate,
                                                fit.sumsquared_error,
                                                max_error)
            segments2 = segment.bottomupsegment(closep, fit.regression,
                                                fit.sumsquared_error,
                                                max_error)
            err3 = str(evaluate_MSE(closep, segments1))
            err6 = str(evaluate_MSE(closep, segments2))
            print(
                str(max_error) + " " + err1 + " " + err2 + " " + err3 + " " +
                err4 + " " + err5 + " " + err6)
            max_error += 0.1

    except e:
        print("Error")
def draw_window_API(my_dpi, max_error, stockToFetch):
    """
    All data contanining the stock price info are retrieved from the database given the stock name
    :param my_dpi: dpi screen
    :param data: data to be plot
    :param max_error: maximum error allowed
    """

    fig = plt.figure(figsize=(1000 / my_dpi, 700 / my_dpi),
                     dpi=96,
                     edgecolor='k',
                     facecolor='black')
    fig.suptitle("PIECEWISE SEGMENTATION INTERPOLATION",
                 fontsize="15",
                 color="white",
                 fontweight='bold',
                 bbox={
                     'facecolor': 'red',
                     'alpha': 0.5,
                     'pad': 10
                 })

    try:
        print('Currently Pulling', stockToFetch)
        urlToVisit = 'http://chartapi.finance.yahoo.com/instrument/1.0/' + stockToFetch + '/chartdata;type=quote;range=5y/csv'
        stockFile = []
        try:
            sourceCode = urllib.request.urlopen(urlToVisit).read().decode()
            splitSource = sourceCode.split('\n')
            for eachLine in splitSource:
                splitLine = eachLine.split(',')
                if len(splitLine) == 6:
                    if 'values' not in eachLine:
                        stockFile.append(eachLine)
        except Exception as e:
            print(str(e), 'failed to organize pulled data.')
    except Exception as e:
        print(str(e), 'failed to pull pricing data')

    try:
        date, closep, highp, lowp, openp, volume = np.loadtxt(
            stockFile,
            delimiter=',',
            unpack=True,
            converters={0: mdates.bytespdate2num('%Y%m%d')})
        SP = len(date)
        # First subplot
        ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=3)
        segments = segment.slidingwindowsegment(closep, fit.interpolate,
                                                fit.sumsquared_error,
                                                max_error)
        draw_plot(closep, plt, ax1, "Sliding window with interpolation")
        draw_segments(segments, 'red')
        plt.ylabel('Stock Price')
        plt.title("Sliding window", color='w')

        # Second subplot
        ax2 = plt.subplot2grid((3, 3), (1, 0), colspan=3)
        segments = segment.topdownsegment(closep, fit.interpolate,
                                          fit.sumsquared_error, max_error)
        draw_plot(closep, plt, ax2, "Sliding window with interpolation")
        draw_segments(segments, 'green')
        plt.ylabel('Stock Price')
        plt.title("Top down", color='w')

        # Third subplot
        ax3 = plt.subplot2grid((3, 3), (2, 0), colspan=3)
        segments = segment.bottomupsegment(closep, fit.interpolate,
                                           fit.sumsquared_error, max_error)
        draw_plot(closep, plt, ax3, "Sliding window with interpolation")
        draw_segments(segments, 'blue')
        plt.ylabel('Stock Price')
        plt.title("Bottom up", color='w')

        plt.subplots_adjust(hspace=0.3)
        plt.show()

    except e:
        print("Error")
def draw_window(my_dpi, data, max_error):
    """
    All data contanining the stock price info are retrieved from the database given the stock name
    :param my_dpi: dpi screen
    :param data: data to be plot
    :param max_error: maximum error allowed
    """

    fig = plt.figure(figsize=(1000 / my_dpi, 700 / my_dpi),
                     dpi=96,
                     facecolor='black')
    fig.suptitle("PIECEWISE SEGMENTATION INTERPOLATION",
                 fontsize="15",
                 color="white",
                 fontweight='bold',
                 bbox={
                     'facecolor': 'red',
                     'alpha': 0.5,
                     'pad': 10
                 })

    try:
        stockFile = []
        try:
            for eachLine in data:
                splitLine = eachLine.split(',')
                if len(splitLine) == 2:
                    if 'values' not in eachLine:
                        stockFile.append(eachLine)
        except Exception as e:
            print(str(e), 'failed to organize pulled data.')
    except Exception as e:
        print(str(e), 'failed to pull pricing data')

    try:
        date, closep_raw = np.loadtxt(
            stockFile,
            delimiter=',',
            unpack=True,
            converters={0: mdates.bytespdate2num('%Y%m%d')})
        closep = closep_raw[::-1]

        print(max(closep))

        max_error = max(closep) * 2.5

        # First subplot
        ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=3)
        segments = segment.slidingwindowsegment(closep, fit.interpolate,
                                                fit.sumsquared_error,
                                                max_error)
        draw_plot(closep, plt, ax1, "Sliding window with interpolation")
        draw_segments(segments, 'red')
        plt.ylabel('Stock Price')
        plt.title("SLIDING WINDOW - ERROR " +
                  str(evaluate_global_error(closep, segments)),
                  color='Yellow',
                  fontweight='bold')

        # Second subplot
        ax2 = plt.subplot2grid((3, 3), (1, 0), colspan=3)
        segments = segment.topdownsegment(closep, fit.interpolate,
                                          fit.sumsquared_error, max_error)
        draw_plot(closep, plt, ax2, "Sliding window with interpolation")
        draw_segments(segments, 'green')
        plt.ylabel('Stock Price')
        plt.title("TOP DOWN - ERROR " +
                  str(evaluate_global_error(closep, segments)),
                  color='Yellow',
                  fontweight='bold')

        # Third subplot
        ax3 = plt.subplot2grid((3, 3), (2, 0), colspan=3)
        segments = segment.bottomupsegment(closep, fit.interpolate,
                                           fit.sumsquared_error, max_error)
        segments = segment.bottomupsegment(closep, fit.interpolate,
                                           fit.sumsquared_error, max_error)
        draw_plot(closep, plt, ax3, "Sliding window with interpolation")
        draw_segments(segments, 'blue')
        plt.ylabel('Stock Price')
        plt.title("BOTTOM UP - ERROR " +
                  str(evaluate_global_error(closep, segments)),
                  color='Yellow',
                  fontweight='bold')

        plt.subplots_adjust(hspace=0.3)
        plt.show()

    except e:
        print("Error")
コード例 #4
0
ファイル: eval.py プロジェクト: nhrjr/ExpandingBEC2D
def eval(directory,h5fileName):

	h5file = h5py.File(directory+'/'+h5fileName,'r')
	snapshots = h5file.attrs["SnapshotTimes"]

	if not os.path.exists(directory+"/runRegFit"):
		os.makedirs(directory+"/runRegFit")

	borders = []
	borders.append([])
	borders.append([])
	borders.append([])
	borders.append([])
	for times in snapshots:
		kSetName = str(times) + "/Observables/KVector"
		kvector = np.array(h5file[kSetName])
		nSetname = str(times) + "/Observables/OccupationNumber"
		nvector = np.array(h5file[nSetname])
		kvector = np.delete(kvector,0)
		nvector = np.delete(nvector,0)
		borders[0].append(kvector.min())
		borders[1].append(kvector.max())
		borders[2].append(nvector.min())
		borders[3].append(nvector.max())
	borders[0] = np.array(borders[0]).min()
	borders[1] = np.array(borders[1]).max()
	borders[2] = np.array(borders[2]).min()
	borders[3] = np.array(borders[3]).max()


	for times in snapshots:
		print("Fitting " + str(times))
		kSetName = str(times) + "/Observables/KVector"
		kvector = h5file[kSetName]
		nSetname = str(times) + "/Observables/OccupationNumber"
		nvector = h5file[nSetname]
		obs = h5file[str(times) + "/Observables"]
		meta = obs.attrs["Meta"]
		the_time = meta[0] * 1000 #in ms
	
		kLog = np.log(kvector)
		kLog = np.delete(kLog,0)
		nLog = np.log(nvector)
		nLog = np.delete(nLog,0)
	
		total_bins = 200
		bins = np.linspace(kLog.min(),kLog.max(),total_bins)
		delta = bins[1] - bins[0]
		idx = np.digitize(kLog,bins)
		running_median = [np.median(nLog[idx == k]) for k in range(total_bins)]
	
		data = running_median
		data = np.array(data)
		# data = np.nan_to_num(data)
		nans, x= nan_helper(data)
		data[nans]= np.interp(x(nans), x(~nans), data[~nans])
		data = data.tolist()
	
		max_error = 0.1
		#sliding window with regression
		figure()
		yscale('log')
		xscale('log')
		# subplot()
		# segments = segment.slidingwindowsegment(data, fit.regression, fit.sumsquared_error, max_error)

		segments = segment.bottomupsegment(data, fit.regression, fit.sumsquared_error, max_error)
		
		# segments = segment.topdownsegment(data, fit.regression, fit.sumsquared_error, max_error)
		
		# segments = segment.slidingwindowsegment(data, fit.interpolate, fit.sumsquared_error, max_error)
		
		# segments = segment.bottomupsegment(data, fit.interpolate, fit.sumsquared_error, max_error)
		
		# segments = segment.topdownsegment(data, fit.interpolate, fit.sumsquared_error, max_error)



		data = np.array(data)
		# data = np.exp(data)
		# bins = np.exp(bins)
		# segments = np.exp(segments)
		draw_plot(bins,data,"Regression Fit of the Spectrum at " + str(the_time) + " ms",borders)
		draw_segments(bins,segments)
	
	
		# fig = plt.figure()
		# fig.set_size_inches(10.0,12.0)
	
		# ax1 = fig.add_subplot(211)
		# subplot()
		# set_yscale('log')
		# set_xscale('log')
		# plot(kvector,nvector,'.',color='r',label=r'$R_x$ $GPE$')
	
	
	
		# ax2 = fig.add_subplot(212)
		# ax2.plot(bins-delta/2,running_median,'.')
		# plot(kLog,nLog)
		# # ax2.plot(kLog,interpolate.splev(kLog,tck,der=0))
	
		# plt.tight_layout()  
		# show()
		# # fig.text(.001,.001,txt)
		savefig(str(directory)+'/runRegFit/'+str(h5fileName)+'_'+str(times)+'_regfit.png',dpi=150)
		close()
コード例 #5
0
ファイル: example.py プロジェクト: NickFoubert/simple-segment
with open("example_data/16265-normalecg.txt") as f:
    file_lines = f.readlines()

data = [float(x.split("\t")[2].strip()) for x in file_lines[100:320]]

max_error = 0.005

# sliding window with regression
figure()
segments = segment.slidingwindowsegment(data, fit.regression, fit.sumsquared_error, max_error)
draw_plot(data, "Sliding window with regression")
draw_segments(segments)

# bottom-up with regression
figure()
segments = segment.bottomupsegment(data, fit.regression, fit.sumsquared_error, max_error)
draw_plot(data, "Bottom-up with regression")
draw_segments(segments)

# top-down with regression
figure()
segments = segment.topdownsegment(data, fit.regression, fit.sumsquared_error, max_error)
draw_plot(data, "Top-down with regression")
draw_segments(segments)


# sliding window with simple interpolation
figure()
segments = segment.slidingwindowsegment(data, fit.interpolate, fit.sumsquared_error, max_error)
draw_plot(data, "Sliding window with simple interpolation")
draw_segments(segments)
コード例 #6
0
    file_lines = f.readlines()

data = [float(x.split("\t")[2].strip()) for x in file_lines[100:320]]

max_error = 0.005

#sliding window with regression
figure()
segments = segment.slidingwindowsegment(data, fit.regression,
                                        fit.sumsquared_error, max_error)
draw_plot(data, "Sliding window with regression")
draw_segments(segments)

#bottom-up with regression
figure()
segments = segment.bottomupsegment(data, fit.regression, fit.sumsquared_error,
                                   max_error)
draw_plot(data, "Bottom-up with regression")
draw_segments(segments)

#top-down with regression
figure()
segments = segment.topdownsegment(data, fit.regression, fit.sumsquared_error,
                                  max_error)
draw_plot(data, "Top-down with regression")
draw_segments(segments)

#sliding window with simple interpolation
figure()
segments = segment.slidingwindowsegment(data, fit.interpolate,
                                        fit.sumsquared_error, max_error)
draw_plot(data, "Sliding window with simple interpolation")
コード例 #7
0
def draw_window_API(my_dpi, max_error, stockToFetch):
    """
    All data contanining the stock price info are retrieved from the database given the stock name
    :param my_dpi: dpi screen
    :param data: data to be plot
    :param max_error: maximum error allowed
    """

    fig = plt.figure(figsize=(1000/my_dpi, 700/my_dpi), dpi=96, edgecolor='k', facecolor='black')
    fig.suptitle("PIECEWISE SEGMENTATION REGRESSION", fontsize="15", color="white", fontweight='bold', bbox={'facecolor':'red', 'alpha':0.5, 'pad':10})

    try:
        print('Currently Pulling',stockToFetch)
        urlToVisit = 'http://chartapi.finance.yahoo.com/instrument/1.0/'+stockToFetch+'/chartdata;type=quote;range=5y/csv'
        stockFile =[]
        try:
            sourceCode = urllib.request.urlopen(urlToVisit).read().decode()
            splitSource = sourceCode.split('\n')
            for eachLine in splitSource:
                splitLine = eachLine.split(',')
                if len(splitLine) == 6:
                    if 'values' not in eachLine:
                        stockFile.append(eachLine)
        except Exception as e:
            print(str(e), 'failed to organize pulled data.')
    except Exception as e:
        print(str(e), 'failed to pull pricing data')

    try:
        date, closep, highp, lowp, openp, volume = np.loadtxt(stockFile, delimiter=',', unpack=True,
                                                              converters={0: mdates.bytespdate2num('%Y%m%d')})
        SP = len(date)
        # First subplot
        ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=3)
        segments = segment.slidingwindowsegment(closep, fit.regression, fit.sumsquared_error, max_error)
        draw_plot(closep,plt,ax1,"Sliding window with regression")
        draw_segments(segments,'red')
        plt.ylabel('Stock Price')
        plt.title("Sliding window", color='w')

        # Second subplot
        ax2 = plt.subplot2grid((3, 3), (1, 0), colspan=3)
        segments = segment.topdownsegment(closep, fit.regression, fit.sumsquared_error, max_error)
        draw_plot(closep, plt, ax2, "Sliding window with regression")
        draw_segments(segments,'green')
        plt.ylabel('Stock Price')
        plt.title("Top down", color='w')

        # Third subplot
        ax3 = plt.subplot2grid((3, 3), (2, 0), colspan=3)
        segments = segment.bottomupsegment(closep, fit.regression, fit.sumsquared_error, max_error)
        draw_plot(closep, plt, ax3, "Sliding window with regression")
        draw_segments(segments,'blue')
        plt.ylabel('Stock Price')
        plt.title("Bottom up", color='w')

        plt.subplots_adjust(hspace=0.3)
        plt.show()

    except e:
        print("Error")
コード例 #8
0
def draw_window(my_dpi, data):
    """
    All data contanining the stock price info are retrieved from the database given the stock name
    :param my_dpi: dpi screen
    :param data: data to be plot
    :param max_error: maximum error allowed
    """

    fig = plt.figure(figsize=(1000/my_dpi, 700/my_dpi), dpi=96, facecolor='black')
    fig.suptitle("PIECEWISE SEGMENTATION REGRESSION", fontsize="15", color="white", fontweight='bold', bbox={'facecolor':'red', 'alpha':0.5, 'pad':10})

    try:
        stockFile = []
        try:
            for eachLine in data:
                splitLine = eachLine.split(',')
                if len(splitLine) == 2:
                    if 'values' not in eachLine:
                        stockFile.append(eachLine)
        except Exception as e:
            print(str(e), 'failed to organize pulled data.')
    except Exception as e:
        print(str(e), 'failed to pull pricing data')

    try:
        date, closep_raw = np.loadtxt(stockFile, delimiter=',', unpack=True,
                                                              converters={0: mdates.bytespdate2num('%Y%m%d')})
        closep = closep_raw[::-1]
        max_closep = max(closep)

        if(max_closep > 2.0):
            max_error = max(closep)*2.7;
        else:
            max_error = max(closep)/2.5;

        print(max_error)

        # First subplot
        ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=3)
        segments = segment.slidingwindowsegment(closep, fit.regression, fit.sumsquared_error, max_error)
        draw_plot(closep,plt,ax1,"Sliding window with regression")
        draw_segments(segments,'red')
        plt.ylabel('Stock Price')
        plt.title("SLIDING WINDOW - ERROR "+str(evaluate_global_error(closep, segments)), color='Yellow', fontweight='bold')

        # Second subplot
        ax2 = plt.subplot2grid((3, 3), (1, 0), colspan=3)
        segments = segment.topdownsegment(closep, fit.regression, fit.sumsquared_error, max_error)
        draw_plot(closep, plt, ax2, "Sliding window with regression")
        draw_segments(segments,'green')
        plt.ylabel('Stock Price')
        plt.title("TOP DOWN - ERROR "+str(evaluate_global_error(closep, segments)), color='Yellow', fontweight='bold')

        # Third subplot
        ax3 = plt.subplot2grid((3, 3), (2, 0), colspan=3)
        segments = segment.bottomupsegment(closep, fit.regression, fit.sumsquared_error, max_error)
        draw_plot(closep, plt, ax3, "Sliding window with regression")
        draw_segments(segments,'blue')
        plt.ylabel('Stock Price')
        plt.title("BOTTOM UP - ERROR "+str(evaluate_global_error(closep, segments)), color='Yellow', fontweight='bold')

        plt.subplots_adjust(hspace=0.3)
        plt.show()

    except e:
        print("Error")