在Apostrophe CMS中,我可以访问与上一个和下一个渲染的小部件有关的小部件顺序索引吗?

时间:2019-04-25 15:07:35

标签: apostrophe-cms

我希望有人能提供帮助。

在我的主页上,我只有一个窗口小部件类型feature-widgets,我有六个,一个接一个。我想在每个与下一个链接的小部件中添加一个向下箭头,我猜最简单的方法是使用小部件的顺序索引,例如#section-{{ index + 1 }}

有什么想法可以访问当前的小部件订单索引?

我找到了一种通过遍历页面模板上的data.page.home.items的方法,但这不是最好的解决方案。

谢谢!

1 个答案:

答案 0 :(得分:1)

由于Apostrophe不会将此信息传递给模板级别的单个窗口小部件,或者实际上不会告诉窗口小部件它们外部的任何信息,但是该信息在DOM中可见,因此您可以使用前端解决方案。由于链接没有SEO值,因此在客户端JavaScript中添加链接不会有任何危害。

您可以为此类型的小部件类型编写一个import matplotlib.pyplot as plt import numpy as np from scipy.fftpack import fft from scipy.signal import welch import glob import sys from numpy import NaN, Inf, arange, isscalar, asarray, array from statistics import mean np.set_printoptions(threshold=sys.maxsize) def peakdet(v, delta, x=None): """ Converted from MATLAB script at http://billauer.co.il/peakdet.html Returns two arrays function [maxtab, mintab]=peakdet(v, delta, x) %PEAKDET Detect peaks in a vector % [MAXTAB, MINTAB] = PEAKDET(V, DELTA) finds the local % maxima and minima ("peaks") in the vector V. % MAXTAB and MINTAB consists of two columns. Column 1 % contains indices in V, and column 2 the found values. % % With [MAXTAB, MINTAB] = PEAKDET(V, DELTA, X) the indices % in MAXTAB and MINTAB are replaced with the corresponding % X-values. % % A point is considered a maximum peak if it has the maximal % value, and was preceded (to the left) by a value lower by % DELTA. % Eli Billauer, 3.4.05 (Explicitly not copyrighted). % This function is released to the public domain; Any use is allowed. """ maxtab = [] mintab = [] if x is None: x = arange(len(v)) v = asarray(v) if len(v) != len(x): sys.exit('Input vectors v and x must have same length') if not isscalar(delta): sys.exit('Input argument delta must be a scalar') if delta <= 0: sys.exit('Input argument delta must be positive') mn, mx = Inf, -Inf mnpos, mxpos = NaN, NaN lookformax = True for i in arange(len(v)): this = v[i] if this > mx: mx = this mxpos = x[i] if this < mn: mn = this mnpos = x[i] if lookformax: if this < mx - delta: maxtab.append((mxpos, mx)) mn = this mnpos = x[i] lookformax = False else: if this > mn + delta: mintab.append((mnpos, mn)) mx = this mxpos = x[i] lookformax = True return array(maxtab), array(mintab) # Definition to get values needed for the FFT plot: def get_fft_values(y_values, T, N, f_s): f_values = np.linspace(0.0, 1.0/(2.0*T), N//2) fft_values_ = fft(y_values) fft_values = 2.0/N * np.abs(fft_values_[0:N//2]) return f_values, fft_values # Definition to find the values of axis: def findyaxis(y_axis_input, x, y): x = np.array(x) order = y.argsort() y = y[order] x = x[order] input = np.array(y_axis_input) return x[y.searchsorted(input, 'left')] def merge(list1, list2): merged_list = [(list1[i], list2[i]) for i in range(0, len(list1))] return merged_list def autocorr(x): result = np.correlate(x, x, mode='full') return result[len(result) // 2:] def get_autocorr_values(y_values, T, N, f_s): autocorr_values = autocorr(y_values) x_values = np.array([T * jj for jj in range(0, N)]) return x_values, autocorr_values def signaltonoise(a, axis=0, ddof=0): """ The signal - to - noise ratio of the input data. Returns the signal - to - noise ratio of `a`, here defined as the mean divided by the standard deviation. Parameters ---------- a: array_like An array_like object containing the sample data. axis: int or None, optional. If axis is equal to None, the array is first ravel 'd. If axis is an integer, this is the axis over which to operate.Default is 0. ddof: int, optional. Degrees of freedom correction for standard deviation.Default is 0. Returns ------- s2n: ndarray. The mean to standard deviation ratio(s) along `axis`, or 0 where the standard deviation is 0. """ a = np.asanyarray(a) m = a.mean(axis) sd = a.std(axis=axis, ddof=ddof) return np.where(sd == 0, 0, m/sd) def get_psd_values(y_values, T, N, f_s): f_values, psd_values = welch(y_values, fs=f_s) return f_values, psd_values def smooth(y, box_pts): box = np.ones(box_pts)/box_pts y_smooth = np.convolve(y, box, mode='same') return y_smooth # Assign folder to `folder`: DataPathList = sorted(glob.glob('DataPath*.txt'), key = lambda z: (len(z))) # DataSizerow = 0 # DataSizecolumn = 0 MaxDataSizerow = 1425 MaxDataSizecolumn = 15 Data = np.zeros((1426,15)) for fp in DataPathList: # Load spreadsheet: print('Opened file number: {}'.format(fp)) dataset = np.loadtxt(fname=fp) print('The size matrix of Sensors Undamaged Scenario:', dataset.shape) print('The column size matrix of Sensors Undamaged Scenario:',dataset.shape[1]) for k in range(1, dataset.shape[1]): # Create some time data to use for the plot: dt = 1 # Getting the time period and frequency: t_n = 2 N = 2192 T_s = 0.00390625 f_s = 256 # Obtaining data in order to plot the graph: y = dataset[:,k] x = np.arange(0, len(y), dt) x1 = np.linspace(0, t_n, N) SNR = signaltonoise(y) print('Signal-to-Noise Ratio (SNR): ', SNR, 'dB') SR = 1/t_n SR1 = 1/T_s Nf = (SR)/2 Nf1 = (SR1)/2 # Plotting the acceleration-time graph: # plt.plot(x1, y) # plt.xlabel('Time (s)') # plt.ylabel('Acceleration (ms^-2)') # plt.title('Plot of Sensor {0}'.format(k)) # # plt.show() # plt.show(block = False) # print('Plot of Sensor {0}'.format(k)) # plt.pause(5) # Pauses the program for 10 seconds # plt.close('all') ## Fast Fourier Transform (FFT) # Obtaining the Sampling frequency and time period: print('Period:', T_s, 's') print('Sampling Frequency: ', f_s, 'Hz') f_values, fft_values = get_fft_values(y, T_s, N, f_s) # Setting plot limits: ax = plt.gca() ax.set_ylim([min(fft_values), max(fft_values)]) ax.set_xlim([min(f_values), max(f_values)]) amp_index = np.array(fft_values) amp_index_max = max(amp_index) amp_index_min = min(amp_index) delta = (amp_index_max + amp_index_min)/2 # Obtaining the amplitude values: maxtab, mintab = np.array(peakdet(amp_index, delta)) amplitudes3 = maxtab y_axis_list = [] for e in range(len(amplitudes3)): amplitude3 = amplitudes3[e] amplitude3final = amplitudes3[e][1] y_values = amplitude3final y_axis_list.append(y_values) x_axis = np.abs(f_values) x_axis_list = [] for o in range(len(y_axis_list)): x_axis_values = findyaxis(y_axis_list[o], x_axis, fft_values) x_axis_list.append(x_axis_values) peaks = merge(x_axis_list, y_axis_list) print('Number of Peaks Coordinates: ', len(peaks)) print('Peaks Coordinates: ', peaks) # Plotting the amplitude-frequency graph: # plt.plot(f_values, fft_values, linestyle='-', color='blue') # plt.scatter(x_axis_list, y_axis_list, marker='*', color='red', label='Peaks: {0}'.format(len(peaks))) # plt.xlabel('Frequency [Hz]', fontsize=16) # plt.ylabel('Amplitude', fontsize=16) # plt.title("Frequency domain of the signal {0}".format(k), fontsize=16) # plt.legend() # # plt.show() # plt.show(block = False) # print('Frequency domain with peaks of the signal {0}'.format(k)) # plt.pause(5) # Pauses the program for 10 seconds # plt.close('all') # Obtaining the PSD values: f_values, psd_values = get_psd_values(y, T_s, N, f_s) amp_psd_index = np.array(psd_values) amp_psd_index_max = max(amp_psd_index) amp_psd_index_min = min(amp_psd_index) psd_delta = (amp_psd_index_max + amp_psd_index_min) / 2 maxtab, mintab = np.array(peakdet(amp_psd_index, psd_delta)) amplitudes_psd = maxtab y_axis_list_psd = [] for e in range(len(amplitudes_psd)): amplitude_psd = amplitudes_psd[e] amplitude_psd_final = amplitudes_psd[e][1] y_values_psd = amplitude_psd_final y_axis_list_psd.append(y_values_psd) x_axis_psd = np.abs(f_values) x_axis_list_psd = [] for o in range(len(y_axis_list_psd)): x_axis_values_psd = findyaxis(y_axis_list_psd[o], x_axis_psd, psd_values) x_axis_list_psd.append(x_axis_values_psd) psd_peaks = merge(x_axis_list_psd, y_axis_list_psd) print('Number of PSD Peaks Coordinates: ', len(psd_peaks)) print('PSD Peaks Coordinates: ', psd_peaks) # Plotting PSD-Frequency graph: # plt.plot(f_values, psd_values, linestyle='-', color='blue') # plt.scatter(x_axis_list_psd, y_axis_list_psd, marker='*', color='red', label='Peaks: {0}'.format(len(psd_peaks))) # plt.xlabel('Frequency [Hz]') # plt.ylabel('PSD [V**2 / Hz]') # plt.title("PSD of the signal {0}".format(k), fontsize=16) # plt.legend() # # plt.show() # plt.show(block = False) # print('PSD with peaks of the signal {0}'.format(k)) # plt.pause(5) # Pauses the program for 10 seconds # plt.close('all') # Obtaining AutoCorrelation values: t_values, autocorr_values = get_autocorr_values(y, T_s, N, f_s) amp_auto_corr_index = np.array(autocorr_values) amp_auto_corr_index_max = max(amp_auto_corr_index) amp_auto_corr_index_min = min(amp_auto_corr_index) auto_corr_delta = (amp_auto_corr_index_max + amp_auto_corr_index_min) / 2 maxtab, mintab = np.array(peakdet(amp_auto_corr_index, auto_corr_delta)) amplitudes_auto_corr = maxtab y_axis_list_auto_corr = [] for e in range(len(amplitudes_auto_corr)): amplitude_auto_corr = amplitudes_auto_corr[e] amplitude_auto_corr_final = amplitudes_auto_corr[e][1] y_values_auto_corr = amplitude_auto_corr_final y_axis_list_auto_corr.append(y_values_auto_corr) x_axis_auto_corr = np.abs(t_values) x_axis_list_auto_corr = [] for o in range(len(y_axis_list_auto_corr)): x_axis_values_auto_corr = findyaxis(y_axis_list_auto_corr[o], x_axis_auto_corr, autocorr_values) x_axis_list_auto_corr.append(x_axis_values_auto_corr) auto_corr_peaks = merge(x_axis_list_auto_corr, y_axis_list_auto_corr) print('Number of AutoCorrelation Peaks Coordinates: ', len(auto_corr_peaks)) print('AutoCorrelation Peaks Coordinates: ', auto_corr_peaks) # Plotting Autocorrelation-Time delay graph # plt.plot(t_values, autocorr_values, linestyle='-', color='blue') # plt.scatter(x_axis_list_auto_corr, y_axis_list_auto_corr, marker='*', color='red', label='Peaks: {0}'.format(len(auto_corr_peaks))) # plt.xlabel('time delay [s]') # plt.ylabel('Autocorrelation amplitude') # plt.title("AutoCorrelation of the signal {0}".format(k), fontsize=16) # plt.legend() # # plt.show() # plt.show(block = False) # print('AutoCorrelation with peaks of the signal {0}'.format(k)) # plt.pause(5) # Pauses the program for 10 seconds # plt.close('all') print('Completed file {}'.format(fp), ', Now going into filtering the signal') ######################################################################################################################## ############################################## Filtered Section ######################################################## ######################################################################################################################## # Plotting the smoothed filtered signal acceleration-time graph: y_filter = smooth(y, 10) # plt.plot(x1, y_filter) # plt.xlabel('Time (s)') # plt.ylabel('Acceleration (ms^-2)') # plt.title('Plot of Smoothed Sensor {0}'.format(k)) # # plt.show() # plt.show(block = False) # print('Plot of Smoothed Sensor {0}'.format(k)) # plt.pause(5) # Pauses the program for 10 seconds # plt.close('all') ## Filtered Fast Fourier Transform (FFT) # Obtaining the Sampling frequency and time period: print('Period:', T_s, 's') print('Sampling Frequency: ', f_s, 'Hz') f_values_filtered, fft_values_filtered = get_fft_values(y_filter, T_s, N, f_s) # Setting plot limits: ax = plt.gca() ax.set_ylim([min(fft_values_filtered), max(fft_values_filtered)]) ax.set_xlim([min(f_values_filtered), max(f_values_filtered)]) amp_index_filtered = np.array(fft_values_filtered) amp_index_filtered_max = max(amp_index_filtered) amp_index_filtered_min = min(amp_index_filtered) amp_index_filtered_delta = (amp_index_filtered_max + abs(amp_index_filtered_min)) / 2 # Obtaining the amplitude values: maxtab, mintab = np.array(peakdet(amp_index_filtered, amp_index_filtered_delta)) amplitudes3 = maxtab y_axis_list_filtered = [] for e in range(len(amplitudes3)): amplitude3 = amplitudes3[e] amplitude3final = amplitudes3[e][1] y_values_filtered = amplitude3final y_axis_list_filtered.append(y_values_filtered) x_axis_filtered = np.abs(f_values_filtered) x_axis_list_filtered = [] for o in range(len(y_axis_list_filtered)): x_axis_values_filtered = findyaxis(y_axis_list_filtered[o], x_axis_filtered, fft_values_filtered) x_axis_list_filtered.append(x_axis_values_filtered) peaks_filtered = merge(x_axis_list_filtered, y_axis_list_filtered) print('Number of Filtered Peaks Coordinates: ', len(peaks_filtered)) print('Filtered Peaks Coordinates: ', peaks_filtered) # Plotting the amplitude-frequency graph: # plt.plot(f_values_filtered, fft_values_filtered, linestyle='-', color='blue') # plt.scatter(x_axis_list_filtered, y_axis_list_filtered, marker='*', color='red', label='Peaks: {0}'.format(len(peaks_filtered))) # plt.xlabel('Frequency [Hz]', fontsize=16) # plt.ylabel('Amplitude', fontsize=16) # plt.title("Filtered Frequency domain of the signal {0}".format(k), fontsize=16) # plt.legend() # # plt.show() # plt.show(block = False) # print('Filtered Frequency domain with peaks of the signal {0}'.format(k)) # plt.pause(5) # Pauses the program for 10 seconds # plt.close('all') # Obtaining PSD Filtered values: f_values_filtered, psd_values_filtered = get_psd_values(y_filter, T_s, N, f_s) amp_psd_index_filtered = np.array(psd_values_filtered) amp_psd_index_filtered_max = max(amp_psd_index_filtered) amp_psd_index_filtered_min = min(amp_psd_index_filtered) amp_psd_index_filtered_delta = (amp_psd_index_filtered_max + abs(amp_psd_index_filtered_min)) / 2 maxtab, mintab = np.array(peakdet(amp_psd_index_filtered, amp_psd_index_filtered_delta)) amplitudes_psd_filtered = maxtab y_axis_list_psd_filtered = [] for e in range(len(amplitudes_psd_filtered)): amplitude_psd_filtered = amplitudes_psd_filtered[e] amplitude_psd_final_filtered = amplitudes_psd_filtered[e][1] y_values_psd_filtered = amplitude_psd_final_filtered y_axis_list_psd_filtered.append(y_values_psd_filtered) x_axis_psd_filtered = np.abs(f_values_filtered) x_axis_list_psd_filtered = [] for o in range(len(y_axis_list_psd_filtered)): x_axis_values_psd_filtered = findyaxis(y_axis_list_psd_filtered[o], x_axis_psd_filtered, psd_values_filtered) x_axis_list_psd_filtered.append(x_axis_values_psd_filtered) psd_peaks_filtered = merge(x_axis_list_psd_filtered, y_axis_list_psd_filtered) print('Number of Filtered PSD Peaks Coordinates: ', len(psd_peaks_filtered)) print('Filtered PSD Peaks Coordinates: ', psd_peaks_filtered) print('X-Axis Filtered PSD Amplitudes: ', amplitudes_psd_filtered[:, [0]]) length_amplitudes_psd_filtered = len(amplitudes_psd_filtered[:, [0]]) print('Amplitudes PSD filtered length: ', length_amplitudes_psd_filtered) if length_amplitudes_psd_filtered > 1: # for PSD_Mean in range(length_amplitudes_psd_filtered): X_axis_values_psd_mean = mean(x_axis_list_psd_filtered) print('Mean Amplitudes PSD filtered: ', X_axis_values_psd_mean) else: X_axis_values_psd_mean = x_axis_list_psd_filtered # Plotting PSD-Frequency filtered graph: # plt.plot(f_values_filtered, psd_values_filtered, linestyle='-', color='blue') # plt.scatter(x_axis_list_psd_filtered, y_axis_list_psd_filtered, marker='*', color='red', label='Peaks: {0}'.format(len(psd_peaks_filtered))) # plt.xlabel('Frequency [Hz]') # plt.ylabel('PSD [V**2 / Hz]') # plt.title("Filtered PSD of the signal {0}".format(k), fontsize=16) # plt.legend() # # plt.show() # plt.show(block = False) # print('Filtered PSD with peaks of the signal {0}'.format(k)) # plt.pause(5) # Pauses the program for 10 seconds # plt.close('all') # Obtaining Filtered AutoCorrelation values: t_values_filtered, autocorr_values_filtered = get_autocorr_values(y_filter, T_s, N, f_s) amp_auto_corr_index_filtered = np.array(autocorr_values_filtered) amp_auto_corr_index_filtered_max = max(amp_auto_corr_index_filtered) amp_auto_corr_index_filtered_min = min(amp_auto_corr_index_filtered) amp_auto_corr_index_filtered_delta = (amp_auto_corr_index_filtered_max + abs(amp_auto_corr_index_filtered_min)) / 2 maxtab, mintab = np.array(peakdet(amp_auto_corr_index_filtered, amp_auto_corr_index_filtered_delta)) amplitudes_auto_corr_filtered = maxtab y_axis_list_auto_corr_filtered = [] for e in range(len(amplitudes_auto_corr_filtered)): amplitude_auto_corr_filtered = amplitudes_auto_corr_filtered[e] amplitude_auto_corr_final_filtered = amplitudes_auto_corr_filtered[e][1] y_values_auto_corr_filtered = amplitude_auto_corr_final_filtered y_axis_list_auto_corr_filtered.append(y_values_auto_corr_filtered) x_axis_auto_corr_filtered = np.abs(t_values_filtered) x_axis_list_auto_corr_filtered = [] for o in range(len(y_axis_list_auto_corr_filtered)): x_axis_values_auto_corr_filtered = findyaxis(y_axis_list_auto_corr_filtered[o], x_axis_auto_corr_filtered, autocorr_values_filtered) x_axis_list_auto_corr_filtered.append(x_axis_values_auto_corr_filtered) auto_corr_peaks_filtered = merge(x_axis_list_auto_corr_filtered, y_axis_list_auto_corr_filtered) print('Number of Filtered AutoCorrelation Peaks Coordinates: ', len(auto_corr_peaks_filtered)) print('Filtered AutoCorrelation Peaks Coordinates: ', auto_corr_peaks_filtered) # Plotting AutoCorrelation-Time delay filtered graph: # plt.plot(t_values_filtered, autocorr_values_filtered, linestyle='-', color='blue') # plt.scatter(x_axis_list_auto_corr_filtered, y_axis_list_auto_corr_filtered, marker='*', color='red', label='Peaks: {0}'.format(len(auto_corr_peaks_filtered))) # plt.xlabel('time delay [s]') # plt.ylabel('Autocorrelation amplitude') # plt.title("Filtered AutoCorrelation of the signal {0}".format(k), fontsize=16) # plt.legend() # # plt.show() # plt.show(block = False) # print('Filtered AutoCorrelation with peaks of the signal {0}'.format(k)) # plt.pause(5) # Pauses the program for 10 seconds # plt.close('all') ######################################################################################################################## ############################################## Feature Matrix ########################################################## ######################################################################################################################## # Forming a feature matrix from frequency, PSD and AutoCorrelation values: for DataSizeRow in range(MaxDataSizerow): for DataSizeColumn in range(MaxDataSizecolumn): DataFrame_Feature = np.array(X_axis_values_psd_mean) Data[DataSizeColumn - 1] = DataFrame_Feature Data[DataSizeColumn + 1] break print('Data Frame: ', Data) # np.savetxt('DataFrameTestfinal1.txt', Data, delimiter = ' , ') # # np.savetxt('DataFrame3.txt', DataFrame, delimiter=' , ') # np.savetxt('DataFrameTestfinal2.txt', DataFrame1, delimiter=' , ') # np.savetxt('DataFrameTestfinal3.txt', DataFrame2, delimiter=' , ') print('Completed both original and filtered signals of file {}'.format(fp)) 方法。这应该做到:

play

请注意,我们不需要知道索引是什么,我们只需为所有小部件分配带有ID的锚,并在“下一次”点击时根据需要更新URL的哈希即可。