├── Spectral-preprocessing-algorithm ├── matlab │ ├── example.m │ ├── nirmaf.m │ ├── savgol.m │ ├── sgoval.m │ ├── center.m │ ├── snv.m │ ├── scal.m │ ├── auto.m │ ├── rescal.m │ ├── normaliz.m │ ├── msc.m │ └── dosc.m └── python │ ├── example.py │ └── PreProcessing.py ├── README.md └── LICENSE /Spectral-preprocessing-algorithm/matlab/example.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FuSiry/Spectral-preprocessing-algorithm/HEAD/Spectral-preprocessing-algorithm/matlab/example.m -------------------------------------------------------------------------------- /Spectral-preprocessing-algorithm/matlab/nirmaf.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FuSiry/Spectral-preprocessing-algorithm/HEAD/Spectral-preprocessing-algorithm/matlab/nirmaf.m -------------------------------------------------------------------------------- /Spectral-preprocessing-algorithm/matlab/savgol.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FuSiry/Spectral-preprocessing-algorithm/HEAD/Spectral-preprocessing-algorithm/matlab/savgol.m -------------------------------------------------------------------------------- /Spectral-preprocessing-algorithm/matlab/sgoval.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FuSiry/Spectral-preprocessing-algorithm/HEAD/Spectral-preprocessing-algorithm/matlab/sgoval.m -------------------------------------------------------------------------------- /Spectral-preprocessing-algorithm/matlab/center.m: -------------------------------------------------------------------------------- 1 | function [mcx,mx] = center(x) 2 | % Mean center scales matrix to zero mean 3 | % 4 | % [mcx,mx] = center(x) 5 | % 6 | % input: 7 | % x data to mean center 8 | % 9 | % output: 10 | % ax mean center data 11 | % mx means of data 12 | % 13 | % By Cleiton A. Nunes 14 | % UFLA,MG,Brazil 15 | 16 | [m,n] = size(x); 17 | mx = mean(x); 18 | mcx = (x-mx(ones(m,1),:)); 19 | 20 | -------------------------------------------------------------------------------- /Spectral-preprocessing-algorithm/matlab/snv.m: -------------------------------------------------------------------------------- 1 | function [x_snv] = snv(x) 2 | % Standard Normal Variate 3 | % 4 | % [x_snv] = snv(x) 5 | % 6 | % input: 7 | % x (samples x variables) data to preprocess 8 | % 9 | % output: 10 | % x_snv (samples x variables) preprocessed data 11 | % 12 | % By Cleiton A. Nunes 13 | % UFLA,MG,Brazil 14 | 15 | [m,n]=size(x); 16 | rmean=mean(x,2); 17 | dr=x-repmat(rmean,1,n); 18 | x_snv=dr./repmat(sqrt(sum(dr.^2,2)/(n-1)),1,n); -------------------------------------------------------------------------------- /Spectral-preprocessing-algorithm/matlab/scal.m: -------------------------------------------------------------------------------- 1 | function sx = scal(x,mx,stdx) % Scals matrix using means and deviations % If only mean was provided, it can also be used. % % sx = scal(x,mx,stdx) % or % sx = scal(x,mx) % % input: % x data to scal % mx mean to consider % stdx standart deviation to consider % % output: % sx scaled data % % By Cleiton A. Nunes % UFLA,MG,Brazil [m,n]=size(x); if nargin==3 sx=(x-mx(ones(m,1),:))./stdx(ones(m,1),:); else sx=(x-mx(ones(m,1),:)); end -------------------------------------------------------------------------------- /Spectral-preprocessing-algorithm/matlab/auto.m: -------------------------------------------------------------------------------- 1 | function [ax,mx,stdx] = auto(x) 2 | % Autoscales matrix to zero mean and unit variance 3 | % 4 | % [ax,mx,stdx] = auto(x) 5 | % 6 | % input: 7 | % x data to autoscale 8 | % 9 | % output: 10 | % ax autoscaled data 11 | % mx means of data 12 | % stdx stantard deviations of data 13 | % 14 | % By Cleiton A. Nunes 15 | % UFLA,MG,Brazil 16 | 17 | 18 | 19 | [m,n] = size(x); 20 | mx = mean(x); 21 | stdx = std(x); 22 | ax = (x-mx(ones(m,1),:))./stdx(ones(m,1),:); 23 | 24 | 25 | -------------------------------------------------------------------------------- /Spectral-preprocessing-algorithm/matlab/rescal.m: -------------------------------------------------------------------------------- 1 | function rx = rescal(x,mx,stdx) 2 | % Rescals matrix using means and standard deviations 3 | % If only mean was provided, it can also be used. 4 | % 5 | % rx = rescal(x,mx,stdx) 6 | % or 7 | % rx = rescal(x,mx) 8 | % 9 | % input: 10 | % x data to rescal 11 | % mx mean to consider 12 | % stdx standart deviation to consider 13 | % 14 | % output: 15 | % rx rescaled data 16 | % 17 | % By Cleiton A. Nunes 18 | % UFLA,MG,Brazil 19 | 20 | 21 | [m,n]=size(x); 22 | if nargin == 3 23 | rx=(x.*stdx(ones(m,1),:))+mx(ones(m,1),:); 24 | else 25 | rx=x+mx(ones(m,1),:); 26 | end 27 | -------------------------------------------------------------------------------- /Spectral-preprocessing-algorithm/matlab/normaliz.m: -------------------------------------------------------------------------------- 1 | function [nx] = normaliz(x) 2 | % Normalize matrix rows dividing by its norm 3 | % 4 | % [nx] = normaliz(x) 5 | % 6 | % input: 7 | % x (samples x variables) data to normalize 8 | % 9 | % output: 10 | % nx (samples x variables) normalized data 11 | % 12 | % By Cleiton A. Nunes 13 | % UFLA,MG,Brazil 14 | 15 | [m,n]=size(x); 16 | nx=x; 17 | nm=zeros(m,1); 18 | for i = 1:m 19 | nm(i)=norm(nx(i,:)); 20 | nx(i,:)=nx(i,:)/nm(i); 21 | end 22 | % z=[1,2,3;3,4,5]; 23 | % [m,n]=size(z); 24 | % nx=z; 25 | % nm=zeros(m,1); 26 | % for i = 1:m 27 | % nm(i)=norm(nx(i,:)); 28 | % nx(i,:)=nx(i,:)/nm(i); -------------------------------------------------------------------------------- /Spectral-preprocessing-algorithm/matlab/msc.m: -------------------------------------------------------------------------------- 1 | function [x_msc]=msc(x,xref) 2 | % Multiplicative Scatter Correction 3 | % 4 | % [x_msc]=msc(x,xref) 5 | % 6 | % input 7 | % x (samples x variables) spectra to correct 8 | % xref (1 x variables) reference spectra (in general mean(x) is used) 9 | % 10 | % Output 11 | % x_msc (samples x variables) corrected spectra 12 | % 13 | % By Cleiton A. Nunes 14 | % UFLA,MG,Brazil 15 | 16 | [m n]=size(x); 17 | rs=xref;cw=ones(1,n); 18 | mz=[];mz=[mz ones(1,n)'];mz=[mz rs']; 19 | [mm,nm]=size(mz); 20 | wmz=mz.*(cw'*ones(1,nm)); 21 | wz=x.*(ones(m,1)*cw); 22 | z=wmz'*wmz; 23 | [u,s,v]=svd(z);sd=diag(s)'; 24 | cn=10^12; 25 | ms=sd(1)/sqrt(cn); 26 | cs=max(sd,ms ); 27 | cz=u*(diag(cs))*v'; 28 | zi=inv(cz); 29 | b=zi*wmz'*wz';B=b'; 30 | x_msc=x; 31 | p=B(:,1);x_msc=x_msc-(p*ones(1,mm)); 32 | p=B(:,2);x_msc=x_msc./(p*ones(mm,1)'); -------------------------------------------------------------------------------- /Spectral-preprocessing-algorithm/python/example.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | from PreProcessing import * 4 | 5 | #载入数据 6 | data_path = './/data//data.csv' #数据 7 | xcol_path = './/data//xcol.csv' #波长 8 | data = np.loadtxt(open(data_path, 'rb'), dtype=np.float64, delimiter=',', skiprows=0) 9 | xcol = np.loadtxt(open(xcol_path, 'rb'), dtype=np.float64, delimiter=',', skiprows=0) 10 | 11 | # 绘制MSC预处理后图片 12 | plt.figure(500) 13 | x_col = xcol #数组逆序 14 | y_col = np.transpose(data) 15 | plt.plot(x_col, y_col) 16 | plt.xlabel("Wavenumber(nm)") 17 | plt.ylabel("Absorbance") 18 | plt.title("The spectrum of the raw for dataset",fontweight= "semibold",fontsize='large') #记得改名字MSC 19 | plt.show() 20 | 21 | #数据预处理、可视化和保存 22 | datareprocessing_path = './/data//dataMSC.csv' #波长 23 | Data_Msc = MSC(data) #改这里的函数名就可以得到不同的预处理 24 | 25 | # 绘制MSC预处理后图片 26 | plt.figure(500) 27 | x_col = xcol #数组逆序 28 | y_col = np.transpose(Data_Msc) 29 | plt.plot(x_col, y_col) 30 | plt.xlabel("Wavenumber(nm)") 31 | plt.ylabel("Absorbance") 32 | plt.title("The spectrum of the MSC for dataset",fontweight= "semibold",fontsize='large') #记得改名字MSC 33 | plt.show() 34 | 35 | #保存预处理后的数据 36 | np.savetxt(datareprocessing_path, Data_Msc, delimiter=',') -------------------------------------------------------------------------------- /Spectral-preprocessing-algorithm/matlab/dosc.m: -------------------------------------------------------------------------------- 1 | function [Z,W,P,T] = dosc(X,Y,nocomp,tol) 2 | % DOSC Direct Orthogonal Signal Correction 3 | % 4 | % [Z,W,P,T] = dosc(X,Y,nocomp,tol); 5 | % 6 | % Input 7 | % X Matrix of predictor variables (usually spectra) (I x J) 8 | % Y Predicted variables (e.g. concentration) (I x K) 9 | % nocomp number of DOSC components to calculate 10 | % tol tolerance used to calculate pseudinverse of X 11 | % a tolerance of 1E-3 worked well in two cases presented in paper [1] 12 | % 13 | % 14 | % Output 15 | % Z DOSC corrected matrix X (I x J) 16 | % W Weights used to determine DOSC components (J x nocomp) 17 | % P Loadings used to remove DOSC component from X (J x nocomp) 18 | % T DOSC components (I x nocomp) 19 | % 20 | % 21 | % Once the calibration is done, new (scaled) x-data can be corrected by 22 | % newx = x - x*W*P'; Or use mfile dosc_pred 23 | % 24 | % See Reference 25 | % Westerhuis JA, de Jong S and Smilde AK, Direct orthogonal signal correction, 26 | % Chemometrics and Intelligent Laboratory Systems, 56, (2001), 13-25. 27 | 28 | % Sijmen de Jong, Oct 99 29 | % Adjusted by Johan Westerhuis 30 | % ========================================================================== 31 | % Copyright 2005 Biosystems Data Analysis Group ; Universiteit van Amsterdam 32 | % ========================================================================== 33 | 34 | % project Y onto X (step 1) 35 | Yhat = X*(pinv(X')'*Y); 36 | 37 | % deflate X wrt Yhat (step 2) 38 | AyX = X-Yhat*(pinv(Yhat)*X); 39 | 40 | % find major PCs of AyX 41 | [Ta,D] = eigs(AyX*AyX',nocomp); 42 | 43 | % Calculate pseudoinverse of X using tolerance (step 5a) 44 | pinvX = pinv(X',tol)'; 45 | 46 | % The tolerance can be changed to the number of PCR components (nc) used 47 | % to estimate the pseudo inverse. 48 | % [U,S,V] = svd(X,0); 49 | % Xcorr = U(:,1:nc)*S(1:nc,1:nc)*V(:,1:nc)'; 50 | % pinvX = pinv(Xcorr); 51 | 52 | W = pinvX*Ta; 53 | T = X*W; 54 | 55 | % Calculate loadings to remove DOSC component (step 7a) 56 | P = X'*T*inv(T'*T); 57 | 58 | % deflate X wrt to DOSC components (step 6a) 59 | Z = X - T*P'; -------------------------------------------------------------------------------- /Spectral-preprocessing-algorithm/python/PreProcessing.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from scipy import signal 3 | from sklearn.linear_model import LinearRegression 4 | from sklearn.preprocessing import MinMaxScaler, StandardScaler 5 | 6 | 7 | # 最大最小值归一化 8 | def MMS(data): 9 | return MinMaxScaler().fit_transform(data) 10 | 11 | 12 | # 标准化 13 | def SS(data): 14 | return StandardScaler().fit_transform(data) 15 | 16 | 17 | # 均值中心化 18 | def CT(data): 19 | for i in range(data.shape[0]): 20 | MEAN = np.mean(data[i]) 21 | data[i] = data[i] - MEAN 22 | return data 23 | 24 | 25 | # 标准正态变换 26 | def SNV(data): 27 | m = data.shape[0] 28 | n = data.shape[1] 29 | print(m, n) # 30 | # 求标准差 31 | data_std = np.std(data, axis=1) # 每条光谱的标准差 32 | # 求平均值 33 | data_average = np.mean(data, axis=1) # 每条光谱的平均值 34 | # SNV计算 35 | data_snv = [[((data[i][j] - data_average[i]) / data_std[i]) for j in range(n)] for i in range(m)] 36 | return data_snv 37 | 38 | 39 | 40 | # 移动平均平滑 41 | def MA(a, WSZ=21): 42 | for i in range(a.shape[0]): 43 | out0 = np.convolve(a[i], np.ones(WSZ, dtype=int), 'valid') / WSZ # WSZ是窗口宽度,是奇数 44 | r = np.arange(1, WSZ - 1, 2) 45 | start = np.cumsum(a[i, :WSZ - 1])[::2] / r 46 | stop = (np.cumsum(a[i, :-WSZ:-1])[::2] / r)[::-1] 47 | a[i] = np.concatenate((start, out0, stop)) 48 | return a 49 | 50 | 51 | # Savitzky-Golay平滑滤波 52 | def SG(data, w=21, p=3): 53 | return signal.savgol_filter(data, w, p) 54 | 55 | 56 | # 一阶导数 57 | def D1(data): 58 | n, p = data.shape 59 | Di = np.ones((n, p - 1)) 60 | for i in range(n): 61 | Di[i] = np.diff(data[i]) 62 | return Di 63 | 64 | 65 | # 二阶导数 66 | def D2(data): 67 | n, p = data.shape 68 | Di = np.ones((n, p - 2)) 69 | for i in range(n): 70 | Di[i] = np.diff(np.diff(data[i])) 71 | return Di 72 | 73 | 74 | # 趋势校正(DT) 75 | def DT(data): 76 | x = np.asarray(range(350, 2501), dtype=np.float32) 77 | out = np.array(data) 78 | l = LinearRegression() 79 | for i in range(out.shape[0]): 80 | l.fit(x.reshape(-1, 1), out[i].reshape(-1, 1)) 81 | k = l.coef_ 82 | b = l.intercept_ 83 | for j in range(out.shape[1]): 84 | out[i][j] = out[i][j] - (j * k + b) 85 | return out 86 | 87 | 88 | # 多元散射校正 89 | # MSC(数据) 90 | def MSC(Data): 91 | # 计算平均光谱 92 | n, p = Data.shape 93 | msc = np.ones((n, p)) 94 | 95 | for j in range(n): 96 | mean = np.mean(Data, axis=0) 97 | 98 | # 线性拟合 99 | for i in range(n): 100 | y = Data[i, :] 101 | l = LinearRegression() 102 | l.fit(mean.reshape(-1, 1), y.reshape(-1, 1)) 103 | k = l.coef_ 104 | b = l.intercept_ 105 | msc[i, :] = (y - b) / k 106 | return msc -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Spectral-preprocessing-algorithm 2 | Common preprocessing such as sg, msc, SNV, first-order derivative, second-order derivative, etc. 3 | 近红外光谱分析技术属于交叉领域,需要化学、计算机科学、生物科学等多领域的合作。为此,在(北邮邮电大学杨辉华老师团队)指导下,近期准备开源传统的PLS,SVM,ANN,RF等经典算和SG,MSC,一阶导,二阶导等预处理以及GA等波长选择算法以及CNN、AE等最新深度学习算法,以帮助其他专业的更容易建立具有良好预测能力和鲁棒性的近红外光谱模型。 4 | 代码仅供学术使用 5 | 6 |
7 | 8 | 9 | # python版本的预处理实例 10 | ## 1.搭建python环境 11 | 推荐基于anaconda安装python,参考安装如下: 12 | [基于anaconda安装python](https://zhuanlan.zhihu.com/p/347990651) 13 | 14 | ## 2.引入库 15 | ```python 16 | import numpy as np 17 | import matplotlib.pyplot as plt 18 | from scipy import signal 19 | from sklearn.linear_model import LinearRegression 20 | from sklearn.preprocessing import MinMaxScaler, StandardScaler 21 | ``` 22 | ## 3.读入数据、预处理以及展示 23 | 24 | ```python 25 | #载入数据 26 | data_path = './/data//data.csv' #数据 27 | xcol_path = './/data//xcol.csv' #波长 28 | data = np.loadtxt(open(data_path, 'rb'), dtype=np.float64, delimiter=',', skiprows=0) 29 | xcol = np.loadtxt(open(xcol_path, 'rb'), dtype=np.float64, delimiter=',', skiprows=0) 30 | 31 | # 绘制MSC预处理后图片 32 | plt.figure(500) 33 | x_col = xcol #数组逆序 34 | y_col = np.transpose(data) 35 | plt.plot(x_col, y_col) 36 | plt.xlabel("Wavenumber(nm)") 37 | plt.ylabel("Absorbance") 38 | plt.title("The spectrum of the raw for dataset",fontweight= "semibold",fontsize='large') #记得改名字MSC 39 | plt.show() 40 | 41 | #数据预处理、可视化和保存 42 | datareprocessing_path = './/data//dataMSC.csv' #波长 43 | Data_Msc = MSC(data) #改这里的函数名就可以得到不同的预处理 44 | 45 | # 绘制MSC预处理后图片 46 | plt.figure(500) 47 | x_col = xcol #数组逆序 48 | y_col = np.transpose(Data_Msc) 49 | plt.plot(x_col, y_col) 50 | plt.xlabel("Wavenumber(nm)") 51 | plt.ylabel("Absorbance") 52 | plt.title("The spectrum of the MSC for dataset",fontweight= "semibold",fontsize='large') #记得改名字MSC 53 | plt.show() 54 | 55 | #保存预处理后的数据 56 | np.savetxt(datareprocessing_path, Data_Msc, delimiter=',') 57 | ``` 58 | 59 | ## 4.结果(以msc为例) 60 | 原始光谱 61 | ![原始光谱](https://img-blog.csdnimg.cn/4772301c3ba840f1a142eec6fa7bb9b7.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBARWNob19Db2Rl,size_20,color_FFFFFF,t_70,g_se,x_16) 62 | msc预处理后 63 | ![msc预处理后](https://img-blog.csdnimg.cn/9a01b6a3eabe427aba1b44217e8e3a63.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBARWNob19Db2Rl,size_20,color_FFFFFF,t_70,g_se,x_16) 64 | #matlab预处理的实例 65 | # matlab版本的预处理实例 66 | ## 1.安装matlab 67 | ### 受matlab版权保护,matlab安装教程自行查找 68 | 69 | ## 2.读入数据、预处理以及展示 70 | 71 | ```python 72 | clc; 73 | clear; 74 | % close all; 75 | %% 打开文件 76 | %%%%%%%%% 77 | CA1 = csvread('D:\ProgramData\Inception\A1.csv'); 78 | Data = CA1; 79 | %%%%%%%%% 80 | 81 | %% 作图,原始图 82 | figure(1); 83 | title('光谱图'); 84 | xlabel('Wavenumber(cm-1)'); 85 | ylabel('Absorbance'); 86 | hold on; 87 | plot(Data'); 88 | 89 | %% 数据处理 90 | 91 | Absorbance = Data; 92 | [Absorbance_m,Absorbance_n]=size(Absorbance); 93 | Absorbance_mean = mean(Absorbance); 94 | % %% 吸光度波数 95 | % Absorbance=data; %得到吸光度 96 | % % Absorbance=Absorbance; 97 | % [Absorbance_m,Absorbance_n]=size(Absorbance); 98 | % Wavenumber=data(:,1:2:end); %得到波数 99 | % % Wavenumber=1:141; 100 | % Absorbance_mean=mean(Absorbance);%每个样本吸光度平均 101 | 102 | % %% 多元散射校正MSC 103 | % Absorbance_msc=msc(Absorbance,Absorbance_mean); 104 | % figure(2); 105 | % plot(Wavenumber,Absorbance_msc); 106 | % %set(gca,'XDir','reverse'); % 横坐标从大到小 107 | % title('多元散射校正MSC'); 108 | % xlabel('Wavenumber(cm-1)'); 109 | % ylabel('Absorbance'); 110 | %% 多元散射校正MSC 111 | msc_file_name = 'D:\DsekTop\MSCA1.csv'; 112 | Absorbance_msc=msc(Absorbance,Absorbance_mean); 113 | csvwrite(msc_file_name,Absorbance_msc,0,0); 114 | figure(2); 115 | title('多元散射校正MSC'); 116 | xlabel('Wavenumber(cm-1)'); 117 | ylabel('Absorbance'); 118 | hold on; 119 | plot(Absorbance_msc'); 120 | ``` 121 | 122 | ## 3.结果(以msc为例) 123 | 原始光谱 124 | 125 | ![原始光谱](https://img-blog.csdnimg.cn/057c74fe220c467da1dcb88f39531d8e.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBARWNob19Db2Rl,size_18,color_FFFFFF,t_70,g_se,x_16) 126 | MSC预处理后光谱 127 | ![MSC预处理后光谱](https://img-blog.csdnimg.cn/d19dd686a471454bae0e3f02bc106f0f.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBARWNob19Db2Rl,size_18,color_FFFFFF,t_70,g_se,x_16) 128 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------