├── .github └── FUNDING.yml ├── Auto ├── coloringbook.py ├── eda01.py └── screening.py ├── CaseStudy └── Peak Detection │ ├── PeakDetection.py │ └── original.sce ├── Data ├── Sampling2D ├── nasa.py └── radiation.py ├── Ensamble ├── AutoFE.py ├── Crime.py ├── KFoldPSO.py ├── comparison.py └── memory.py ├── ExploratoryAnalysis ├── MetroCDMX.py └── files.py ├── Farming ├── Fish.py ├── NFTnutrients.R ├── ObjectDetection.py ├── data │ ├── Climate.csv │ ├── ClimateFull.csv │ └── readme.md ├── evaporation.py ├── nftdynamics.r └── rainwater.py ├── Generative ├── brownian.py ├── curves.py ├── curveswrapper.py ├── fractalsanimation.py ├── fractalsblender.py ├── randomcubes.py └── voxelfy.py ├── LICENSE ├── Life Sciences ├── AutoRiging.py ├── MiningIntAct.py ├── MiningIntActAdvViz.py ├── Proteome.py ├── Pubmed.py ├── RotationAnimation.py ├── Uniprot AdvancedViz.py └── UniprotDownloadData.py ├── Modeling ├── CyclinWee1System.R ├── InfectiousDisease.r ├── InfectiousDiseases02.r ├── InfectiousDiseases03.r ├── Windkessel.R ├── aggregates.jl ├── automata.r ├── circadian.jl ├── compost.r ├── epidemicstime.r ├── fermentation.R ├── filter.r ├── hh.r ├── lacOperon.R ├── mapk.R ├── pcr.r ├── progesterone.R ├── repressilator.r ├── signallingnfkb.R └── spread.r ├── NeuralNetworks ├── Autoencoder.py ├── MNISTAttention.py ├── MNISTConvMixer.py ├── MNISTConvolutional.py ├── MNISTDENSEConv.py ├── MNISTMixer.py ├── MNISTSpectralNorm.py ├── SimulatedAnnealing.py ├── evolutionaryvae.py └── nasa.py ├── ParameterEstimation ├── Complete │ ├── ODE Fitting03.py │ ├── ODE fitting 02.py │ ├── ODE fitting 4.py │ └── ODE fitting01.py └── Fragments │ └── ODEFiting01 │ ├── Fragment01.py │ ├── Fragment02.py │ ├── Fragment03.py │ └── Fragment04.py ├── README.md ├── Visualization ├── BarPlot.py ├── ClassifierDF.py ├── ClassifierDFBlender.py ├── Graph.py ├── GraphsVisualization.py ├── HeatmapBlender.py ├── Heatmapsmpl.py ├── LollipopPlot.py ├── PandasMap.py ├── RadarPlot.py ├── Scatter.py ├── Surfaces.py ├── WaffleChart.py ├── choropleth.py ├── cloning.py ├── confidenceintervals.py ├── dashboard.py ├── maps.py ├── spyderplot.py ├── themes.py └── waterfall2.py ├── education └── mathexcercises.py └── scrapping ├── FornitePlayers.py ├── ageofmithologyunits.py ├── market.py ├── prices.py └── weather.py /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | patreon: # Replace with a single Patreon username 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry 13 | custom: https://tavoglc.medium.com/membership 14 | -------------------------------------------------------------------------------- /Auto/coloringbook.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | """ 4 | Created on Wed May 24 01:29:29 2023 5 | 6 | @author: tavo 7 | """ 8 | 9 | import os 10 | import cv2 11 | import numpy as np 12 | import matplotlib.pyplot as plt 13 | 14 | imdir = '/home/tavo/Documentos/cats/' 15 | files = os.listdir(imdir) 16 | files = [val for val in files if os.path.isfile(imdir+val)] 17 | 18 | for val in files: 19 | 20 | fig,axs = plt.subplots(1,2,figsize=(20,10)) 21 | 22 | img = cv2.imread(imdir+val) 23 | img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) 24 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 25 | gray = cv2.medianBlur(gray, 11) 26 | edges = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 19,19) 27 | 28 | axs[0].imshow(img) 29 | axs[0].axis("off") 30 | 31 | axs[1].imshow(edges,cmap="gray") 32 | axs[1].axis("off") 33 | 34 | coloring = '/home/tavo/Documentos/cats/coloring/' 35 | 36 | for val in files: 37 | 38 | img = cv2.imread(imdir+val) 39 | img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) 40 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 41 | gray = cv2.medianBlur(gray, 11) 42 | edges = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 19,19) 43 | 44 | plt.figure(figsize=(20,20)) 45 | plt.imshow(edges,cmap="gray") 46 | plt.axis("off") 47 | plt.savefig(coloring+val,dpi=150) 48 | plt.close() 49 | -------------------------------------------------------------------------------- /CaseStudy/Peak Detection/PeakDetection.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | 4 | @author: TavoGLC 5 | From: 6 | 7 | -A simple peak detection method- 8 | 9 | """ 10 | 11 | ############################################################################### 12 | # Libraries to use 13 | ###############################A################################################ 14 | 15 | import numpy as np 16 | import matplotlib.pyplot as plt 17 | import matplotlib.patheffects as path_effects 18 | 19 | from matplotlib.gridspec import GridSpec 20 | 21 | ¡############################################################################### 22 | # General plot functions 23 | ############################################################################### 24 | 25 | #Elimates the left and top lines and ticks in a matplotlib plot 26 | def PlotStyle(Axes,Title): 27 | 28 | Axes.spines['top'].set_visible(False) 29 | Axes.spines['right'].set_visible(False) 30 | Axes.spines['bottom'].set_visible(True) 31 | Axes.spines['left'].set_visible(True) 32 | Axes.set_title(Title) 33 | 34 | ############################################################################### 35 | # Loading the data 36 | ############################################################################### 37 | 38 | #Directories where data is located 39 | DataDir='Data directory' 40 | DataFile=DataDir+'\\'+'FileName.csv' 41 | 42 | ############################################################################### 43 | # Data Visual Evaluation 44 | ############################################################################### 45 | 46 | Data=np.genfromtxt(DataFile,delimiter=';') 47 | 48 | TimeData=Data[:,0] 49 | TimeStep=TimeData[1]-TimeData[0] 50 | 51 | SignalData=Data[:,1] 52 | 53 | plt.figure(1) 54 | 55 | plt.plot(TimeData,SignalData) 56 | ax=plt.gca() 57 | PlotStyle(ax,' Full Recording ') 58 | 59 | ############################################################################### 60 | # Base Line Removal Function 61 | ############################################################################### 62 | 63 | nData=len(SignalData) 64 | nSample=int(0.15*nData) #Sample size 65 | gap=100 66 | 67 | #Finds a change in the base line and returns the location of the change 68 | def CropPoint(DataList): 69 | 70 | response=-1 71 | 72 | for k in range(len(DataList)-gap): 73 | 74 | DISC=np.abs((DataList[k]-DataList[k+gap])/DataList[k]) 75 | 76 | if DISC>2: 77 | 78 | response=k 79 | 80 | break 81 | 82 | else: 83 | 84 | pass 85 | 86 | return response 87 | 88 | ############################################################################### 89 | # Croping the data 90 | ############################################################################### 91 | 92 | #Moving average of the selected sample to remove the baseline 93 | AveragesFoward=[np.mean(SignalData[k:k+gap]) for k in range(nSample)] 94 | AveragesBackward=[np.mean(SignalData[len(SignalData)-gap-k:len(SignalData)-k]) for k in range(nSample)] 95 | 96 | #Croping points 97 | FowardCrop=CropPoint(AveragesFoward)+gap 98 | BackwardCrop=nData-(CropPoint(AveragesBackward)+gap) 99 | CropedSignal=SignalData[FowardCrop:BackwardCrop] 100 | 101 | CropedTime=[TimeStep*k for k in range(len(CropedSignal))] 102 | 103 | plt.figure(2) 104 | plt.plot(CropedTime,CropedSignal) 105 | ax=plt.gca() 106 | PlotStyle(ax,' Baseline removed') 107 | 108 | ############################################################################### 109 | # Peak segmentation 110 | ############################################################################### 111 | 112 | #Location of a value in a list, only the location of the first time that the value is encounter 113 | def Location(List,Value): 114 | 115 | for k in range(len(List)): 116 | 117 | if List[k]==Value: 118 | 119 | response=k 120 | break 121 | 122 | else: 123 | 124 | pass 125 | 126 | return response 127 | 128 | #Aproximate peak location 129 | def AproximateLocations(DataList): 130 | 131 | cData=DataList 132 | cnData=len(cData) 133 | cMx=np.max(cData) 134 | 135 | Locations=[k for k in range(cnData) if cData[k]<=cMx and (0.9*cMx)0.95*MaxDataValue); 113 | i=1; 114 | k=1; 115 | 116 | for j=1:length(MaxValuePositions)-1 117 | if MaxValuePositions(j+1)-MaxValuePositions(j)<500 then 118 | PeakData(i,k)=MaxValuePositions(j); 119 | i=i+1; 120 | else 121 | k=k+1; 122 | i=1; 123 | end 124 | end 125 | 126 | TotalPeaks=k; 127 | PeakPositions=zeros(TotalPeaks,1); 128 | 129 | clear i j k MaxDataValue MaxValuePositions 130 | 131 | for j=1:TotalPeaks 132 | a=find(PeakData(:,j)~=0); 133 | b=PeakData(a,j); 134 | c=CutFluo3DataAB(b(1):b(length(b))); 135 | d=max(c); 136 | e=find(c==d); 137 | PeakPositions(j)=b(1)+e(1); 138 | end 139 | 140 | clear a b c d e j PeakData 141 | 142 | //Separacion de los picos con ventana de tiempo fija 143 | 144 | TakenDataA=2000; 145 | TakenDataB=10000; 146 | FluoData=zeros(TakenDataA+TakenDataB,TotalPeaks); 147 | 148 | for j=1:TotalPeaks 149 | if j==TotalPeaks then 150 | for k=(PeakPositions(j)-TakenDataA+1):length(CutFluo3DataAB) 151 | FixFactor=abs(PeakPositions(j)-TakenDataA); 152 | FluoData(k-FixFactor,j)=CutFluo3DataAB(k); 153 | end 154 | else 155 | for k=(PeakPositions(j)-TakenDataA+1):(PeakPositions(j)+TakenDataB) 156 | FixFactor=abs(PeakPositions(j)-TakenDataA); 157 | FluoData(k-FixFactor,j)=CutFluo3DataAB(k); 158 | end 159 | end 160 | end 161 | 162 | DataLength=TakenDataA+TakenDataB; 163 | 164 | clear PeakPositions j k FixFactor TakenDataA TakenDataB 165 | 166 | LengthData=zeros(TotalPeaks,1); 167 | 168 | for j=1:TotalPeaks 169 | LengthData(j)=length(find(FluoData(:,j)~=0)); 170 | end 171 | 172 | GoodPeaks=find(LengthData==DataLength); 173 | 174 | clear LengthData j CutFluo3DataAB 175 | 176 | MeanFluoData=zeros(DataLength,1); 177 | 178 | for j=1:DataLength 179 | MeanFluoData(j)=mean(FluoData(j,GoodPeaks)); 180 | end 181 | 182 | clear GoodPeaks j 183 | -------------------------------------------------------------------------------- /Data/nasa.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | """ 4 | Created on Mon Mar 20 01:40:41 2023 5 | 6 | @author: tavo 7 | """ 8 | ############################################################################### 9 | # Loading packages 10 | ############################################################################### 11 | 12 | import os 13 | import numpy as np 14 | import pandas as pd 15 | from netCDF4 import Dataset 16 | from sklearn.ensemble import RandomForestRegressor 17 | 18 | import matplotlib.pyplot as plt 19 | from mpl_toolkits.basemap import Basemap 20 | 21 | ############################################################################### 22 | # Loading packages 23 | ############################################################################### 24 | 25 | def ImageStyle(Axes): 26 | """ 27 | Parameters 28 | ---------- 29 | Axes : Matplotlib axes object 30 | Applies a general style to the matplotlib object 31 | 32 | Returns 33 | ------- 34 | None. 35 | """ 36 | Axes.spines['top'].set_visible(False) 37 | Axes.spines['bottom'].set_visible(False) 38 | Axes.spines['left'].set_visible(False) 39 | Axes.spines['right'].set_visible(False) 40 | Axes.set_xticks([]) 41 | Axes.set_yticks([]) 42 | 43 | def MakeMap(data,ax): 44 | 45 | m = Basemap(projection='cyl',llcrnrlat=-65, urcrnrlat=85, 46 | llcrnrlon=-180, urcrnrlon=180,ax=ax) 47 | m.drawcoastlines(color='gray') 48 | m.drawcountries(color='gray') 49 | m.imshow(np.flip(data[5:155,:],axis=0),interpolation='gaussian') 50 | 51 | ############################################################################### 52 | # Loading packages 53 | ############################################################################### 54 | 55 | def ProcessVariable(dset,name): 56 | 57 | container_x = [] 58 | container_y = [] 59 | 60 | for k,val in enumerate(dset): 61 | 62 | locald = np.array(val.variables[name]) 63 | locations = np.argwhere(locald!=-9999) 64 | finaldata = np.hstack((locations,(k*np.ones((locations.shape[0],1))))) 65 | container_x.append(finaldata) 66 | container_y.append(locald[locald!=-9999]) 67 | 68 | container_x = np.vstack(container_x) 69 | container_y = np.hstack(container_y) 70 | 71 | regr = RandomForestRegressor(n_estimators=4,max_depth=32, 72 | n_jobs=-3, 73 | random_state=0) 74 | regr.fit(container_x, container_y) 75 | 76 | grid = np.argwhere(np.zeros((180,360))==0) 77 | outdata = np.hstack((grid,(k*np.ones((grid.shape[0],1))))) 78 | ydata = regr.predict(outdata) 79 | 80 | ydata = ydata.reshape(180,360) 81 | lastdata = np.array(dset[-1].variables[name]) 82 | locs = np.argwhere(lastdata==-9999) 83 | 84 | for sal in locs: 85 | i,j = sal 86 | lastdata[i,j] = ydata[i,j] 87 | 88 | return (ydata+lastdata)/2 89 | 90 | def ProcessData(dataList): 91 | 92 | dtacontainer = [] 93 | 94 | dtacontainer.append(ProcessVariable(dataList,'SurfPres_Forecast_TqJ_A')) 95 | dtacontainer.append(ProcessVariable(dataList,'SurfAirTemp_TqJ_A')) 96 | dtacontainer.append(ProcessVariable(dataList,'RelHumSurf_TqJ_A')) 97 | dtacontainer.append(ProcessVariable(dataList,'CloudFrc_TqJ_A')) 98 | dtacontainer.append(ProcessVariable(dataList,'TotO3_TqJ_A')) 99 | dtacontainer.append(ProcessVariable(dataList,'ClrOLR_TqJ_A')) 100 | 101 | dtacontainer.append(np.divide(dtacontainer[1],dtacontainer[0])) 102 | 103 | return dtacontainer 104 | 105 | ############################################################################### 106 | # Loading packages 107 | ############################################################################### 108 | 109 | dpi = 300 110 | 111 | fig_size = (15,10) 112 | 113 | img_dirs = ['/media/tavo/storage/dtaimg/0'+str(k+1) for k in range(7)] 114 | 115 | dataorg = '/media/tavo/storage/dta_ts/' 116 | datadir = dataorg + 'files/' 117 | 118 | basedir = '/media/tavo/storage/dta/' 119 | files = np.sort(os.listdir(basedir)) 120 | 121 | filePaths = [basedir+val for val in files] 122 | dates = [fls[29:39].replace('.','/') for fls in filePaths] 123 | 124 | filesDF = pd.DataFrame() 125 | 126 | filesDF['date'] = pd.to_datetime(dates) 127 | filesDF['paths'] = filePaths 128 | 129 | idx = pd.period_range(filesDF['date'].min(), filesDF['date'].max()) 130 | 131 | finaldates = [] 132 | finalpaths = [] 133 | ii=0 134 | 135 | for val in idx: 136 | if str(val)==str(filesDF['date'].iloc[ii])[0:10]: 137 | finaldates.append(str(val)) 138 | finalpaths.append(filesDF['paths'].iloc[ii]) 139 | ii=ii+1 140 | else: 141 | finaldates.append(str(val)) 142 | finalpaths.append(filesDF['paths'].iloc[ii]) 143 | 144 | finalPathsDF = pd.DataFrame() 145 | finalPathsDF['date'] = pd.to_datetime(finaldates) 146 | finalPathsDF['paths'] = finalpaths 147 | 148 | window_size = 90 149 | 150 | window = [] 151 | for k in range(window_size): 152 | 153 | dta = Dataset(filePaths[k]) 154 | window.append(dta) 155 | 156 | ############################################################################### 157 | # Loading packages 158 | ############################################################################### 159 | 160 | filenames = [] 161 | dates = [] 162 | 163 | for k in range(window_size,finalPathsDF.shape[0]): 164 | 165 | window = [] 166 | window_paths = finalPathsDF[k-window_size:k] 167 | 168 | for pt in window_paths['paths']: 169 | dta = Dataset(pt) 170 | window.append(dta) 171 | 172 | localData = ProcessData(window) 173 | 174 | saveData = np.stack(localData) 175 | name = 'rollingmean_'+str(window_size)+'_'+ str(window_paths['date'].iloc[-1]) 176 | np.save(datadir+name,saveData) 177 | filenames.append(name) 178 | 179 | for ii,sal in enumerate(img_dirs): 180 | 181 | plt.figure(figsize=fig_size) 182 | ax = plt.gca() 183 | MakeMap(localData[ii],ax) 184 | ax.margins(x=0) 185 | plt.box(False) 186 | plt.savefig(sal+'/img'+str(k-window_size)+'.png',dpi=dpi,bbox_inches='tight',pad_inches=0) 187 | plt.close() 188 | 189 | print(k) 190 | 191 | finalPathsDF.to_csv(dataorg+'MetaData.csv') 192 | -------------------------------------------------------------------------------- /Data/radiation.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | """ 4 | Created on Sat Jun 17 03:23:52 2023 5 | 6 | @author: tavo 7 | """ 8 | 9 | import numpy as np 10 | import pandas as pd 11 | import matplotlib.pyplot as plt 12 | 13 | from sklearn.ensemble import RandomForestRegressor 14 | 15 | ############################################################################### 16 | # Visualization functions 17 | ############################################################################### 18 | fontsize = 16 19 | 20 | def PlotStyle(Axes): 21 | """ 22 | Parameters 23 | ---------- 24 | Axes : Matplotlib axes object 25 | Applies a general style to the matplotlib object 26 | 27 | Returns 28 | ------- 29 | None. 30 | """ 31 | Axes.spines['top'].set_visible(False) 32 | Axes.spines['bottom'].set_visible(True) 33 | Axes.spines['left'].set_visible(True) 34 | Axes.spines['right'].set_visible(False) 35 | Axes.xaxis.set_tick_params(labelsize=fontsize) 36 | Axes.yaxis.set_tick_params(labelsize=fontsize) 37 | 38 | ############################################################################### 39 | # Visualization functions 40 | ############################################################################### 41 | 42 | def MaxPE(ytrue,ypred): 43 | cont = [] 44 | for val,sal in zip(ytrue,ypred): 45 | cont.append(100*(val-sal)/val) 46 | return np.max(cont) 47 | 48 | ############################################################################### 49 | # Visualization functions 50 | ############################################################################### 51 | 52 | wldata = pd.read_csv('/media/tavo/storage/sunspots/solarcurrent.csv') 53 | wldata = wldata[wldata['irradiance']>0] 54 | 55 | wbins = [200,290,320,400,700,1000,2500] 56 | wlnames = ['UVA','UVB','UVC','Vis','NIR','SWIR'] 57 | 58 | wdata = wldata.groupby(['date',pd.cut(wldata['wavelength'],wbins)])['irradiance'].mean().unstack() 59 | wdata.columns = wlnames 60 | 61 | wdata = wdata.reset_index() 62 | wdata['date'] = pd.to_datetime(wdata['date']) 63 | 64 | for val in wlnames: 65 | 66 | mean = wdata[val].mean() 67 | std = wdata[val].std() 68 | 69 | wdata[val] = [sal if np.abs((sal-mean)/std)<3 else mean for sal in wdata[val]] 70 | 71 | wdata['dayofyear'] = wdata['date'].dt.dayofyear 72 | wdata['week'] = wdata['date'].dt.isocalendar().week 73 | wdata['dayofweek'] = wdata['date'].dt.dayofweek 74 | wdata['year'] = wdata['date'].dt.year 75 | wdata['month'] = wdata['date'].dt.month 76 | wdata['quarter'] = wdata['date'].dt.quarter 77 | wdata['dim'] = wdata['date'].dt.daysinmonth 78 | wdata['weekend'] = [1 if val in [5,6] else 0 for val in wdata['week']] 79 | 80 | wdata = wdata.dropna() 81 | 82 | xlabs = ['dayofyear','week','dayofweek','year','month','quarter','dim','weekend'] 83 | 84 | for lab in xlabs: 85 | fig,axs = plt.subplots(1,len(wlnames),figsize=(20,7),sharey=True) 86 | grouped = wdata.groupby(lab) 87 | for jj,wl in enumerate(wlnames): 88 | dta = grouped[wl].mean() 89 | dta = (dta - dta.min())/(dta.max() - dta.min()) + 0.01 90 | if lab in xlabs[0:2]: 91 | dta.plot(ax=axs[jj]) 92 | else: 93 | dta.plot.bar(ax=axs[jj]) 94 | axs[jj].set_ylabel('Normalized Irradiance',fontsize=fontsize) 95 | axs[jj].text(0.01, 0.99, wl, size=16, color='black', ha='left', va='top', transform=axs[jj].transAxes) 96 | PlotStyle(axs[jj]) 97 | 98 | ############################################################################### 99 | # Visualization functions 100 | ############################################################################### 101 | 102 | window = 7 103 | xdata = wdata[xlabs].values 104 | ydata = wdata[wlnames].values 105 | 106 | xcont = [] 107 | ycont = [] 108 | 109 | for k in range(len(xdata)-window): 110 | 111 | xcont.append(xdata[k:k+window].ravel()) 112 | ycont.append(ydata[k+window]) 113 | 114 | xcont = np.array(xcont) 115 | ycont = np.array(ycont) 116 | 117 | regr = RandomForestRegressor(n_estimators=300,random_state=354) 118 | regr.fit(xcont,ycont) 119 | 120 | predvals = regr.predict(xcont) 121 | 122 | errors = [MaxPE(ycont[:,k],predvals[:,k]) for k in range(len(wlnames))] 123 | 124 | plt.figure() 125 | plt.bar(np.arange(len(wlnames)),errors) 126 | plt.xticks(np.arange(len(wlnames)),wlnames) 127 | plt.ylabel('Max % Error',fontsize=fontsize) 128 | ax = plt.gca() 129 | PlotStyle(ax) 130 | 131 | ############################################################################### 132 | # Visualization functions 133 | ############################################################################### 134 | 135 | fdata = pd.DataFrame() 136 | fdata['date'] = pd.date_range(wdata['date'].min(), wdata['date'].max()) 137 | fdata['dayofyear'] = fdata['date'].dt.dayofyear 138 | fdata['week'] = fdata['date'].dt.isocalendar().week 139 | fdata['dayofweek'] = fdata['date'].dt.dayofweek 140 | fdata['year'] = fdata['date'].dt.year 141 | fdata['month'] = fdata['date'].dt.month 142 | fdata['quarter'] = fdata['date'].dt.quarter 143 | fdata['dim'] = fdata['date'].dt.daysinmonth 144 | fdata['weekend'] = [1 if val in [5,6] else 0 for val in fdata['week']] 145 | 146 | xfdata = fdata[xlabs].values 147 | xfcont = [] 148 | 149 | for k in range(len(xfdata)-window): 150 | 151 | xfcont.append(xfdata[k:k+window].ravel()) 152 | 153 | xfcont = np.array(xfcont) 154 | predvalsf = regr.predict(xfcont) 155 | 156 | ############################################################################### 157 | # Visualization functions 158 | ############################################################################### 159 | 160 | outdata = pd.DataFrame() 161 | outdata['date'] = pd.date_range(wdata['date'].min()+pd.DateOffset(days=window), wdata['date'].max()) 162 | outdata['dayofyear'] = outdata['date'].dt.dayofyear 163 | outdata['week'] = outdata['date'].dt.isocalendar().week 164 | outdata['dayofweek'] = outdata['date'].dt.dayofweek 165 | outdata['year'] = outdata['date'].dt.year 166 | outdata['month'] = outdata['date'].dt.month 167 | outdata['quarter'] = outdata['date'].dt.quarter 168 | outdata['dim'] = outdata['date'].dt.daysinmonth 169 | outdata['weekend'] = [1 if val in [5,6] else 0 for val in outdata['week']] 170 | 171 | 172 | for k in range(len(wlnames)): 173 | outdata[wlnames[k]] = predvalsf[:,k] 174 | 175 | for lab in xlabs: 176 | fig,axs = plt.subplots(1,len(wlnames),figsize=(20,7),sharey=True) 177 | grouped = outdata.groupby(lab) 178 | for jj,wl in enumerate(wlnames): 179 | dta = grouped[wl].mean() 180 | dta = (dta - dta.min())/(dta.max() - dta.min()) + 0.01 181 | if lab in xlabs[0:2]: 182 | dta.plot(ax=axs[jj]) 183 | else: 184 | dta.plot.bar(ax=axs[jj]) 185 | axs[jj].set_ylabel('Normalized Irradiance',fontsize=fontsize) 186 | axs[jj].text(0.01, 0.99, wl, size=16, color='black', ha='left', va='top', transform=axs[jj].transAxes) 187 | PlotStyle(axs[jj]) 188 | 189 | outdata = outdata.set_index('date') 190 | outdata = outdata[wlnames] 191 | 192 | outdata.to_csv('/media/tavo/storage/sunspots/solarinterpolated.csv') 193 | -------------------------------------------------------------------------------- /Farming/data/readme.md: -------------------------------------------------------------------------------- 1 | Contains data :) 2 | -------------------------------------------------------------------------------- /Generative/curveswrapper.py: -------------------------------------------------------------------------------- 1 | """ 2 | MIT License 3 | Copyright (c) 2020 Octavio Gonzalez-Lugo 4 | Permission is hereby granted, free of charge, to any person obtaining a copy 5 | of this software and associated documentation files (the "Software"), to deal 6 | in the Software without restriction, including without limitation the rights 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the Software is 9 | furnished to do so, subject to the following conditions: 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 13 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 14 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 15 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 16 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 17 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 18 | SOFTWARE. 19 | @author: Octavio Gonzalez-Lugo 20 | """ 21 | 22 | ############################################################################### 23 | # Wrapper Functions 24 | ############################################################################### 25 | 26 | def Wrapper01(x,y): 27 | return x**2+np.pi*np.sin(y) 28 | 29 | def Wrapper02(x,y): 30 | return y+np.pi*np.cos(x**2) 31 | 32 | def Wrapper01(x,y): 33 | return x+np.abs(np.pi*np.sin(y)) 34 | 35 | def Wrapper02(x,y): 36 | return y+np.abs(np.pi*np.cos(x)) 37 | 38 | def Wrapper01(x,y): 39 | return x**2+y**2 40 | 41 | def Wrapper02(x,y): 42 | return np.cos(x)+np.sin(y) 43 | 44 | def Wrapper01(x,y): 45 | return x**2+y**2 46 | 47 | def Wrapper02(x,y): 48 | return np.sin((x-y))+y 49 | 50 | def Wrapper01(x,y): 51 | return np.sin(x)+np.cos(y)+x 52 | 53 | def Wrapper02(x,y): 54 | return np.sin((x-y))+y 55 | 56 | 57 | def Wrapper01(x,y): 58 | return np.cos(x)-y 59 | 60 | def Wrapper02(x,y): 61 | return np.sin(y)-x 62 | 63 | def Wrapper01(x,y): 64 | return x+np.cos(x-y)*y 65 | 66 | def Wrapper02(x,y): 67 | return y+np.sin(y-x)*x 68 | 69 | 70 | def Wrapper01(x,y): 71 | return x-np.cos(x/y) 72 | 73 | def Wrapper02(x,y): 74 | return y+np.sin(y*x) 75 | 76 | def Wrapper01(x,y): 77 | return np.sin(x-y)+x 78 | 79 | def Wrapper02(x,y): 80 | return np.sin(y+x)+y 81 | 82 | def Wrapper01(x,y): 83 | return np.sin(x-y)+((-1)**np.sign(np.sin(x-y)))*x 84 | 85 | def Wrapper02(x,y): 86 | return np.sin(y+x)+y 87 | 88 | def Wrapper01(x,y): 89 | return np.sin(x)*np.cos(y) 90 | 91 | def Wrapper02(x,y): 92 | return np.sin(y-x) 93 | 94 | def Wrapper01(x,y): 95 | return np.sin(x)+np.cos(y) 96 | 97 | def Wrapper02(x,y): 98 | return np.sin(y)*np.cos(x) 99 | 100 | def Wrapper01(x,y): 101 | return x+x*np.sin(x+y) 102 | 103 | def Wrapper02(x,y): 104 | return np.sin(y-x)*x 105 | 106 | def Wrapper01(x,y): 107 | return x+x*np.sin(x+y) 108 | 109 | def Wrapper02(x,y): 110 | return np.sin(y)*x 111 | 112 | def Wrapper01(x,y): 113 | return x+x*np.sin(x+y) 114 | 115 | def Wrapper02(x,y): 116 | return np.cos(y)+y*x 117 | 118 | def Wrapper01(x,y): 119 | return x+x*np.sin(x+y) 120 | 121 | def Wrapper02(x,y): 122 | return np.cos(y)+x 123 | 124 | def Wrapper01(x,y): 125 | return np.sin(y)*np.sin(x) 126 | 127 | def Wrapper02(x,y): 128 | return x*np.cos(y) 129 | 130 | def Wrapper01(x,y): 131 | return x+np.sin(y)*np.sin(x) 132 | 133 | def Wrapper02(x,y): 134 | return y+np.cos(x)*np.cos(y) 135 | -------------------------------------------------------------------------------- /Generative/fractalsanimation.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | MIT License 6 | Copyright (c) 2021 Octavio Gonzalez-Lugo 7 | 8 | Permission is hereby granted, free of charge, to any person obtaining a copy 9 | of this software and associated documentation files (the "Software"), to deal 10 | in the Software without restriction, including without limitation the rights 11 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | copies of the Software, and to permit persons to whom the Software is 13 | furnished to do so, subject to the following conditions: 14 | The above copyright notice and this permission notice shall be included in all 15 | copies or substantial portions of the Software. 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | SOFTWARE. 23 | @author: Octavio Gonzalez-Lugo 24 | 25 | """ 26 | 27 | ############################################################################### 28 | # Plotting functions 29 | ############################################################################### 30 | 31 | import numpy as np 32 | import matplotlib.pyplot as plt 33 | 34 | ############################################################################### 35 | # Plotting functions 36 | ############################################################################### 37 | 38 | GlobalDirectory=r"/home/tavoglc/localBuffer/" 39 | 40 | ############################################################################### 41 | # Plotting functions 42 | ############################################################################### 43 | 44 | def ImageStyle(Axes): 45 | """ 46 | Parameters 47 | ---------- 48 | Axes : Matplotlib axes object 49 | Applies a general style to the matplotlib object 50 | 51 | Returns 52 | ------- 53 | None. 54 | """ 55 | Axes.spines['top'].set_visible(False) 56 | Axes.spines['bottom'].set_visible(False) 57 | Axes.spines['left'].set_visible(False) 58 | Axes.spines['right'].set_visible(False) 59 | Axes.set_xticks([]) 60 | Axes.set_yticks([]) 61 | 62 | ############################################################################### 63 | # Plotting functions 64 | ############################################################################### 65 | 66 | def MakeMaldebrontFractal(gridSize,maxIter): 67 | ''' 68 | Parameters 69 | ---------- 70 | gridSize : int 71 | number of divisions on the grid. 72 | maxIter : int 73 | max number of iterations taken to determine if a given point belogs or 74 | not to the Maldebront set. 75 | 76 | Returns 77 | ------- 78 | Ns : 2D array 79 | relative colouring of the fractal. 80 | 81 | ''' 82 | 83 | M=np.zeros((gridSize,gridSize)) 84 | Ns=np.zeros((gridSize,gridSize)) 85 | xvals=np.linspace(0.25,.5,gridSize) 86 | yvals=np.linspace(0.25,.5,gridSize) 87 | 88 | for u,x in enumerate(xvals): 89 | for v,y in enumerate(yvals): 90 | z=0 91 | c=complex(x,y) 92 | for i in range(maxIter): 93 | z=z*z+c 94 | if abs(z)>2: 95 | M[u,v]=1 96 | Ns[u,v]=i+1-np.log(np.log2(abs(z))) 97 | break 98 | 99 | return Ns 100 | 101 | 102 | def MakeJuliaSet(gridSize,maxIter): 103 | ''' 104 | Parameters 105 | ---------- 106 | gridSize : int 107 | number of divisions on the grid. 108 | maxIter : int 109 | max number of iterations taken to determine if a given point belogs or 110 | not to the Julia set. 111 | 112 | Returns 113 | ------- 114 | Ns : 2D array 115 | relative colouring of the fractal. 116 | 117 | ''' 118 | 119 | c=complex(-0.1,0.65) 120 | Julia=np.zeros((gridSize,gridSize)) 121 | Shades=np.zeros((gridSize,gridSize)) 122 | xvals=np.linspace(-1.5,1.5,gridSize) 123 | yvals=np.linspace(-1.5,1.5,gridSize) 124 | 125 | for k,x in enumerate(xvals): 126 | for j,y in enumerate(yvals): 127 | z=complex(x,y) 128 | for i in range(maxIter): 129 | z=z**2+c 130 | if abs(z)>(10): 131 | Julia[k,j]=1 132 | break 133 | shade=1-np.sqrt(i/maxIter) 134 | Shades[k,j]=shade 135 | 136 | return Shades 137 | 138 | ############################################################################### 139 | # Plotting functions 140 | ############################################################################### 141 | 142 | for k in range(240): 143 | 144 | Ns=MakeJuliaSet(2000,k+1) 145 | imdir=GlobalDirectory+"fig"+str(k)+".png" 146 | plt.figure(figsize=(10,10)) 147 | plt.imshow(Ns,cmap="gray") 148 | ax=plt.gca() 149 | ImageStyle(ax) 150 | plt.savefig(imdir) 151 | plt.close() 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Octavio Gonzalez-Lugo 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Life Sciences/AutoRiging.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | """ 4 | 5 | MIT License 6 | 7 | Copyright (c) 2019 Octavio Gonzalez-Lugo 8 | 9 | Permission is hereby granted, free of charge, to any person obtaining a copy 10 | of this software and associated documentation files (the "Software"), to deal 11 | in the Software without restriction, including without limitation the rights 12 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | copies of the Software, and to permit persons to whom the Software is 14 | furnished to do so, subject to the following conditions: 15 | The above copyright notice and this permission notice shall be included in all 16 | copies or substantial portions of the Software. 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | SOFTWARE. 24 | 25 | @author: Octavio Gonzalez-Lugo 26 | 27 | 28 | """ 29 | 30 | ###################################################################### 31 | # Loading the packages 32 | ###################################################################### 33 | 34 | import bpy 35 | from bpy import context 36 | 37 | ###################################################################### 38 | # Loading the data 39 | ###################################################################### 40 | 41 | Dir='pdbFile Location' 42 | 43 | ###################################################################### 44 | # Functions 45 | ###################################################################### 46 | 47 | def GetPDBFile(pdbFileDir): 48 | 49 | """ 50 | Opens a pdb file and returns each line in the file as text in a list 51 | 52 | pdbFileDir : Location of the file 53 | """ 54 | 55 | cDir = pdbFileDir 56 | 57 | with open(cDir) as file: 58 | 59 | Data=file.read().lower() 60 | Lines=Data.splitlines() 61 | 62 | return Lines 63 | 64 | def GetAlphaAtomCoordinates(pdbData): 65 | 66 | """ 67 | Returns a list with the coordinates of the alpha carbons of a protein 68 | 69 | pdbData : Output of the function GetPDBLines 70 | """ 71 | 72 | nData=len(pdbData) 73 | container=[] 74 | 75 | for k in range(nData): 76 | splitLine=pdbData[k].split() 77 | 78 | if splitLine[0]=='atom' and splitLine[2]=='ca': #Selects the alpha carbons in a protein 79 | coordinates=splitLine[6:9] 80 | container.append([float(val) for val in coordinates]) 81 | 82 | #Center of mas calculation to center the protein to (0,0,0) 83 | nAlpha=len(container) 84 | mX=sum([val[0] for val in container])/nAlpha 85 | mY=sum([val[1] for val in container])/nAlpha 86 | mZ=sum([val[2] for val in container])/nAlpha 87 | 88 | center=[mX,mY,mZ] 89 | centerCoordinates=[] 90 | 91 | #Centering the protein. 92 | for coord in container: 93 | 94 | centered=[a-b for a,b in zip(coord,center)] 95 | centerCoordinates.append(centered) 96 | 97 | return centerCoordinates 98 | 99 | pdbData=GetPDBFile(Dir) 100 | AlphaCarbonCoordinates=GetAlphaAtomCoordinates(pdbData) 101 | 102 | ###################################################################### 103 | # Rigging the protein. 104 | ###################################################################### 105 | 106 | #Number of alpha carbons in the protein 107 | nAlphas=len(AlphaCarbonCoordinates) 108 | 109 | #Adding the first bone 110 | tailLocation=tuple(AlphaCarbonCoordinates[0]) 111 | bpy.ops.object.armature_add(enter_editmode=True,location=tailLocation) 112 | 113 | #Extruding the remaining bones, one for each alpha carbon 114 | for k in range(1,3): 115 | headLocation=tuple([a-b for a,b in zip(AlphaCarbonCoordinates[k],AlphaCarbonCoordinates[k-1])]) #Coordinates correction 116 | bpy.ops.armature.extrude_move(TRANSFORM_OT_translate={"value":headLocation}) 117 | -------------------------------------------------------------------------------- /Life Sciences/MiningIntAct.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | 4 | @author: TavoGLC 5 | From: 6 | 7 | -Mining biological databases: IntAct- 8 | 9 | """ 10 | #################################################################### 11 | # Importing packages and libraries 12 | #################################################################### 13 | 14 | import re 15 | import csv 16 | import time 17 | import numpy as np 18 | 19 | from bs4 import BeautifulSoup 20 | from selenium import webdriver 21 | 22 | #################################################################### 23 | # Data Directory 24 | #################################################################### 25 | 26 | GlobalDir='Global data directory' 27 | DataDir=GlobalDir+'\\'+'Data' 28 | 29 | TargetListDir=GlobalDir+'\\'+'TargetList.csv' 30 | 31 | #################################################################### 32 | # Data saving functions 33 | #################################################################### 34 | 35 | #Generates a directory to save a file, name will be the Uniprot identifier 36 | def GenCSVDir(ParentDirectory,Name): 37 | 38 | return ParentDirectory+'\\'+Name+'.csv' 39 | 40 | #Save the interactions of a unique protein in a csv file 41 | def SaveCSVFile(Data,FileDirectory): 42 | 43 | cData=Data 44 | cFile=FileDirectory 45 | 46 | with open(cFile,'w') as output: 47 | 48 | LocalWriter=csv.writer(output) 49 | LocalWriter.writerow(cData) 50 | 51 | #################################################################### 52 | # Importing packages and libraries 53 | #################################################################### 54 | 55 | #Makes an edit to merge the uniprot and EBI identifiers 56 | def EBIEdit(EbiID): 57 | 58 | cID=EbiID 59 | 60 | try: 61 | 62 | NewEBIID=cID[0:6]+'-'+cID[7:len(cID)] 63 | 64 | except IndexError: 65 | 66 | NewEBIID='none' 67 | 68 | return NewEBIID 69 | 70 | 71 | #Find the number of pages in the IntAct Table 72 | def FindNumberOfPages(ParsedHTML): 73 | 74 | cPage=ParsedHTML 75 | #Selection of the numeration in the header table 76 | cStrings=cPage.find_all('span','ui-paginator-page','ui-corner-all') 77 | 78 | try: 79 | 80 | #It will work for a table with 10 or less result pages 81 | cVal=int(str(cStrings[-1])[-8:-7]) 82 | 83 | if cVal==0: 84 | 85 | nPages=int(str(cStrings[-1])[-9:-7]) 86 | 87 | else: 88 | 89 | nPages=cVal 90 | 91 | except IndexError: 92 | 93 | 94 | nPages=0 95 | 96 | return nPages 97 | 98 | #Returns the interactions of a protein 99 | def GetDataTable(ParsedHTML,localTarget): 100 | 101 | cParsed=ParsedHTML 102 | #Selects the text entries in the results table 103 | cSelection=cParsed.select('.ui-dt-c') 104 | Container=[] 105 | 106 | for line in cSelection: 107 | 108 | #Takes the text inside the table 109 | Container.append(line.text.strip()) 110 | 111 | kLines=len(Container) 112 | IntContainer=[] 113 | 114 | EBIID='none' 115 | 116 | #Takes the text inside the table 117 | for line in Container: 118 | 119 | if re.match(localTarget+'(.*)',line): 120 | 121 | EBIID=line 122 | 123 | else: 124 | 125 | pass 126 | 127 | #Takes the text inside the table 128 | editID=EBIEdit(EBIID) 129 | 130 | #Takes the text inside the table 131 | if editID=='none': 132 | 133 | pass 134 | 135 | else: 136 | 137 | IntContainer.append(editID) 138 | 139 | for k in range(kLines): 140 | 141 | #Looks for the EBI identifier number 142 | if re.match('(.*)EBI-(.*)',Container[k]): 143 | 144 | #Selects only proteins expressed in humans 145 | if Container[k+1]=='human (9606)': 146 | 147 | IntContainer.append(Container[k]) 148 | 149 | else: 150 | 151 | pass 152 | 153 | else: 154 | 155 | pass 156 | 157 | return IntContainer 158 | 159 | #IntAct scraping function 160 | def MakeEntryCall(UniprotID): 161 | 162 | localTarget=UniprotID 163 | 164 | #Browser mnipulation 165 | localDriver=webdriver.Chrome(executable_path='ChromeDriverPath') 166 | localDriver.get("https://www.ebi.ac.uk/intact/") 167 | time.sleep(5) 168 | 169 | localDriver.find_element_by_id('queryTxt').send_keys(localTarget) 170 | time.sleep(5) 171 | 172 | localDriver.find_element_by_id('quickSearchBtn').click() 173 | time.sleep(5) 174 | 175 | localDriver.find_element_by_link_text('Interactors').click() 176 | time.sleep(5) 177 | 178 | #Parsing results data 179 | CurrentParsed = BeautifulSoup(localDriver.page_source, 'html.parser') 180 | cPages=FindNumberOfPages(CurrentParsed) 181 | cData=GetDataTable(CurrentParsed,localTarget) 182 | 183 | if cPages==0: 184 | 185 | time.sleep(5) 186 | localDriver.close() 187 | 188 | else: 189 | 190 | #Loop over the number of results 191 | for k in range(cPages-1): 192 | 193 | # 194 | try: 195 | 196 | #Global xpath of the next element 197 | localDriver.find_element_by_xpath('/html/body/div[2]/div/div[2]/form[1]/div/div/div[2]/div[2]/div/div[1]/div[3]/table/thead/tr[1]/th/span[4]/span').click() 198 | LParsed=BeautifulSoup(localDriver.page_source, 'html.parser') 199 | lData=GetDataTable(LParsed,localTarget) 200 | cData.extend(lData) 201 | time.sleep(5) 202 | 203 | except Exception: 204 | 205 | break 206 | 207 | localDriver.close() 208 | 209 | return cData 210 | 211 | #################################################################### 212 | # Importing packages and libraries 213 | #################################################################### 214 | 215 | #Loading Uniprot identifiers 216 | targetList=np.genfromtxt(TargetListDir,dtype="|U6") 217 | 218 | #Looping through the identifiers adn saving the data 219 | for target in targetList: 220 | 221 | data=MakeEntryCall(target) 222 | cDir=GenCSVDir(DataDir,target) 223 | SaveCSVFile(data,cDir) 224 | 225 | -------------------------------------------------------------------------------- /Life Sciences/RotationAnimation.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | 6 | MIT License 7 | 8 | Copyright (c) 2019 Octavio Gonzalez-Lugo 9 | 10 | Permission is hereby granted, free of charge, to any person obtaining a copy 11 | of this software and associated documentation files (the "Software"), to deal 12 | in the Software without restriction, including without limitation the rights 13 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 14 | copies of the Software, and to permit persons to whom the Software is 15 | furnished to do so, subject to the following conditions: 16 | The above copyright notice and this permission notice shall be included in all 17 | copies or substantial portions of the Software. 18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 | SOFTWARE. 25 | 26 | @author: Octavio Gonzalez-Lugo 27 | 28 | 29 | """ 30 | 31 | ###################################################################### 32 | # Loading the packages 33 | ###################################################################### 34 | 35 | import bpy 36 | import random 37 | 38 | ###################################################################### 39 | # Functions 40 | ###################################################################### 41 | 42 | nalphas=254 43 | 44 | def MakeBoneNames(nRes): 45 | 46 | """ 47 | Creates a list with the names of all the bones to be animated. 48 | nRes : Number of Residues in the protein 49 | """ 50 | 51 | bonesLocal=[] 52 | 53 | for k in range(nRes-1): 54 | 55 | if k+1<10: 56 | nameLocal='Bone.00'+str(k+1) 57 | elif k+1>=10 and k+1<100: 58 | nameLocal='Bone.0'+str(k+1) 59 | else: 60 | nameLocal='Bone.'+str(k+1) 61 | 62 | bonesLocal.append(nameLocal) 63 | 64 | return bonesLocal 65 | 66 | ###################################################################### 67 | # Animating the protein. 68 | ###################################################################### 69 | 70 | boneNames=MakeBoneNames(nAlphas) 71 | rig = bpy.data.objects['Armature'] #Selects the current armature object 72 | 73 | #Iterates trough the frames in the animation 74 | for j in range(0,250,14): 75 | 76 | #Iterates trough all the bones in the model 77 | for k in range(len(boneNames)): 78 | 79 | cbone=boneNames[k] 80 | bone = rig.pose.bones[cbone] 81 | bone.rotation_axis_angle = (0,0, 0, 0) #Original Position 82 | bone.keyframe_insert('rotation_axis_angle', frame=j) 83 | bone.rotation_axis_angle = (0.00015,0, 0,random.random()/5) #Random Rotation 84 | bone.keyframe_insert('rotation_axis_angle', frame=j+7) 85 | bone.rotation_axis_angle = (0,0, 0, 0) #Return to original position 86 | bone.keyframe_insert('rotation_axis_angle', frame=j+14) 87 | bpy.context.object.pose.bones[cbone].rotation_mode = 'AXIS_ANGLE' 88 | -------------------------------------------------------------------------------- /Life Sciences/Uniprot AdvancedViz.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | 4 | @author: TavoGLC 5 | From: 6 | 7 | -Mining biological databases: UniProt- 8 | 9 | """ 10 | 11 | ############################################################################### 12 | # Libraries to use 13 | ############################################################################### 14 | 15 | import numpy as np 16 | import matplotlib.pyplot as plt 17 | 18 | from sklearn.cluster import DBSCAN 19 | from matplotlib.gridspec import GridSpec 20 | from scipy.spatial import distance as ds 21 | from sklearn.decomposition import PCA,KernelPCA,SparsePCA 22 | 23 | ############################################################################### 24 | # General Plot Functions 25 | ############################################################################### 26 | 27 | #Elimates the left and top lines and ticks in a matplotlib plot 28 | def PlotStyle(Axes,Title): 29 | 30 | Axes.spines['top'].set_visible(False) 31 | Axes.spines['right'].set_visible(False) 32 | Axes.spines['bottom'].set_visible(True) 33 | Axes.spines['left'].set_visible(True) 34 | Axes.xaxis.set_tick_params(labelsize=14) 35 | Axes.yaxis.set_tick_params(labelsize=14) 36 | Axes.set_title(Title) 37 | 38 | # 39 | def PanelPlot(Figure,Data): 40 | 41 | cFig=Figure 42 | gridSp=GridSpec(2,2) 43 | DS1,DS2,DS3=Data 44 | 45 | ax1=cFig.add_subplot(gridSp[0,0]) 46 | ax1.plot(DS1[:,0],DS1[:,1] ,'bo') 47 | PlotStyle(ax1,'Principal Component Analysis (PCA)') 48 | 49 | ax2=cFig.add_subplot(gridSp[0,1]) 50 | ax2.plot(DS2[:,0],DS2[:,1] ,'bo') 51 | PlotStyle(ax2,'Sparse PCA') 52 | 53 | ax3=cFig.add_subplot(gridSp[1,0]) 54 | ax3.plot(DS3[:,0],DS3[:,1] ,'bo') 55 | PlotStyle(ax3,'Kernel PCA') 56 | 57 | ############################################################################### 58 | # Libraries to use 59 | ############################################################################### 60 | 61 | ##Global Directory 62 | GlobalDir='Global data directory' 63 | 64 | #Data directory 65 | DataDir=GlobalDir+'\\'+'Data' 66 | 67 | #Localization Data 68 | LocDataFile=DataDir+'\\'+'LOCData.csv' 69 | 70 | #GO data 71 | GODataFile=DataDir+'\\'+'GOData.csv' 72 | 73 | ############################################################################### 74 | # Loading Data 75 | ############################################################################### 76 | 77 | #Loads the matrix generated in the previous post 78 | GOIndex=np.genfromtxt(GOIndexFile,delimiter=',') 79 | GOData=np.genfromtxt(GODataFile,delimiter=',') 80 | 81 | ############################################################################### 82 | # Dimensionality Reduction 83 | ############################################################################### 84 | 85 | #Performs 3 of the most common dimensionality reductions techniques 86 | def DimensionalityReduction(Data): 87 | 88 | cData=Data 89 | pca=PCA(n_components=2) 90 | kpca=KernelPCA(kernel='rbf',n_components=2) 91 | spca=SparsePCA(n_components=2) 92 | 93 | pData=pca.fit_transform(cData) 94 | sData=kpca.fit_transform(cData) 95 | kData=spca.fit_transform(cData) 96 | 97 | return pData,sData,kData 98 | 99 | #Dimentionali reduced data 100 | LocReduced=DimensionalityReduction(LocData) 101 | GOReduced=DimensionalityReduction(GOData) 102 | 103 | ############################################################################### 104 | # Visualization 105 | ############################################################################### 106 | 107 | #Plotting the location data 108 | fig=plt.figure(1,figsize=(12,12)) 109 | 110 | PanelPlot(fig,LocReduced) 111 | 112 | plt.suptitle('Location Data') 113 | 114 | #Ploting the GO data 115 | fig=plt.figure(2,figsize=(12,12)) 116 | 117 | PanelPlot(fig,GOReduced) 118 | 119 | plt.suptitle('Gene Ontology Data') 120 | 121 | ############################################################################### 122 | # Clustering Functions 123 | ############################################################################### 124 | 125 | #DBSCAN optimization 126 | def GetDBSCANParameters(Data): 127 | 128 | cData=Data 129 | nData=len(Data) 130 | 131 | #Estimation of the min number of samples 132 | MinSamples=int(np.log(nData)) 133 | 134 | Container=[] 135 | 136 | #Estimation of eps, calculates the distance between points and saves the minimmum distance 137 | #of a given pair 138 | for k in range(nData-1): 139 | 140 | cVal=cData[k] 141 | LoopContainer=[] 142 | 143 | for j in range(k+1,nData): 144 | 145 | LoopContainer.append(ds.euclidean(cVal,cData[j])) 146 | 147 | Container.append(np.min(LoopContainer)) 148 | 149 | #Calculates the histogram of the distances data, as the distances histogram is skewed towards 0 150 | #we only take the 3 first intervals to calculate eps 151 | Counts,Intervals=np.histogram(Container) 152 | EPS=np.mean(Intervals[0:3]) 153 | 154 | return MinSamples,EPS 155 | 156 | #Calculates the clusters and returns a dictionary of the cluster location 157 | def GetClusterDict(Data,EPS,MinSamples): 158 | 159 | cData=Data 160 | Clusters=DBSCAN(eps=EPS, min_samples=MinSamples).fit(cData) 161 | Labels=Clusters.labels_ 162 | 163 | k=0 164 | LocalDict={} 165 | 166 | for val in Labels: 167 | 168 | LocalDict[k]=str(val) 169 | k=k+1 170 | 171 | return LocalDict, len(set(Labels)) 172 | 173 | #Given a dictionary takes the data of a given cluster 174 | def ClusterData(Data,CurrentDictionary,ClusterNumber): 175 | 176 | cData=[list(Data[k]) for k in range(len(Data)) if CurrentDictionary[k]==ClusterNumber] 177 | 178 | return np.array(cData) 179 | 180 | #Plot of the different clusters in a dataset 181 | def ClusterPlot(Data,Axes,PlotTitle): 182 | 183 | cData=Data 184 | LocalSamp,LocalEPS=GetDBSCANParameters(cData) 185 | LDict,nClusters=GetClusterDict(cData,LocalEPS,LocalSamp) 186 | 187 | #Generates evenly spaced colors from the viridis colormap 188 | LocalColors=plt.cm.viridis(np.linspace(0, 1,nClusters),alpha=0.75) 189 | 190 | for k in range(nClusters): 191 | 192 | LocalData=ClusterData(cData,LDict,str(k)) 193 | 194 | #When a cluster have only one element the code will raise an index error, often that data point do not belong to any cluster 195 | #and DBSCAN will asign the -1 cluster number 196 | try: 197 | 198 | Axes.plot(LocalData[:,0],LocalData[:,1],'o',markerfacecolor=LocalColors[k],markeredgecolor=LocalColors[k],markersize=14,label='Cluster '+str(k)) 199 | 200 | except IndexError: 201 | 202 | pass 203 | 204 | PlotStyle(Axes,PlotTitle) 205 | 206 | #Creates a panel of the cluster plots 207 | def PanelClusterPlot(Figure,Data): 208 | 209 | cFig=Figure 210 | gridSp=GridSpec(2,2) 211 | DS1,DS2,DS3=Data 212 | 213 | ax1=cFig.add_subplot(gridSp[0,0]) 214 | ClusterPlot(DS1,ax1,'Principal Component Analysis (PCA)') 215 | 216 | ax2=cFig.add_subplot(gridSp[0,1]) 217 | ClusterPlot(DS2,ax2,'Sparse PCA') 218 | 219 | ax3=cFig.add_subplot(gridSp[1,0]) 220 | ClusterPlot(DS3,ax3,'Kernel PCA') 221 | 222 | ############################################################################### 223 | # Visualization 224 | ############################################################################### 225 | 226 | #Plotting the location data 227 | fig=plt.figure(3,figsize=(12,12)) 228 | 229 | PanelClusterPlot(fig,LocReduced) 230 | 231 | plt.suptitle('Location Data') 232 | 233 | #Plotting the location data 234 | fig=plt.figure(4,figsize=(12,12)) 235 | 236 | PanelClusterPlot(fig,GOReduced) 237 | 238 | plt.suptitle('GO Data') 239 | -------------------------------------------------------------------------------- /Modeling/CyclinWee1System.R: -------------------------------------------------------------------------------- 1 | #MIT License 2 | #Copyright (c) 2020 Octavio Gonzalez-Lugo 3 | 4 | #Permission is hereby granted, free of charge, to any person obtaining a copy 5 | #of this software and associated documentation files (the "Software"), to deal 6 | #in the Software without restriction, including without limitation the rights 7 | #to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | #copies of the Software, and to permit persons to whom the Software is 9 | #furnished to do so, subject to the following conditions: 10 | #The above copyright notice and this permission notice shall be included in all 11 | #copies or substantial portions of the Software. 12 | #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 13 | #IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 14 | #FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 15 | #AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 16 | #LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 17 | #OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 18 | #SOFTWARE. 19 | #@author: Octavio Gonzalez-Lugo 20 | 21 | ############################################################################### 22 | # Loading packages 23 | ############################################################################### 24 | 25 | library(deSolve) 26 | library(ggplot2) 27 | library(tidygraph) 28 | library(ggraph) 29 | 30 | ############################################################################### 31 | #Solver function 32 | ############################################################################### 33 | 34 | solveModel<- function(Model,InitialConditions,ModelParameters,ColumnNames){ 35 | #Solves numerically an ODE system model,returns a formated dataframe 36 | #Model -> function, Model to be solved 37 | #InitialConditions -> list, Initial conditions for the ODE system 38 | #ModelParameters -> list, Parameters of the ODE model 39 | #ColumnNames -> list, names of the columns for the dataframe 40 | #MinMax -> bool, controlls if a minmax normalization is applied to the data. 41 | times <- seq(0, 100, by = 0.1) 42 | out <- ode(InitialConditions,times,Model,ModelParameters,method="rk4") 43 | ModelData<-data.frame(out) 44 | colnames(ModelData)<-ColumnNames 45 | ModelData 46 | } 47 | 48 | ############################################################################### 49 | # Plot Functions 50 | ############################################################################### 51 | 52 | SignalingPlot<-function(inputData,PlotTitle){ 53 | #Returns a ggplot 54 | #inputData -> model data 55 | #ColumNames -> Names for the columns in the data 56 | #PlotTitle -> Title for the plot 57 | ModelData<-inputData 58 | 59 | graphContainer<-ggplot(data=ModelData,aes(x=Time,y=Cdc2,color="Cdc2"))+geom_line()+ 60 | geom_line(aes(y=Wee1,color="Wee1"))+ 61 | scale_color_manual(values=c("Cdc2"="black","Wee1"="red"))+ 62 | labs(title=PlotTitle,color=" ")+ 63 | theme(axis.title.y = element_blank(),axis.text.y = element_blank(),axis.ticks.y = element_blank(),axis.text.x = element_blank(),axis.ticks.x = element_blank()) 64 | show(graphContainer) 65 | } 66 | 67 | PhasePlanePlot<-function(inputData,PlotTitle){ 68 | #Returns a ggplot 69 | #inputData -> model data 70 | #ColumNames -> Names for the columns in the data 71 | #PlotTitle -> Title for the plot 72 | ModelData<-inputData 73 | 74 | graphContainer<-ggplot(data=ModelData,aes(x=Cdc2,y=Wee1,color="Cdc2"))+geom_point()+ 75 | scale_color_manual(values=c("Cdc2"="black"))+ 76 | labs(title=PlotTitle,color=" ")+ 77 | theme(axis.text.y = element_blank(),axis.ticks.y = element_blank(),axis.text.x = element_blank(),axis.ticks.x = element_blank()) 78 | show(graphContainer) 79 | } 80 | 81 | 82 | ############################################################################### 83 | #Model CyclinWee1System 84 | ############################################################################### 85 | 86 | CyclinWee1System <- function(t,Y,params){ 87 | 88 | #Simple organic matter decomposition model 89 | #t -> integration time value 90 | # 91 | #Y -> list Values for the function to be evaluated 92 | # Y[1] -> Cdc2 93 | # Y[2] -> Wee1 94 | # 95 | #params -> Parameters of the ODE system model 96 | # 97 | # 98 | 99 | with(as.list(c(Y,params)),{ 100 | 101 | dx1dt = a1*(1-Y[1]) - (b1*Y[1]*(v*Y[2])**g1)/(K1+(v*Y[2])**g1) 102 | dy1dt = a2*(1-Y[2]) - (b2*Y[2]*Y[1]**g2)/(K2+Y[1]**g1) 103 | 104 | list(c(dx1dt,dy1dt)) 105 | }) 106 | } 107 | 108 | params <- c(a1=1,b1=200,g1=4,K1=30,v=1.9,a2=1,b2=10,g2=4,K2=1) 109 | Y <- c(0.5,0.51) 110 | 111 | columnNames<-c("Time","Cdc2","Wee1") 112 | CyclinWee1SystemData<-solveModel(CyclinWee1System,Y,params,columnNames) 113 | SignalingPlot(CyclinWee1SystemData,"Signaling (High Hill B)") 114 | PhasePlanePlot(CyclinWee1SystemData,"Phase plane (High Hill B)") 115 | -------------------------------------------------------------------------------- /Modeling/InfectiousDisease.r: -------------------------------------------------------------------------------- 1 | """ 2 | Created on Mon Jul 16 23:21:01 2018 3 | 4 | MIT License 5 | Copyright (c) 2020 Octavio Gonzalez-Lugo 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | @author: Octavio Gonzalez-Lugo 23 | 24 | """ 25 | 26 | ############################################################################### 27 | # Loading packages 28 | ############################################################################### 29 | 30 | library(deSolve) 31 | library(ggplot2) 32 | 33 | ############################################################################### 34 | # General Functions 35 | ############################################################################### 36 | 37 | rectangleRule<-function(solution){ 38 | 0.01*sum(solution) 39 | } 40 | 41 | makeModelPlot<-function(modelData){ 42 | modelPlot<-ggplot(data=modelData,aes(x=Time,y=Suceptible,color="Suceptible"))+geom_line()+ 43 | geom_line(aes(y=Infected,color="Infected"))+ 44 | geom_line(aes(y=Recovered,color="Recovered"))+ 45 | geom_line(aes(y=Total,color="Total"))+ 46 | scale_color_manual(values=c("Suceptible"="black","Infected"="red","Recovered"="blue","Total"="green"))+ 47 | labs(color=" ")+ 48 | theme(axis.title.y = element_blank(),axis.text.y = element_blank(),axis.ticks.y = element_blank(),axis.text.x = element_blank(),axis.ticks.x = element_blank()) 49 | show(modelPlot) 50 | } 51 | 52 | ############################################################################### 53 | # SIR model 54 | ############################################################################### 55 | 56 | SIR <- function(t,Y,params){ 57 | with(as.list(c(Y,params)),{ 58 | dSdt=-be*Y[2]*Y[1] 59 | dIdt=be*Y[2]*Y[1]-ga*Y[2] 60 | dRdt=ga*Y[2] 61 | dNdt=0 62 | list(c(dSdt,dIdt,dRdt,dNdt)) 63 | }) 64 | } 65 | 66 | params <- c(be=0.05,ga=0.8) 67 | Y <- c(1000,1,0,1000) 68 | time <- seq(0, 4, by = 0.01) 69 | out <- ode(Y,time,SIR,params) 70 | 71 | SIRData<-data.frame(out) 72 | columnNames<-c("Time","Suceptible","Infected","Recovered","Total") 73 | colnames(SIRData)<-columnNames 74 | 75 | #makeModelPlot(SIRData) 76 | 77 | ############################################################################### 78 | # SIRS model 79 | ############################################################################### 80 | 81 | SIRS <- function(t,Y,params){ 82 | with(as.list(c(Y,params)),{ 83 | dSdt=-be*Y[2]*Y[1]+de*Y[3] 84 | dIdt=be*Y[2]*Y[1]-ga*Y[2] 85 | dRdt=ga*Y[2]-de*Y[3] 86 | dNdt=0 87 | list(c(dSdt,dIdt,dRdt,dNdt)) 88 | }) 89 | } 90 | 91 | params <- c(be=0.05,ga=0.85,de=0.425) 92 | Y <- c(1000,1,0,1000) 93 | times <- seq(0, 4, by = 0.01) 94 | out <- ode(Y,times,SIRS,params) 95 | 96 | SIRSData<-data.frame(out) 97 | columnNames<-c("Time","Suceptible","Infected","Recovered","Total") 98 | colnames(SIRSData)<-columnNames 99 | 100 | makeModelPlot(SIRSData) 101 | 102 | ############################################################################### 103 | # SIRD model 104 | ############################################################################### 105 | 106 | SIRD <- function(t,Y,params){ 107 | with(as.list(c(Y,params)),{ 108 | dSdt=-be*Y[2]*Y[1]+nu*Y[4]-mu*Y[1] 109 | dIdt=be*Y[2]*Y[1]-ga*Y[2]-mu*Y[2] 110 | dRdt=ga*Y[2]-mu*Y[3] 111 | dNdt=0 112 | list(c(dSdt,dIdt,dRdt,dNdt)) 113 | }) 114 | } 115 | 116 | params <- c(be=0.05,ga=0.85,mu=4,nu=4) 117 | Y <- c(1000,1,0,1000) 118 | times <- seq(0, 4, by = 0.01) 119 | out <- ode(Y,times,SIRD,params) 120 | 121 | SIRDData<-data.frame(out) 122 | columnNames<-c("Time","Suceptible","Infected","Recovered","Total") 123 | colnames(SIRDData)<-columnNames 124 | 125 | #makeModelPlot(SIRDData) 126 | 127 | SolveSIRD<-function(NuVal,MuVal){ 128 | 129 | paramsP <- c(be=0.05,ga=0.85,mu=MuVal,nu=NuVal) 130 | Y <- c(1000,1,0,1000) 131 | timesL <- seq(0, 3, by = 0.01) 132 | outL <- ode(Y,timesL,SIRD,paramsP) 133 | SIRDDataL<-data.frame(outL) 134 | columnNames<-c("Time","Suceptible","Infected","Recovered","Total") 135 | colnames(SIRDDataL)<-columnNames 136 | SIRDDataL$Infected 137 | } 138 | 139 | paramRange<- seq(4,0,by=-0.1) 140 | infectionDepletion<-rep(0,length(paramRange)) 141 | restriction<-rep(0,length(paramRange)) 142 | 143 | for (i in 1:length(paramRange)){ 144 | cSolution<-SolveSIRD(paramRange[i],paramRange[i]) 145 | infectionDepletion[i]<-rectangleRule(cSolution) 146 | restriction[i]<-(paramRange[1]-paramRange[i])/paramRange[1] 147 | } 148 | 149 | infectionDepletion<-(infectionDepletion[1]-infectionDepletion)/infectionDepletion[1] 150 | 151 | depletion<-data.frame("Reduction"=infectionDepletion,"Restriction"=restriction) 152 | 153 | Dplot<-ggplot(data=depletion,aes(x=Restriction,y=Reduction))+geom_line() 154 | #show(Dplot) 155 | 156 | ############################################################################### 157 | # SIRSV model 158 | ############################################################################### 159 | 160 | SIRSV <- function(t,Y,params){ 161 | with(as.list(c(Y,params)),{ 162 | dSdt=-be*Y[2]*Y[1]+(mu-sig)*Y[4]-mu*Y[1] 163 | dIdt=be*Y[2]*Y[1]-ga*Y[2]-mu*Y[2] 164 | dRdt=ga*Y[2]-mu*Y[3] 165 | dNdt=0 166 | list(c(dSdt,dIdt,dRdt,dNdt)) 167 | }) 168 | } 169 | 170 | params <- c(be=0.05,ga=0.85,mu=4,sig=0) 171 | Y <- c(1000,1,0,1000) 172 | times <- seq(0, 4, by = 0.01) 173 | out <- ode(Y,times,SIRSV,params) 174 | 175 | SIRVData<-data.frame(out) 176 | columnNames<-c("Time","Suceptible","Infected","Recovered","Total") 177 | colnames(SIRVData)<-columnNames 178 | 179 | #makeModelPlot(SIRVData) 180 | 181 | SolveSIRV<-function(sigma){ 182 | 183 | paramsL <- c(be=0.05,ga=0.85,mu=4,sig=sigma) 184 | YL <- c(1000,1,0,1000) 185 | timesL <- seq(0, 4, by = 0.01) 186 | outL <- ode(YL,timesL,SIRSV,paramsL) 187 | 188 | SIRVLData<-data.frame(outL) 189 | columnNames<-c("Time","Suceptible","Infected","Recovered","Total") 190 | colnames(SIRVLData)<-columnNames 191 | 192 | SIRVLData$Infected 193 | } 194 | 195 | paramRange<- seq(0,4,by=0.1) 196 | infectionDepletion<-rep(0,length(paramRange)) 197 | fraction<-rep(0,length(paramRange)) 198 | 199 | for (i in 1:length(paramRange)){ 200 | cSolution<-SolveSIRV(paramRange[i]) 201 | infectionDepletion[i]<-rectangleRule(cSolution) 202 | fraction[i]<-(4-paramRange[i])/4 203 | } 204 | 205 | infectionDepletion<-(infectionDepletion[1]-infectionDepletion)/infectionDepletion[1] 206 | 207 | vaccination<-data.frame("Infection"=infectionDepletion,"Vaccination"=fraction) 208 | 209 | Vplot<-ggplot(data=vaccination,aes(x=Vaccination,y=Infection))+geom_line() 210 | #show(Vplot) 211 | -------------------------------------------------------------------------------- /Modeling/InfectiousDiseases02.r: -------------------------------------------------------------------------------- 1 | 2 | #Created on Mon Jul 16 23:21:01 2018 3 | 4 | #MIT License 5 | #Copyright (c) 2020 Octavio Gonzalez-Lugo 6 | 7 | #Permission is hereby granted, free of charge, to any person obtaining a copy 8 | #of this software and associated documentation files (the "Software"), to deal 9 | #in the Software without restriction, including without limitation the rights 10 | #to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | #copies of the Software, and to permit persons to whom the Software is 12 | #furnished to do so, subject to the following conditions: 13 | #The above copyright notice and this permission notice shall be included in all 14 | #copies or substantial portions of the Software. 15 | #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | #IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | #FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | #AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | #LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | #OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | #SOFTWARE. 22 | #@author: Octavio Gonzalez-Lugo 23 | 24 | ############################################################################### 25 | # Loading packages 26 | ############################################################################### 27 | 28 | library(deSolve) 29 | library(ggplot2) 30 | 31 | ############################################################################### 32 | # General Functions 33 | ############################################################################### 34 | 35 | sigmoidalRate<-function(MaxValue,MaxPopulation,CurrentPopulation){ 36 | #Transforms a rate to a sigmoidal function 37 | #MaxValue -> max value to be returned 38 | #CurrentPopulation -> Current population, value to be evaluated in the sigmoidal function 39 | #MaxPopulation -> Normalization factor 40 | popRatio=10*(CurrentPopulation/MaxPopulation) 41 | MaxValue*(1/(1+exp(-popRatio+5))) 42 | } 43 | 44 | hardThreshold<-function(MaxValue,MaxPopulation,CurrentPopulation){ 45 | #Generate a picewise rate function 46 | #MaxValue -> max value to be returned 47 | #CurrentPopulation -> Current population 48 | #MaxPopulation -> Normalization factor 49 | popRatio=CurrentPopulation/MaxPopulation 50 | if(popRatio<0.2){ 51 | MaxValue 52 | } 53 | else{ 54 | MaxValue*0.25 55 | } 56 | } 57 | 58 | makeModelPlot<-function(modelData){ 59 | #Returns a ggplot object 60 | #modelData -> dataframe with the integration results of an epidemics model 61 | modelPlot<-ggplot(data=modelData,aes(x=Time,y=Susceptible,color="Susceptible"))+geom_line()+ 62 | geom_line(aes(y=Infected,color="Infected"))+ 63 | geom_line(aes(y=Recovered,color="Recovered"))+ 64 | geom_line(aes(y=Total,color="Total"))+ 65 | scale_color_manual(values=c("Susceptible"="black","Infected"="red","Recovered"="blue","Total"="green"))+ 66 | labs(color=" ")+ 67 | theme(axis.title.y = element_blank(),axis.text.y = element_blank(),axis.ticks.y = element_blank(),axis.text.x = element_blank(),axis.ticks.x = element_blank()) 68 | show(modelPlot) 69 | } 70 | 71 | solveModel<- function(Model,InitialConditions,ModelParameters){ 72 | #Solves numerically an ODE system model 73 | #Model -> function, Model to be solved 74 | #InitialConditions -> list, Initial conditions for the ODE system 75 | #ModelParameters -> list, Parameters of the ODE model 76 | times <- seq(0, 10, by = 0.001) 77 | out <- ode(InitialConditions,times,Model,ModelParameters) 78 | ModelData<-data.frame(out) 79 | columnNames<-c("Time","Susceptible","Infected","Recovered","Total") 80 | colnames(ModelData)<-columnNames 81 | ModelData 82 | } 83 | 84 | ############################################################################### 85 | # SIRSD model 86 | ############################################################################### 87 | 88 | SIRSD <- function(t,Y,params){ 89 | #Modified SIR model, takes into account loss of immunity and demographic factors 90 | #t -> integration time value 91 | #Y -> list Values for the function to be evaluated 92 | #params -> Parameters of the ODE system model 93 | with(as.list(c(Y,params)),{ 94 | dSdt=-be*Y[2]*Y[1]+de*Y[3]+nu*Y[4]-mu*Y[1] 95 | dIdt=be*Y[2]*Y[1]-ga*Y[2]-mu*Y[2] 96 | dRdt=ga*Y[2]-de*Y[3]-mu*Y[3] 97 | dNdt=0 98 | list(c(dSdt,dIdt,dRdt,dNdt)) 99 | }) 100 | } 101 | 102 | params <- c(be=0.05,ga=0.85,de=0.425,nu=4.0,mu=4.0) 103 | Y <- c(1000,1,0,1000) 104 | 105 | SIRSDData<-solveModel(SIRSD,Y,params) 106 | 107 | #makeModelPlot(SIRSDData) 108 | 109 | ############################################################################### 110 | # Infection rates 111 | ############################################################################### 112 | 113 | ExampleData <- seq(0, 10, by = 0.01) 114 | plotData=sigmoidalRate(0.8,10,ExampleData) 115 | 116 | sigmoidalRateDF<-data.frame(x=ExampleData,y=plotData) 117 | colnames(sigmoidalRateDF)<-c("Normalized_Population","Infection_Rate") 118 | modelPlot<-ggplot(data=sigmoidalRateDF,aes(x=Normalized_Population,y=Infection_Rate))+geom_line() 119 | #show(modelPlot) 120 | 121 | ############################################################################### 122 | # Non Linear infection rates 123 | ############################################################################### 124 | 125 | SIRSDNL <- function(t,Y,params){ 126 | #Modified SIR model, takes into account loss of immunity and demographic factors 127 | #Modification in the infection rate into a sigmoidal responce function 128 | #of the infected population 129 | #t -> integration time value 130 | #Y -> list Values for the function to be evaluated 131 | #params -> Parameters of the ODE system model 132 | with(as.list(c(Y,params)),{ 133 | dSdt=-sigmoidalRate(be,Y[4],Y[2])*Y[2]*Y[1]+de*Y[3]+nu*Y[4]-mu*Y[1] 134 | dIdt=sigmoidalRate(be,Y[4],Y[2])*Y[2]*Y[1]-ga*Y[2]-mu*Y[2] 135 | dRdt=ga*Y[2]-de*Y[3]-mu*Y[3] 136 | dNdt=0 137 | list(c(dSdt,dIdt,dRdt,dNdt)) 138 | }) 139 | } 140 | 141 | params <- c(be=0.8,ga=0.85,de=0.0,nu=0.0,mu=0.0) 142 | Y <- c(1000,1,0,1000) 143 | 144 | SIRSDNLData<-solveModel(SIRSDNL,Y,params) 145 | 146 | #makeModelPlot(SIRSDNLData) 147 | 148 | ############################################################################### 149 | # Resource management 150 | ############################################################################### 151 | 152 | SIRSDNLR <- function(t,Y,params){ 153 | #Modified SIR model, takes into account loss of immunity and demographic factors 154 | #Modification of the infection rate into a sigmoidal responce function 155 | #of the infected population 156 | #Modification of the recovery rate, hard threshold of resource avaliability 157 | #function of the infected population 158 | #t -> integration time value 159 | #Y -> list Values for the function to be evaluated 160 | #params -> Parameters of the ODE system model 161 | with(as.list(c(Y,params)),{ 162 | dSdt=-sigmoidalRate(be,Y[4],Y[2])*Y[2]*Y[1]+de*Y[3]+nu*Y[4]-mu*Y[1] 163 | dIdt=sigmoidalRate(be,Y[4],Y[2])*Y[2]*Y[1]-hardThreshold(ga,Y[4],Y[2])*Y[2]-mu*Y[2] 164 | dRdt=hardThreshold(ga,Y[4],Y[2])*Y[2]-de*Y[3]-mu*Y[3] 165 | dNdt=0 166 | list(c(dSdt,dIdt,dRdt,dNdt)) 167 | }) 168 | } 169 | 170 | params <- c(be=0.8,ga=0.85,de=0.0,nu=0.0,mu=0.0) 171 | Y <- c(1000,1,0,1000) 172 | 173 | SIRSDNLRData<-solveModel(SIRSDNLR,Y,params) 174 | 175 | makeModelPlot(SIRSDNLRData) 176 | -------------------------------------------------------------------------------- /Modeling/Windkessel.R: -------------------------------------------------------------------------------- 1 | #MIT License 2 | #Copyright (c) 2021 Octavio Gonzalez-Lugo 3 | 4 | #Permission is hereby granted, free of charge, to any person obtaining a copy 5 | #of this software and associated documentation files (the "Software"), to deal 6 | #in the Software without restriction, including without limitation the rights 7 | #to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | #copies of the Software, and to per mit persons to whom the Software is 9 | #furnished to do so, subject to the following conditions: 10 | #The above copyright notice and this permission notice shall be included in all 11 | #copies or substantial portions of the Software. 12 | #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 13 | #IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 14 | #FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 15 | #AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 16 | #LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 17 | #OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 18 | #SOFTWARE. 19 | #@author: Octavio Gonzalez-Lugo 20 | 21 | ############################################################################### 22 | # Loading packages 23 | ############################################################################### 24 | 25 | library(deSolve) 26 | library(ggplot2) 27 | 28 | 29 | ############################################################################### 30 | #Solver function 31 | ############################################################################### 32 | 33 | solveModel<- function(Model,InitialConditions,ModelParameters,ColumnNames){ 34 | #Solves numerically an ODE system model,returns a formated dataframe 35 | #Model -> function, Model to be solved 36 | #InitialConditions -> list, Initial conditions for the ODE system 37 | #ModelParameters -> list, Parameters of the ODE model 38 | #ColumnNames -> list, names of the columns for the dataframe 39 | #MinMax -> bool, controlls if a minmax normalization is applied to the data. 40 | times <- seq(0, 10, by = 0.001) 41 | out <- ode(InitialConditions,times,Model,ModelParameters,method="bdf") 42 | ModelData<-data.frame(out) 43 | colnames(ModelData)<-ColumnNames 44 | ModelData 45 | } 46 | 47 | ############################################################################### 48 | # Plot Functions 49 | ############################################################################### 50 | 51 | MakeWindkesselModelPlot<-function(inputData,PlotTitle){ 52 | #Returns a ggplot 53 | #inputData -> model data 54 | #ColumNames -> Names for the columns in the data 55 | #PlotTitle -> Title for the plot 56 | ModelData<-inputData 57 | 58 | graphContainer<-ggplot(data=ModelData,aes(x=Time,y=Pressure,color="Pressure"))+geom_line()+ 59 | scale_color_manual(values=c("Pressure"="black"))+ 60 | labs(title=PlotTitle,color=" ")+ 61 | theme(axis.title.y = element_blank(),axis.text.y = element_blank(),axis.ticks.y = element_blank(),axis.text.x = element_blank(),axis.ticks.x = element_blank()) 62 | show(graphContainer) 63 | } 64 | 65 | ############################################################################### 66 | # Plot Functions 67 | ############################################################################### 68 | 69 | timeScale <- function(t,Tc,Ts){ 70 | scaledTime <- t/Tc 71 | decimal <- scaledTime - floor(scaledTime) 72 | responce <- FALSE 73 | if (decimal <= (2/7)){ 74 | responce <- TRUE 75 | } 76 | responce 77 | } 78 | 79 | inputLoad<-function(t,Tc,Ts){ 80 | a <- t%%Tc 81 | i0 <- 400*sin(pi*(a/Ts)) 82 | output <- 0 83 | if (timeScale(t,Tc,Ts)){ 84 | output <- i0 85 | } 86 | output 87 | } 88 | 89 | inputLoadDer<-function(t,Tc,Ts){ 90 | a<-t%%Tc 91 | i0 <- 400*cos(pi*(a/Ts)) 92 | output<-0 93 | if (timeScale(t,Tc,Ts)){ 94 | output <- i0 95 | } 96 | output 97 | } 98 | 99 | inputLoad2Der<-function(t,Tc,Ts){ 100 | a<-t%%Tc 101 | i0 <- -400*sin(pi*(a/Ts)) 102 | output<-0 103 | if (timeScale(t,Tc,Ts)){ 104 | output <- i0 105 | } 106 | output 107 | } 108 | ############################################################################### 109 | #Volumetric Flow 110 | ############################################################################### 111 | 112 | times <- seq(0, 5, by = 0.01) 113 | flowt <- rep(0,length(times)) 114 | k <- 1 115 | 116 | for (val in times){ 117 | flowt[k]<-inputLoad(val,60/72,(2/5)*(60/72)) 118 | k <- k+1 119 | } 120 | 121 | FlowData<-Container<-matrix(0,length(times),2) 122 | FlowData[,1]<-times 123 | FlowData[,2]<-flowt 124 | FlowData<-data.frame(FlowData) 125 | colnames(FlowData)<-c("Time","Flow") 126 | 127 | graphContainer<-ggplot(data=FlowData,aes(x=Time,y=Flow,color="Flow"))+geom_line()+ 128 | scale_color_manual(values=c("Flow"="blue"))+ 129 | labs(title="Volumetric flow",color=" ")+ 130 | theme(axis.title.y = element_blank(),axis.text.y = element_blank(),axis.ticks.y = element_blank(),axis.text.x = element_blank(),axis.ticks.x = element_blank()) 131 | show(graphContainer) 132 | 133 | ############################################################################### 134 | #Models 135 | ############################################################################### 136 | 137 | WindkesselModel2Elements <- function(t,Y,params){ 138 | 139 | #Simple organic matter decomposition model 140 | #t -> integration time value 141 | # 142 | #Y -> list Values for the function to be evaluated 143 | # Y[1] -> Blood pressure 144 | # 145 | #params -> Parameters of the ODE system model 146 | # 147 | # 148 | 149 | with(as.list(c(Y,params)),{ 150 | 151 | dPdt=(1/C)*(inputLoad(t,Tc,Ts)-(Y[1]/R)) 152 | 153 | list(c(dPdt)) 154 | }) 155 | } 156 | 157 | params <- c(C=1.0666,R=0.95000,Tc=60/72,Ts=(2/5)*(60/72)) 158 | Y <- c(80) 159 | 160 | columnNames<-c("Time","Pressure") 161 | threeNodeData<-solveModel(WindkesselModel2Elements,Y,params,columnNames) 162 | MakeWindkesselModelPlot(threeNodeData,"Windkessel Model 2 Elements") 163 | 164 | ############################################################################### 165 | #Models 166 | ############################################################################### 167 | 168 | WindkesselModel3Elements <- function(t,Y,params){ 169 | 170 | #Simple organic matter decomposition model 171 | #t -> integration time value 172 | # 173 | #Y -> list Values for the function to be evaluated 174 | # Y[1] -> Blood preassure 175 | # 176 | #params -> Parameters of the ODE system model 177 | # 178 | # 179 | 180 | with(as.list(c(Y,params)),{ 181 | 182 | dPdt=(1/C)*((1+(R1/R2))*inputLoad(t,Tc,Ts)+(C*R1*inputLoadDer(t,Tc,Ts))-(Y[1]/R1)) 183 | 184 | list(c(dPdt)) 185 | }) 186 | } 187 | 188 | params <- c(C=1.0666,R1=0.9,R2=0.05,Tc=60/72,Ts=(2/5)*(60/72)) 189 | Y <- c(80) 190 | 191 | columnNames<-c("Time","Pressure") 192 | threeNodeData<-solveModel(WindkesselModel3Elements,Y,params,columnNames) 193 | MakeWindkesselModelPlot(threeNodeData,"Windkessel Model 3 Elements") 194 | 195 | ############################################################################### 196 | #Models 197 | ############################################################################### 198 | 199 | WindkesselModel4Elements <- function(t,Y,params){ 200 | 201 | #Simple organic matter decomposition model 202 | #t -> integration time value 203 | # 204 | #Y -> list Values for the function to be evaluated 205 | # Y[1] -> Blood preassure 206 | # 207 | #params -> Parameters of the ODE system model 208 | # 209 | # 210 | 211 | 212 | with(as.list(c(Y,params)),{ 213 | 214 | dPdt=(1/C)*((1+(R1/R2))*inputLoad(t,Tc,Ts)+((C*R1+L/R1)*inputLoadDer(t,Tc,Ts))+(C*L*inputLoad2Der(t,Tc,Ts))-(Y[1]/R1)) 215 | 216 | list(c(dPdt)) 217 | }) 218 | } 219 | 220 | params <- c(C=1.0666,R1=0.9000,R2=0.05,L=25,Tc=60/72,Ts=(2/5)*(60/72)) 221 | Y <- c(80) 222 | 223 | columnNames<-c("Time","Pressure") 224 | threeNodeData<-solveModel(WindkesselModel4Elements,Y,params,columnNames) 225 | MakeWindkesselModelPlot(threeNodeData,"Windkessel Model 4 Elements") 226 | 227 | -------------------------------------------------------------------------------- /Modeling/aggregates.jl: -------------------------------------------------------------------------------- 1 | 2 | ############################################################################### 3 | # Loading packages 4 | ############################################################################### 5 | 6 | using DifferentialEquations 7 | using Plots 8 | 9 | ############################################################################### 10 | # Loading packages 11 | ############################################################################### 12 | 13 | function Polymer01!(du,u,p,t) 14 | 15 | u1,u2 = u 16 | n,k1pn,k2pn = p 17 | 18 | du[1] = -k1pn*u1^(n+1) - k2pn*u1*u2 19 | du[2] = k1pn*u1^(n+1) 20 | 21 | nothing 22 | end 23 | 24 | u0s = [50,0] 25 | params = [1,1.4*10^-4,5*10^-3] 26 | 27 | prob = ODEProblem(Polymer01!,u0s,(0.0,100),params) 28 | sol = solve(prob) 29 | plot(sol,tspan=(0,100)) 30 | 31 | ############################################################################### 32 | # Loading packages 33 | ############################################################################### 34 | function Polymer02!(du,u,p,t) 35 | 36 | u1,u2 = u 37 | k1pn,k2pn = p 38 | 39 | du[1] = -k1pn*u1 - k2pn*u1*u2 40 | du[2] = k1pn*u1 + k2pn*u1*u2 41 | 42 | nothing 43 | end 44 | 45 | u0s = [50,0] 46 | params = [2.5*10^-3,3.7*10^-3] 47 | 48 | prob = ODEProblem(Polymer02!,u0s,(0.0,200),params) 49 | sol = solve(prob) 50 | plot(sol,tspan=(0,200)) 51 | 52 | ############################################################################### 53 | # Loading packages 54 | ############################################################################### 55 | 56 | function Polymer03!(du,u,p,t) 57 | 58 | u1,u2,u3 = u 59 | n,k1sg,k2sg,k3sg = p 60 | 61 | du[1] = -n*k1sg*u1^n - k3sg*u1*u3 62 | du[2] = k1sg*u1^n - 2*k2sg*u2 63 | du[3] = k2sg*u2 64 | 65 | nothing 66 | end 67 | 68 | u0s = [50,0,0] 69 | params = [1,1.4*10^-1,2.9*10^-3,1.3*10^-1] 70 | 71 | prob = ODEProblem(Polymer03!,u0s,(0.0,200),params) 72 | sol = solve(prob) 73 | plot(sol,tspan=(0,200)) 74 | -------------------------------------------------------------------------------- /Modeling/automata.r: -------------------------------------------------------------------------------- 1 | #MIT License 2 | #Copyright (c) 2020 Octavio Gonzalez-Lugo 3 | 4 | #Permission is hereby granted, free of charge, to any person obtaining a copy 5 | #of this software and associated documentation files (the "Software"), to deal 6 | #in the Software without restriction, including without limitation the rights 7 | #to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | #copies of the Software, and to permit persons to whom the Software is 9 | #furnished to do so, subject to the following conditions: 10 | #The above copyright notice and this permission notice shall be included in all 11 | #copies or substantial portions of the Software. 12 | #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 13 | #IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 14 | #FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 15 | #AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 16 | #LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 17 | #OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 18 | #SOFTWARE. 19 | #@author: Octavio Gonzalez-Lugo 20 | 21 | ############################################################################### 22 | # Automata definitions 23 | ############################################################################### 24 | 25 | rulesPattern=matrix(0,8,3) 26 | 27 | rulesPattern[1,]<-c(0,0,0) 28 | rulesPattern[2,]<-c(0,0,1) 29 | rulesPattern[3,]<-c(0,1,0) 30 | rulesPattern[4,]<-c(0,1,1) 31 | rulesPattern[5,]<-c(1,0,0) 32 | rulesPattern[6,]<-c(1,0,1) 33 | rulesPattern[7,]<-c(1,1,0) 34 | rulesPattern[8,]<-c(1,1,1) 35 | 36 | rulesValues=c(1,1,1,1,1,1,1,0) 37 | 38 | changeRuleValues<-function(Threshold){ 39 | 40 | finalRule<-rep(0,8) 41 | disc<-runif(8) 42 | for(k in 1:8){ 43 | if (disc[k]>=Threshold){ 44 | finalRule[k]<-1 45 | } 46 | } 47 | finalRule 48 | } 49 | 50 | ############################################################################### 51 | # Functions 52 | ############################################################################### 53 | 54 | randomInitialState<-function(size){ 55 | 56 | #creates a random list of 0,1 values of variabble size 57 | #size 58 | # -> int, size of the list 59 | 60 | container<-rep(0,size) 61 | disc<-runif(size) 62 | for (k in 1:size){ 63 | if (disc[k]>=0.5){ 64 | container[k]<-1 65 | } 66 | } 67 | container 68 | } 69 | 70 | inputManagement<-function(Fragment){ 71 | 72 | #Format the input and correct the edege by adding zeros to the end 73 | #Fragment 74 | # -> list, list of length 3 witn the current pattern 75 | 76 | fragmentSize=length(Fragment) 77 | for(k in 1:fragmentSize){ 78 | if (is.na(Fragment[k])){ 79 | Fragment[k]=0 80 | } 81 | } 82 | Fragment 83 | } 84 | 85 | splitRow<-function(row){ 86 | 87 | #split a list in fragments of splitSize size 88 | #row 89 | # -> list, list of variable size 90 | 91 | splitSize<-3 92 | rowSize<-length(row) 93 | splitedRow<-matrix(0,rowSize,splitSize) 94 | for(k in 1:rowSize){ 95 | end<-k+splitSize-1 96 | currentFragment<-row[k:end] 97 | splitedRow[k,]<-inputManagement(currentFragment) 98 | } 99 | splitedRow 100 | } 101 | 102 | findRuleValue<-function(value,RulesPatterns,RulesValues){ 103 | 104 | #find the correspondig value for the current value 105 | #value 106 | # -> list, current fragment to be evaluated 107 | #RulesPatterns 108 | # -> matrix, contains the different patterns 109 | #RulesValues 110 | # -> list, contains the output value for each pattern 111 | 112 | nRules<-length(RulesPatterns[,1]) 113 | out<--1 114 | for(k in 1:nRules){ 115 | if(isTRUE(all.equal(value,RulesPatterns[k,]))){ 116 | out<-RulesValues[k] 117 | break 118 | } 119 | } 120 | out 121 | } 122 | 123 | applyRule<-function(Row,RulesPatterns,RulesValues){ 124 | 125 | #Wrapper function to apply the automata rule to a row 126 | #Row 127 | # -> list, Row o be evaluated 128 | #Row 129 | # -> list, Row o be evaluated 130 | #RulesPatterns 131 | # -> matrix, contains the different patterns 132 | #RulesValues 133 | # -> list, contains the output value for each pattern 134 | 135 | splitedVals<-splitRow(Row) 136 | nvalues<-length(splitedVals[,1]) 137 | container<-rep(0,nvalues) 138 | for(k in 1:nvalues){ 139 | container[k]<-findRuleValue(splitedVals[k,],RulesPatterns,RulesValues) 140 | } 141 | container 142 | } 143 | 144 | iterateAutomata<-function(Steps,InitialState,RulesPatterns,RulesValues){ 145 | 146 | #Wrapper function to evaluate the automata 147 | #Steps 148 | # -> int, Iterations for the rules to be applied 149 | #InitialState 150 | # -> list, Row o be evaluated 151 | #RulesPatterns 152 | # -> matrix, contains the different patterns 153 | #RulesValues 154 | # -> list, contains the output value for each pattern 155 | 156 | automataContainer<-matrix(0,Steps+1,length(InitialState)) 157 | automataContainer[1,]<-InitialState 158 | for(k in 1:Steps){ 159 | currentState<-automataContainer[k,] 160 | nextState<-applyRule(currentState,RulesPatterns,RulesValues) 161 | automataContainer[k+1,]<-nextState 162 | } 163 | automataContainer 164 | } 165 | 166 | 167 | iterateDynamicRuleAutomata<-function(Steps,StepsToChange,InitialState,RulesPatterns){ 168 | 169 | #Wrapper function to evaluate the automata 170 | #Steps 171 | # -> int, Iterations for the rules to be applied 172 | #StepsToChange 173 | # -> int, Iterations for the rules to change 174 | #InitialState 175 | # -> list, Row o be evaluated 176 | #RulesPatterns 177 | # -> matrix, contains the different patterns 178 | #RulesValues 179 | # -> list, contains the output value for each pattern 180 | 181 | automataContainer<-matrix(0,Steps+1,length(InitialState)) 182 | automataContainer[1,]<-InitialState 183 | currentRules<-changeRuleValues(0.5) 184 | 185 | for(k in 1:Steps){ 186 | 187 | if(k%%StepsToChange==0){ 188 | currentRules<-changeRuleValues(0.5) 189 | } 190 | currentState<-automataContainer[k,] 191 | nextState<-applyRule(currentState,RulesPatterns,currentRules) 192 | automataContainer[k+1,]<-nextState 193 | } 194 | automataContainer 195 | } 196 | 197 | 198 | ############################################################################### 199 | # Automata Visualization 200 | ############################################################################### 201 | 202 | initialSt<-randomInitialState(300) 203 | #automataIt<-iterateAutomata(600,initialSt,rulesPattern,rulesValues) 204 | 205 | #image(automataIt,axes=FALSE,col=gray((0:32)/32)) 206 | 207 | automataItD<-iterateDynamicRuleAutomata(600,4,initialSt,rulesPattern) 208 | image(automataItD,axes=FALSE,col=gray((0:32)/32)) 209 | -------------------------------------------------------------------------------- /Modeling/circadian.jl: -------------------------------------------------------------------------------- 1 | """ 2 | MIT License 3 | Copyright (c) 2023 Octavio Gonzalez-Lugo 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | The above copyright notice and this permission notice shall be included in all 12 | copies or substantial portions of the Software. 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE. 20 | 21 | @author: Octavio Gonzalez-Lugo 22 | 23 | Model 24 | An efective model of endogenous clocks and external stimuli determining circadian rhythms 25 | Tim Breitenbach, Charlotte Helfrich‑Förster & Thomas Dandekar 26 | 27 | """ 28 | 29 | ############################################################################### 30 | # Loading packages 31 | ############################################################################### 32 | 33 | using DifferentialEquations 34 | using Plots 35 | 36 | ############################################################################### 37 | # Loading packages 38 | ############################################################################### 39 | 40 | function f(Vm,Km,V) 41 | Vm*(V/(V+Km)) 42 | end 43 | 44 | function f0(Vm,Km,KI,n,V) 45 | Vm*((Km^n)/((V^n)+(KI^n))) 46 | end 47 | 48 | function Goldbeter!(du,u,p,t) 49 | 50 | u1,u2,u3,u4,u5= u 51 | n,KI,Km,K1,K2,K3,K4,Kd,vs,vm,ks,V1,V2,V3,V4,k1,k2,vd = p 52 | 53 | du[1] = f0(vs,K1,KI,n,u5) - f(vm,Km,u1) 54 | du[2] = ks*u1 - f(V1,K1,u2) + f(V2,K2,u3) 55 | du[3] = f(V1,K1,u1) - f(V2,K2,u3) - f(V3,K3,u3) + f(V4,K4,u4) 56 | du[4] = f(V3,K3,u3) - f(V4,K4,u4) - k1*u4 + k2*u5 - f(vd,Kd,u4) 57 | du[5] = k1*u4 - k2*u5 58 | 59 | nothing 60 | end 61 | 62 | u0s = [0.5,0.5,0.5,0.6,1.5] 63 | params = [4,1,0.5,2,2,2,2,0.2,0.76,0.65,0.38,3.2,1.58,5,2.5,1.9,1.3,0.95] 64 | prob = ODEProblem(Goldbeter!,u0s,(0.0,240),params) 65 | sol = solve(prob) 66 | plot(sol,tspan=(0,240)) 67 | 68 | ############################################################################### 69 | # Loading packages 70 | ############################################################################### 71 | 72 | function day(t) 73 | cos(2*3.14*(t/24)+5)+1 74 | end 75 | 76 | function GoldbeterDay!(du,u,p,t) 77 | 78 | u1,u2,u3,u4,u5= u 79 | n,KI,Km,K1,K2,K3,K4,Kd,vs,vm,ks,V1,V2,V3,V4,k1,k2,vd = p 80 | 81 | du[1] = f0(vs,K1,KI,n,u5) - f(vm,Km,u1) + day(t)*exp(-u1) 82 | du[2] = ks*u1 - f(V1,K1,u2) + f(V2,K2,u3) - day(t)*u2 83 | du[3] = f(V1,K1,u1) - f(V2,K2,u3) - f(V3,K3,u3) + f(V4,K4,u4) 84 | du[4] = f(V3,K3,u3) - f(V4,K4,u4) - k1*u4 + k2*u5 - f(vd,Kd,u4) 85 | du[5] = k1*u4 - k2*u5 86 | 87 | nothing 88 | end 89 | 90 | u0s = [0.5,0.5,0.5,0.6,1.5] 91 | params = [4,1,0.5,2,2,2,2,0.2,0.76,0.65,0.38,3.2,1.58,5,2.5,1.9,1.3,0.95] 92 | prob = ODEProblem(GoldbeterDay!,u0s,(0.0,240),params) 93 | sol = solve(prob) 94 | plot(sol,tspan=(0,240)) 95 | 96 | ############################################################################### 97 | # Loading packages 98 | ############################################################################### 99 | 100 | function day_shift(t) 101 | 102 | cos(2*3.14*(t/(24+0.02*t)))+1 103 | end 104 | 105 | function GoldbeterDayShift!(du,u,p,t) 106 | 107 | u1,u2,u3,u4,u5= u 108 | n,KI,Km,K1,K2,K3,K4,Kd,vs,vm,ks,V1,V2,V3,V4,k1,k2,vd = p 109 | 110 | du[1] = f0(vs,K1,KI,n,u5) - f(vm,Km,u1) + day_shift(t)*exp(-u1) 111 | du[2] = ks*u1 - f(V1,K1,u2) + f(V2,K2,u3) - day_shift(t)*u2 112 | du[3] = f(V1,K1,u1) - f(V2,K2,u3) - f(V3,K3,u3) + f(V4,K4,u4) 113 | du[4] = f(V3,K3,u3) - f(V4,K4,u4) - k1*u4 + k2*u5 - f(vd,Kd,u4) 114 | du[5] = k1*u4 - k2*u5 115 | 116 | nothing 117 | end 118 | 119 | u0s = [0.5,0.5,0.5,0.6,1.5] 120 | params = [4,1,0.5,2,2,2,2,0.2,0.76,0.65,0.38,3.2,1.58,5,2.5,1.9,1.3,0.95] 121 | prob = ODEProblem(GoldbeterDayShift!,u0s,(0.0,2400),params) 122 | sol = solve(prob) 123 | plot(sol,tspan=(0,2400)) 124 | 125 | ############################################################################### 126 | # Loading packages 127 | ############################################################################### 128 | 129 | function day_shift(t) 130 | 131 | (t*0.01)*cos(2*3.14*(t/24))+1 132 | end 133 | 134 | function GoldbeterDayShift!(du,u,p,t) 135 | 136 | u1,u2,u3,u4,u5= u 137 | n,KI,Km,K1,K2,K3,K4,Kd,vs,vm,ks,V1,V2,V3,V4,k1,k2,vd = p 138 | 139 | du[1] = f0(vs,K1,KI,n,u5) - f(vm,Km,u1) + day_shift(t)*exp(-u1) 140 | du[2] = ks*u1 - f(V1,K1,u2) + f(V2,K2,u3) - day_shift(t)*u2 141 | du[3] = f(V1,K1,u1) - f(V2,K2,u3) - f(V3,K3,u3) + f(V4,K4,u4) 142 | du[4] = f(V3,K3,u3) - f(V4,K4,u4) - k1*u4 + k2*u5 - f(vd,Kd,u4) 143 | du[5] = k1*u4 - k2*u5 144 | 145 | nothing 146 | end 147 | 148 | u0s = [0.5,0.5,0.5,0.6,1.5] 149 | params = [4,1,0.5,2,2,2,2,0.2,0.76,0.65,0.38,3.2,1.58,5,2.5,1.9,1.3,0.95] 150 | prob = ODEProblem(GoldbeterDayShift!,u0s,(0.0,2400),params) 151 | sol = solve(prob) 152 | plot(sol,tspan=(0,180)) 153 | 154 | ############################################################################### 155 | # Loading packages 156 | ############################################################################### 157 | 158 | function darklight(t) 159 | 160 | day = floor(t/24) 161 | hours = t-(day*24) 162 | if hours>6 && hours<18 163 | out = (1-cos(2*3.14*(t/24))) 164 | else 165 | out = 0.01 166 | end 167 | end 168 | 169 | function day_shift(t) 170 | dayoftheyearw = 1-sin(2*3.14*(t/(365*24))+(65*24)) 171 | out = dayoftheyearw*darklight(t) 172 | end 173 | 174 | function GoldbeterDayShift!(du,u,p,t) 175 | 176 | u1,u2,u3,u4,u5= u 177 | n,KI,Km,K1,K2,K3,K4,Kd,vs,vm,ks,V1,V2,V3,V4,k1,k2,vd = p 178 | 179 | du[1] = f0(vs,K1,KI,n,u5) - f(vm,Km,u1) + day_shift(t)*exp(-u1) 180 | du[2] = ks*u1 - f(V1,K1,u2) + f(V2,K2,u3) - day_shift(t)*u2 181 | du[3] = f(V1,K1,u1) - f(V2,K2,u3) - f(V3,K3,u3) + f(V4,K4,u4) 182 | du[4] = f(V3,K3,u3) - f(V4,K4,u4) - k1*u4 + k2*u5 - f(vd,Kd,u4) 183 | du[5] = k1*u4 - k2*u5 184 | 185 | nothing 186 | end 187 | 188 | u0s = [0.5,0.5,0.5,0.6,1.5] 189 | params = [4,1,0.5,2,2,2,2,0.2,0.76,0.65,0.38,3.2,1.58,5,2.5,1.9,1.3,0.95] 190 | prob = ODEProblem(GoldbeterDayShift!,u0s,(0.0,8760),params) 191 | sol = solve(prob) 192 | plot(sol,tspan=(0,24*360)) 193 | -------------------------------------------------------------------------------- /Modeling/epidemicstime.r: -------------------------------------------------------------------------------- 1 | 2 | #Created on Mon Jul 16 23:21:01 2018 3 | 4 | #MIT License 5 | #Copyright (c) 2020 Octavio Gonzalez-Lugo 6 | 7 | #Permission is hereby granted, free of charge, to any person obtaining a copy 8 | #of this software and associated documentation files (the "Software"), to deal 9 | #in the Software without restriction, including without limitation the rights 10 | #to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | #copies of the Software, and to permit persons to whom the Software is 12 | #furnished to do so, subject to the following conditions: 13 | #The above copyright notice and this permission notice shall be included in all 14 | #copies or substantial portions of the Software. 15 | #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | #IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | #FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | #AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | #LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | #OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | #SOFTWARE. 22 | #@author: Octavio Gonzalez-Lugo 23 | 24 | ############################################################################### 25 | # Loading packages 26 | ############################################################################### 27 | 28 | library(deSolve) 29 | library(ggplot2) 30 | 31 | ############################################################################### 32 | # General Functions 33 | ############################################################################### 34 | 35 | sigmoidalRate<-function(MaxValue,MaxPopulation,CurrentPopulation){ 36 | #Transforms a rate to a sigmoidal function 37 | #MaxValue -> max value to be returned 38 | #CurrentPopulation -> Current population, value to be evaluated in the sigmoidal function 39 | #MaxPopulation -> Normalization factor 40 | popRatio=10*(CurrentPopulation/MaxPopulation) 41 | MaxValue*(1/(1+exp(-popRatio+5))) 42 | } 43 | 44 | hardThreshold<-function(MaxValue,MaxPopulation,CurrentPopulation){ 45 | #Generate a picewise rate function 46 | #MaxValue -> max value to be returned 47 | #CurrentPopulation -> Current population 48 | #MaxPopulation -> Normalization factor 49 | popRatio=CurrentPopulation/MaxPopulation 50 | if(popRatio<0.25){ 51 | MaxValue 52 | } 53 | else{ 54 | MaxValue*0.25 55 | } 56 | } 57 | 58 | solveModel<- function(Model,InitialConditions,ModelParameters,ColumnNames){ 59 | #Solves numerically an ODE system model 60 | #Model -> function, Model to be solved 61 | #InitialConditions -> list, Initial conditions for the ODE system 62 | #ModelParameters -> list, Parameters of the ODE model 63 | times <- seq(0, 25, by = 0.01) 64 | out <- ode(InitialConditions,times,Model,ModelParameters) 65 | ModelData<-data.frame(out) 66 | colnames(ModelData)<-ColumnNames 67 | ModelData 68 | } 69 | 70 | ############################################################################### 71 | # Visualization functions 72 | ############################################################################### 73 | 74 | makeMergedCostPlot<-function(modelData,PlotTitle){ 75 | #Returns a ggplot object 76 | #modelData -> dataframe with the integration results of an epidemics model 77 | modelPlot<-ggplot(data=modelData,aes(x=Time,y=Susceptible,color="Susceptible"))+geom_line()+ 78 | geom_line(aes(y=Infected,color="Infected"))+ 79 | geom_line(aes(y=Recovered,color="Recovered"))+ 80 | geom_line(aes(y=Total,color="Total"))+ 81 | geom_line(aes(y=Jobs,color="Jobs"))+ 82 | geom_line(aes(y=Cost,color="Cost"))+ 83 | geom_line(aes(y=Diseases,color="Diseases"))+ 84 | geom_line(aes(y=Recovery,color="Recovery"))+ 85 | scale_color_manual(values=c("Susceptible"="black","Infected"="red","Recovered"="blue","Total"="green","Jobs"="orange","Cost"="brown","Diseases"="gray","Recovery"="cyan"))+ 86 | labs(title=PlotTitle,color=" ")+ 87 | theme(axis.title.y = element_blank(),axis.text.y = element_blank(),axis.ticks.y = element_blank(),axis.text.x = element_blank(),axis.ticks.x = element_blank()) 88 | show(modelPlot) 89 | } 90 | 91 | 92 | ############################################################################### 93 | # Merged model. 94 | ############################################################################### 95 | 96 | mergedCostModel <- function(t,Y,params){ 97 | #Modified SIR model, takes into account loss of immunity and demographic factors 98 | #Modification of the infection rate into a sigmoidal responce function 99 | #of the infected population 100 | #Modification of the recovery rate, hard threshold of resource avaliability 101 | #function of the infected population 102 | #t -> integration time value 103 | #Y -> list Values for the function to be evaluated 104 | #params -> Parameters of the ODE system model 105 | with(as.list(c(Y,params)),{ 106 | 107 | dSdt=-sigmoidalRate(be,Y[4],Y[2])*Y[2]*Y[1]+de*Y[3]+nu*Y[6]-mu*Y[1]-be*Y[1] 108 | dIdt=sigmoidalRate(be,Y[4],Y[2])*Y[2]*Y[1]-hardThreshold(ga,Y[4],Y[2]+Y[4])*Y[2]-mu*Y[2] 109 | dRdt=hardThreshold(ga,Y[4],Y[2]+Y[4])*Y[2]-de*Y[3]-mu*Y[3] 110 | 111 | dIEdt=(be/2)*Y[1]-hardThreshold(ga,Y[6],Y[2]+Y[4])*Y[4]-mu*Y[4] 112 | dREdt=hardThreshold(ga,Y[6],Y[2]+Y[4])*Y[4]-mor*Y[4]-mu*Y[5] 113 | 114 | dNdt=dSdt+dIdt+dRdt+dIEdt+dREdt 115 | 116 | dJdt=-u1*Y[7]+u2*0.5*(Y[6]-(Y[2]+Y[4])) 117 | dCdt=c1*Y[2]-c2*0.5*(Y[6]-(Y[2]+Y[4])) 118 | 119 | list(c(dSdt,dIdt,dRdt,dIEdt,dREdt,dNdt,dJdt,dCdt)) 120 | }) 121 | } 122 | 123 | params <- c(be=0.8,ga=0.8,de=0.4,nu=4,mu=4,u1=0.05,u2=0.075,c1=0.025,c2=0.1,mor=0.001) 124 | Y <- c(1000,1,0,50,0,1051,500,500) 125 | columnNames<-c("Time","Susceptible","Infected","Recovered","Diseases","Recovery","Total","Jobs","Cost") 126 | 127 | mergedData<-solveModel(mergedCostModel,Y,params,columnNames) 128 | makeMergedCostPlot(mergedData,"Current Scenario") 129 | 130 | ############################################################################### 131 | # Time Delay model. 132 | ############################################################################### 133 | 134 | timeDelayDefinition <- function(t,Y,params,tao){ 135 | #Modified SIR model, takes into account loss of immunity and demographic factors 136 | #Modification of the infection rate into a sigmoidal responce function 137 | #of the infected population 138 | #Modification of the recovery rate, hard threshold of resource avaliability 139 | #function of the infected population 140 | #t -> integration time value 141 | #Y -> list Values for the function to be evaluated 142 | #params -> Parameters of the ODE system model 143 | #tao -> Time delay to enforce social restriction 144 | with(as.list(c(Y,params)),{ 145 | 146 | if(t function, Model to be solved 35 | #InitialConditions -> list, Initial conditions for the ODE system 36 | #ModelParameters -> list, Parameters of the ODE model 37 | #ColumnNames -> list, names of the columns for the dataframe 38 | #MinMax -> bool, controlls if a minmax normalization is applied to the data. 39 | times <- seq(0, 5, by = 0.01) 40 | out <- ode(InitialConditions,times,Model,ModelParameters,method="rk4") 41 | if (MinMax){ 42 | dims<-dim(out) 43 | for (k in 2:dims[2]){ 44 | out[,k]<-MinMaxNormalization(out[,k]) 45 | } 46 | } 47 | ModelData<-data.frame(out) 48 | colnames(ModelData)<-ColumnNames 49 | ModelData 50 | } 51 | 52 | Monod<-function(S,Um,Ks){ 53 | rate<-Um*S/(S+Ks) 54 | rate 55 | } 56 | 57 | ############################################################################### 58 | # Plot Functions 59 | ############################################################################### 60 | 61 | MakeMonodFermentationPlot<-function(inputData,PlotTitle){ 62 | #Returns a ggplot 63 | #inputData -> model data 64 | #ColumNames -> Names for the columns in the data 65 | #PlotTitle -> Title for the plot 66 | ModelData<-inputData 67 | 68 | graphContainer<-ggplot(data=ModelData,aes(x=Time,y=Biomass,color="Biomass"))+geom_line()+ 69 | geom_line(aes(y=Substrate,color="Substrate"))+ 70 | geom_line(aes(y=Product,color="Product"))+ 71 | scale_color_manual(values=c("Biomass"="black","Substrate"="red","Product"="green"))+ 72 | labs(title=PlotTitle,color=" ")+ 73 | theme(axis.title.y = element_blank(),axis.text.y = element_blank(),axis.ticks.y = element_blank(),axis.text.x = element_blank(),axis.ticks.x = element_blank()) 74 | show(graphContainer) 75 | } 76 | 77 | ############################################################################### 78 | #Models 79 | ############################################################################### 80 | 81 | MonodFermentationModel <- function(t,Y,params){ 82 | 83 | #Simple organic matter decomposition model 84 | #t -> integration time value 85 | # 86 | #Y -> list Values for the function to be evaluated 87 | # Y[1] -> Foward and reverse primer content 88 | # Y[2] -> Sample DNA foward and reverse strand 89 | # Y[3] -> Complex sampleDNA-primer foward and reverse 90 | # 91 | #params -> Parameters of the ODE system model 92 | # Um -> Max growth rate 93 | # Ks -> Substrate constant 94 | # Yxs -> Substrate yield coefficient 95 | # k1 -> Product formation constant 96 | # k2 -> Product formation constant 97 | # 98 | 99 | with(as.list(c(Y,params)),{ 100 | 101 | dXdt=Monod(Y[2],Um,Ks)*Y[1] 102 | dSdt=-(Monod(Y[2],Um,Ks)*Y[1])/Yxs 103 | dPdt=(k1+k2*Monod(Y[2],Um,Ks))*Y[1] 104 | 105 | list(c(dXdt,dSdt,dPdt)) 106 | }) 107 | } 108 | 109 | params <- c(Um=1.5,Ks=0.751,Yxs=0.85,k1=0.01,k2=0.1) 110 | Y <- c(1,100,0.01) 111 | 112 | columnNames<-c("Time","Biomass","Substrate","Product") 113 | fermentationData<-solveModel(MonodFermentationModel,Y,params,columnNames,FALSE) 114 | MakeMonodFermentationPlot(fermentationData,"Fermentation High Substrate") 115 | 116 | params <- c(Um=1.5,Ks=0.751,Yxs=0.85,k1=0.01,k2=0.1) 117 | Y <- c(1,0.5,0.01) 118 | 119 | columnNames<-c("Time","Biomass","Substrate","Product") 120 | fermentationData<-solveModel(MonodFermentationModel,Y,params,columnNames,FALSE) 121 | MakeMonodFermentationPlot(fermentationData,"Fermentation Low Substrate") 122 | 123 | ############################################################################### 124 | #Models 125 | ############################################################################### 126 | 127 | MonodFermentationInhibitionModel <- function(t,Y,params){ 128 | 129 | #Simple organic matter decomposition model 130 | #t -> integration time value 131 | # 132 | #Y -> list Values for the function to be evaluated 133 | # Y[1] -> Foward and reverse primer content 134 | # Y[2] -> Sample DNA foward and reverse strand 135 | # Y[3] -> Complex sampleDNA-primer foward and reverse 136 | # 137 | #params -> Parameters of the ODE system model 138 | # Um -> Max growth rate 139 | # Ks -> Substrate constant 140 | # Yxs -> Substrate yield coefficient 141 | # k1 -> Product formation constant 142 | # k2 -> Product formation constant 143 | # cp -> Product inhibitory concentration 144 | 145 | with(as.list(c(Y,params)),{ 146 | 147 | dXdt=((1-Y[3]/cp))*Monod(Y[2],Um,Ks)*Y[1] 148 | dSdt=-(((1-Y[3]/cp))*Monod(Y[2],Um,Ks)*Y[1])/Yxs 149 | dPdt=(k1+k2*((1-Y[3]/cp))*Monod(Y[2],Um,Ks))*Y[1] 150 | 151 | list(c(dXdt,dSdt,dPdt)) 152 | }) 153 | } 154 | 155 | params <- c(Um=1.5,Ks=0.751,Yxs=0.85,k1=0.01,k2=0.1,cp=25) 156 | Y <- c(1,100,0.01) 157 | 158 | columnNames<-c("Time","Biomass","Substrate","Product") 159 | fermentationData<-solveModel(MonodFermentationInhibitionModel,Y,params,columnNames,FALSE) 160 | MakeMonodFermentationPlot(fermentationData,"High Inhibitory Concentration") 161 | 162 | params <- c(Um=1.5,Ks=0.751,Yxs=0.85,k1=0.01,k2=0.1,cp=5) 163 | Y <- c(1,100,0.01) 164 | 165 | columnNames<-c("Time","Biomass","Substrate","Product") 166 | fermentationData<-solveModel(MonodFermentationInhibitionModel,Y,params,columnNames,FALSE) 167 | MakeMonodFermentationPlot(fermentationData,"Low Inhibitory Concentration") 168 | 169 | 170 | -------------------------------------------------------------------------------- /Modeling/filter.r: -------------------------------------------------------------------------------- 1 | #MIT License 2 | #Copyright (c) 2020 Octavio Gonzalez-Lugo 3 | 4 | #Permission is hereby granted, free of charge, to any person obtaining a copy 5 | #of this software and associated documentation files (the "Software"), to deal 6 | #in the Software without restriction, including without limitation the rights 7 | #to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | #copies of the Software, and to permit persons to whom the Software is 9 | #furnished to do so, subject to the following conditions: 10 | #The above copyright notice and this permission notice shall be included in all 11 | #copies or substantial portions of the Software. 12 | #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 13 | #IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 14 | #FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 15 | #AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 16 | #LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 17 | #OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 18 | #SOFTWARE. 19 | #@author: Octavio Gonzalez-Lugo 20 | 21 | ############################################################################### 22 | # Loading packages 23 | ############################################################################### 24 | 25 | library(ggplot2) 26 | library(tidygraph) 27 | library(ggraph) 28 | library(pracma) 29 | 30 | ############################################################################### 31 | # Utility Functions 32 | ############################################################################### 33 | 34 | MinMaxNormalization<-function(ColumnData){ 35 | #Performs min-max normalization to the data. 36 | MinVal=min(ColumnData) 37 | MaxVal=max(ColumnData) 38 | DataRange=MaxVal-MinVal 39 | nData=length(ColumnData) 40 | container=rep(0,nData) 41 | 42 | for( k in 1:nData){ 43 | container[k]=(ColumnData[k]-MinVal)/DataRange 44 | } 45 | container 46 | } 47 | 48 | ############################################################################### 49 | # Plotting functions 50 | ############################################################################### 51 | 52 | MakePlateModelPlot<-function(inputData,ColumNames,PlotTitle){ 53 | #Returns a ggplot 54 | #inputData -> model data 55 | #ColumNames -> Names for the columns in the data 56 | #PlotTitle -> Title for the plot 57 | ModelData<-data.frame(inputData) 58 | colnames(ModelData)<-ColumnNames 59 | 60 | modelPlot<-ggplot(data=ModelData,aes(x=Time,y=TwoPlates,color="TwoPlates"))+geom_line()+ 61 | geom_line(aes(y=FivePlates,color="FivePlates"))+ 62 | geom_line(aes(y=TenPlates,color="TenPlates"))+ 63 | geom_line(aes(y=TwentyPlates,color="TwentyPlates"))+ 64 | geom_line(aes(y=TwentyFivePlates,color="TwentyFivePlates"))+ 65 | geom_line(aes(y=FiftyPlates,color="FiftyPlates"))+ 66 | scale_color_manual(values=c("TwoPlates"="black","FivePlates"="red","TenPlates"="blue","TwentyPlates"="green","TwentyFivePlates"="orange","FiftyPlates"="brown"))+ 67 | labs(title=PlotTitle,color=" ")+ 68 | theme(axis.title.y = element_blank(),axis.text.y = element_blank(),axis.ticks.y = element_blank(),axis.text.x = element_blank(),axis.ticks.x = element_blank()) 69 | show(modelPlot) 70 | } 71 | 72 | MakeEDMModelPlot<-function(inputData,ColumNames,PlotTitle){ 73 | #Returns a ggplot 74 | #inputData -> model data 75 | #ColumNames -> Names for the columns in the data 76 | #PlotTitle -> Title for the plot 77 | ModelData<-data.frame(inputData) 78 | colnames(ModelData)<-ColumnNames 79 | 80 | modelPlot<-ggplot(data=ModelData,aes(x=Time,y=Start,color="Start"))+geom_line()+ 81 | geom_line(aes(y=Quarter,color="Quarter"))+ 82 | geom_line(aes(y=Middle,color="Middle"))+ 83 | geom_line(aes(y=End,color="End"))+ 84 | scale_color_manual(values=c("Start"="black","Quarter"="red","Middle"="blue","End"="green"))+ 85 | labs(title=PlotTitle,color=" ")+ 86 | theme(axis.title.y = element_blank(),axis.text.y = element_blank(),axis.ticks.y = element_blank(),axis.text.x = element_blank(),axis.ticks.x = element_blank()) 87 | show(modelPlot) 88 | } 89 | 90 | ############################################################################### 91 | # Plate model 92 | ############################################################################### 93 | 94 | PlateFilter<-function(n,t,tau,tr){ 95 | # Analytical solution of the stirred tank in series model, taken from 96 | #Plate models in chromatography: Analysis and implications for scale up 97 | # A. Velayudhan and M.R Ladisch 98 | #Advances in biochemical engineering 1993 99 | #n -> number of theoretical plates 100 | #t -> time 101 | #tau -> retention time 102 | #tr -> injection time 103 | container<-rep(0,length(t)) 104 | for (k in 1:length(t)){ 105 | container[k]<-gammainc(n*t[k]/tr,n)[3]-gammainc(n*((t[k]-tau)/tr),n)[3] 106 | } 107 | container 108 | } 109 | 110 | GetPlateModelData<-function(ns,endTime,Tau,tr){ 111 | #n -> number of theoretical plates 112 | #t -> time 113 | #tau -> retention time 114 | #tr -> injection time 115 | 116 | time<-seq(0,endTime,by=0.01) 117 | filterPlateData<-matrix(nrow=length(time),ncol=length(ns)+1) 118 | filterPlateData[,1]<-time 119 | 120 | for (k in 1:length(ns)){ 121 | normData<-PlateFilter(ns[k],time,Tau,tr) 122 | #normData<-MinMaxNormalization(normData) 123 | filterPlateData[,k+1]<-normData 124 | } 125 | filterPlateData 126 | } 127 | 128 | PlateNumbers<-c(2,5,10,20,25,50) 129 | ColumnNames<-c("Time","TwoPlates","FivePlates","TenPlates","TwentyPlates","TwentyFivePlates","FiftyPlates") 130 | PlateData<-GetPlateModelData(PlateNumbers,25,1.2,10) 131 | MakePlateModelPlot(PlateData,ColumnNames,"Plate Model") 132 | 133 | ############################################################################### 134 | # EDM model 135 | ############################################################################### 136 | 137 | EDM<-function(t,x,L,Flow,ep,Dapp,K,Area){ 138 | #Analysis and Numerical Investigation of Dynamic Models for Liquid Chromatography 139 | # MS Math. Shumaila Javeed 140 | #t -> time 141 | #L -> length of the filter 142 | #Flow -> volumetric flow trough the filter 143 | #ep -> filter porosity 144 | #Dapp -> Apparent difussion 145 | #K -> equilibrium constant 146 | #Area -> cross section area of the filter 147 | 148 | Fr<-(1-ep)/ep 149 | u=Flow/(ep*Area) 150 | Pec<-L*u/Dapp 151 | a1<-sqrt(Pec/2) 152 | a2<-sqrt((L/u)*t*(1+K*Fr)) 153 | a3<-(L/u)*(1+K*Fr) 154 | A1<-erfc(a1*(a3*x-t)/a2) 155 | A2<-exp(x*Pec)*erfc(a1*((a3*x+t)/a2)) 156 | 157 | A3<-1/2*(A1+A2) 158 | A3 159 | } 160 | 161 | edmparts<-function(t,x,L,Flow,ep,Dapp,K,Area,tinj,cinit,cinj){ 162 | #Wrapper function to simulate the filter loading 163 | #parameters same as EDM 164 | #tinj -> time of feeding the filter 165 | #cint -> time of feeding the filter 166 | #cinj -> time of feeding the filter 167 | out<-0 168 | if(t model data 37 | #ColumNames -> Names for the columns in the data 38 | #PlotTitle -> Title for the plot 39 | ModelData<-inputData 40 | 41 | graphContainer<-ggplot(data=ModelData,aes(x=Time,y=mRNA,color="mRNA"))+geom_line()+ 42 | geom_line(aes(y=Bgalactosidasepermease,color="Bgalactosidasepermease"))+ 43 | geom_line(aes(y=Bgalactosidase,color="Bgalactosidase"))+ 44 | geom_line(aes(y=Lactose,color="Lactose"))+ 45 | scale_color_manual(values=c("mRNA"="black","Bgalactosidasepermease"="red","Bgalactosidase"="blue","Lactose"="green"))+ 46 | labs(title=PlotTitle,color=" ")+ 47 | theme(axis.title.y = element_blank(),axis.text.y = element_blank(),axis.ticks.y = element_blank(),axis.text.x = element_blank(),axis.ticks.x = element_blank()) 48 | show(graphContainer) 49 | } 50 | 51 | LacOperonBPlot<-function(inputData,PlotTitle){ 52 | #Returns a ggplot 53 | #inputData -> model data 54 | #ColumNames -> Names for the columns in the data 55 | #PlotTitle -> Title for the plot 56 | ModelData<-inputData 57 | 58 | graphContainer<-ggplot(data=ModelData,aes(x=Time,y=mRNA,color="mRNA"))+geom_line()+ 59 | geom_line(aes(y=Bgalactosidasepermease,color="Bgalactosidasepermease"))+ 60 | geom_line(aes(y=Bgalactosidase,color="Bgalactosidase"))+ 61 | geom_line(aes(y=Lactose,color="Lactose"))+ 62 | geom_line(aes(y=Allolactose,color="Allolactose"))+ 63 | scale_color_manual(values=c("mRNA"="black","Bgalactosidasepermease"="red","Bgalactosidase"="blue","Lactose"="green","Allolactose"="orange"))+ 64 | labs(title=PlotTitle,color=" ")+ 65 | theme(axis.title.y = element_blank(),axis.text.y = element_blank(),axis.ticks.y = element_blank(),axis.text.x = element_blank(),axis.ticks.x = element_blank()) 66 | show(graphContainer) 67 | } 68 | 69 | 70 | ############################################################################### 71 | #Solver function 72 | ############################################################################### 73 | 74 | solveModel<- function(Model,InitialConditions,ModelParameters,ColumnNames){ 75 | #Solves numerically an ODE system model,returns a formated dataframe 76 | #Model -> function, Model to be solved 77 | #InitialConditions -> list, Initial conditions for the ODE system 78 | #ModelParameters -> list, Parameters of the ODE model 79 | #ColumnNames -> list, names of the columns for the dataframe 80 | #MinMax -> bool, controlls if a minmax normalization is applied to the data. 81 | times <- seq(0, 10, by = 0.01) 82 | out <- ode(InitialConditions,times,Model,ModelParameters,method="rk4") 83 | ModelData<-data.frame(out) 84 | colnames(ModelData)<-ColumnNames 85 | ModelData 86 | } 87 | 88 | ############################################################################### 89 | #Models 90 | ############################################################################### 91 | 92 | LacOperonA <- function(t,Y,params){ 93 | 94 | #Simple organic matter decomposition model 95 | #t -> integration time value 96 | # 97 | #Y -> list Values for the function to be evaluated 98 | # Y[1] -> mRNa concentration 99 | # Y[2] -> Bgalactosidasepermease concentration 100 | # Y[3] -> Bgalactosidase 101 | # Y[4] -> Lactose 102 | # 103 | #params -> Parameters of the ODE system model 104 | # 105 | # 106 | 107 | with(as.list(c(Y,params)),{ 108 | 109 | dy1dt=(1+k*Y[4]**p)/(1+Y[4]**p) - b1*Y[1] 110 | dy2dt=Y[1]-b2*Y[2] 111 | dy3dt=r3*Y[1]-b3*Y[3] 112 | dy4dt=S*Y[2]-Y[3]*Y[4] 113 | 114 | list(c(dy1dt,dy2dt,dy3dt,dy4dt)) 115 | }) 116 | } 117 | 118 | params <- c(p=1.5,k=15,b1=2.5,b2=6,r3=0.2,b3=0.5,S=0.75) 119 | Y <- c(0,0.01,0,5) 120 | 121 | columnNames<-c("Time","mRNA","Bgalactosidasepermease","Bgalactosidase","Lactose") 122 | ModelAData<-solveModel(LacOperonA,Y,params,columnNames) 123 | LacOperonAPlot(ModelAData,"Simple Model") 124 | 125 | ############################################################################### 126 | #Models 127 | ############################################################################### 128 | 129 | LacOperonB <- function(t,Y,params){ 130 | 131 | #Simple organic matter decomposition model 132 | #t -> integration time value 133 | # 134 | #Y -> list Values for the function to be evaluated 135 | # Y[1] -> mRNA concentration 136 | # Y[2] -> Bgalactosidase concentration 137 | # Y[3] -> Lactose concentration 138 | # Y[4] -> Allolactose concentration 139 | # Y[5] -> Bgalactosidasepermease concentration 140 | # 141 | #params -> Parameters of the ODE system model 142 | # 143 | # 144 | 145 | with(as.list(c(Y,params)),{ 146 | 147 | dy1dt=am*(1+k1*(exp(mutm)*Y[4])**p)/(k+k1*(exp(mutm)*Y[4])**p) + G0 - gm*Y[1] 148 | dy2dt=ab*exp(-mutb)*Y[1] - gb*Y[2] 149 | dy3dt=al*(Y[5]*Le/(Kle+Le)) - bl1*(Y[5]*Y[3]/(Kl1+Y[3])) - bl2*(Y[2]*Y[3]/(Kl2+Y[3])) - gl*Y[3] 150 | dy4dt=aa*(Y[2]*Y[3]/(Kl+Y[3])) - ba*(Y[2]*Y[4]/(Ka+Y[4])) - ga*Y[4] 151 | dy5dt=ap*exp(-mutbp)*Y[1] - gp*Y[5] 152 | 153 | list(c(dy1dt,dy2dt,dy3dt,dy4dt,dy5dt)) 154 | }) 155 | } 156 | 157 | params <- c(am=2.5,k1=5,k=0.05,p=1.5,G0=0.001,gm=2.4,ab=0.3,gb=0.5,al=1.6,Le=0.1,Kle=1.4,bl1=0.4,Kl1=1,bl2=0.6,Kl2=0.66,gl=0.78,aa=0.7,Kl=0.6,ba=0.7,Ka=10.7,ga=0.5,ap=1,gp=1,mutm=0.1,mutb=0.1,mutbp=0.1) 158 | Y <- c(0,0.01,5,0.001,0.001) 159 | 160 | columnNames<-c("Time","mRNA","Bgalactosidase","Lactose","Allolactose","Bgalactosidasepermease") 161 | ModelBData<-solveModel(LacOperonB,Y,params,columnNames) 162 | LacOperonBPlot(ModelBData,"Low leakage") 163 | 164 | 165 | -------------------------------------------------------------------------------- /Modeling/mapk.R: -------------------------------------------------------------------------------- 1 | #MIT License 2 | #Copyright (c) 2020 Octavio Gonzalez-Lugo 3 | 4 | #Permission is hereby granted, free of charge, to any person obtaining a copy 5 | #of this software and associated documentation files (the "Software"), to deal 6 | #in the Software without restriction, including without limitation the rights 7 | #to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | #copies of the Software, and to permit persons to whom the Software is 9 | #furnished to do so, subject to the following conditions: 10 | #The above copyright notice and this permission notice shall be included in all 11 | #copies or substantial portions of the Software. 12 | #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 13 | #IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 14 | #FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 15 | #AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 16 | #LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 17 | #OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 18 | #SOFTWARE. 19 | #@author: Octavio Gonzalez-Lugo 20 | 21 | ############################################################################### 22 | # Loading packages 23 | ############################################################################### 24 | 25 | library(deSolve) 26 | library(ggplot2) 27 | library(tidygraph) 28 | library(ggraph) 29 | library(gridExtra) 30 | library(grid) 31 | library(lattice) 32 | 33 | ############################################################################### 34 | #Solver function 35 | ############################################################################### 36 | 37 | solveModel<- function(Model,InitialConditions,ModelParameters,ColumnNames){ 38 | #Solves numerically an ODE system model,returns a formated dataframe 39 | #Model -> function, Model to be solved 40 | #InitialConditions -> list, Initial conditions for the ODE system 41 | #ModelParameters -> list, Parameters of the ODE model 42 | #ColumnNames -> list, names of the columns for the dataframe 43 | #MinMax -> bool, controlls if a minmax normalization is applied to the data. 44 | times <- seq(0, 1000, by = 0.1) 45 | out <- ode(InitialConditions,times,Model,ModelParameters,method="daspk") 46 | ModelData<-data.frame(out) 47 | colnames(ModelData)<-ColumnNames 48 | ModelData 49 | } 50 | 51 | ############################################################################### 52 | # Plot Functions 53 | ############################################################################### 54 | columnNames<-c("Time","Mos","MEK","MEKPP","MAPK","MAPKPP") 55 | SiganllingPlot<-function(inputData,PlotTitle){ 56 | #Returns a ggplot 57 | #inputData -> model data 58 | #ColumNames -> Names for the columns in the data 59 | #PlotTitle -> Title for the plot 60 | ModelData<-inputData 61 | 62 | graph01<-ggplot(data=ModelData,aes(x=Time,y=Mos,color="Mos"))+geom_line()+ 63 | scale_color_manual(values=c("Mos"="black"))+ 64 | labs(title='Mos',color=" ")+ 65 | theme(legend.position="none",axis.title.y = element_blank(),axis.text.y = element_blank(),axis.ticks.y = element_blank(),axis.text.x = element_blank(),axis.ticks.x = element_blank()) 66 | 67 | graph02<-ggplot(data=ModelData,aes(x=Time,y=MEK,color="MEK"))+geom_line()+ 68 | scale_color_manual(values=c("MEK"="black"))+ 69 | labs(title='MEK',color=" ")+ 70 | theme(legend.position="none",axis.title.y = element_blank(),axis.text.y = element_blank(),axis.ticks.y = element_blank(),axis.text.x = element_blank(),axis.ticks.x = element_blank()) 71 | 72 | graph03<-ggplot(data=ModelData,aes(x=Time,y=MEKP,color="MEKP"))+geom_line()+ 73 | scale_color_manual(values=c("MEKP"="black"))+ 74 | labs(title='MEKP',color=" ")+ 75 | theme(legend.position="none",axis.title.y = element_blank(),axis.text.y = element_blank(),axis.ticks.y = element_blank(),axis.text.x = element_blank(),axis.ticks.x = element_blank()) 76 | 77 | graph04<-ggplot(data=ModelData,aes(x=Time,y=MEKPP,color="MEKPP"))+geom_line()+ 78 | scale_color_manual(values=c("MEKPP"="black"))+ 79 | labs(title='MEKPP',color=" ")+ 80 | theme(legend.position="none",axis.title.y = element_blank(),axis.text.y = element_blank(),axis.ticks.y = element_blank(),axis.text.x = element_blank(),axis.ticks.x = element_blank()) 81 | 82 | graph05<-ggplot(data=ModelData,aes(x=Time,y=MAPK,color="MAPK"))+geom_line()+ 83 | scale_color_manual(values=c("MAPK"="black"))+ 84 | labs(title='MAPK',color=" ")+ 85 | theme(legend.position="none",axis.title.y = element_blank(),axis.text.y = element_blank(),axis.ticks.y = element_blank(),axis.text.x = element_blank(),axis.ticks.x = element_blank()) 86 | 87 | graph06<-ggplot(data=ModelData,aes(x=Time,y=MAPKP,color="MAPKP"))+geom_line()+ 88 | scale_color_manual(values=c("MAPKP"="black"))+ 89 | labs(title='MAPKP',color=" ")+ 90 | theme(legend.position="none",axis.title.y = element_blank(),axis.text.y = element_blank(),axis.ticks.y = element_blank(),axis.text.x = element_blank(),axis.ticks.x = element_blank()) 91 | 92 | graph07<-ggplot(data=ModelData,aes(x=Time,y=MAPKPP,color="MAPKPP"))+geom_line()+ 93 | scale_color_manual(values=c("MAPKPP"="black"))+ 94 | labs(title='MAPKPP',color=" ")+ 95 | theme(legend.position="none",axis.title.y = element_blank(),axis.text.y = element_blank(),axis.ticks.y = element_blank(),axis.text.x = element_blank(),axis.ticks.x = element_blank()) 96 | 97 | gridarrange<-rbind(c(1,1,2,3,4),c(1,1,5,6,7)) 98 | graphContainer<-grid.arrange(graph01,graph02,graph03,graph04,graph05,graph06,graph07, layout_matrix=gridarrange) 99 | show(graphContainer) 100 | 101 | } 102 | 103 | ############################################################################### 104 | #Model Progesterone 105 | ############################################################################### 106 | 107 | MapkSystem <- function(t,Y,params){ 108 | 109 | #Simple organic matter decomposition model 110 | #t -> integration time value 111 | # 112 | #Y -> list Values for the function to be evaluated 113 | # Y[1],Y[2]-> Foward and reverse primer content 114 | # Y[3],Y[4]-> Sample DNA foward and reverse strand 115 | # Y[5],Y[6]-> Complex sampleDNA-primer foward and reverse 116 | # Y[7] -> mRNA and protein content of gene 2 117 | # Y[5] -> mRNA and protein content of gene 2 118 | # 119 | #params -> Parameters of the ODE system model 120 | # 121 | # 122 | 123 | with(as.list(c(Y,params)),{ 124 | 125 | dxdt = -(V2*Y[1])/(K2+Y[1]) + V1 + V0*Y[1]*v*Y[7] 126 | 127 | dy1dt = (V6*Y[3])/(K6+Y[3]) - (V3*Y[1]*Y[2])/(K3+Y[2]) 128 | dy2dt = (V3*Y[1]*Y[2])/(K3+Y[2]) + (V5*Y[4])/(K5+Y[4]) - (V4*Y[1]*Y[3])/(K4+Y[3]) + (V6*Y[3])/(K6+Y[3]) 129 | dy3dt = (V4*Y[1]*Y[3])/(K4+Y[3]) - (V5*Y[4])/(K5+Y[4]) 130 | 131 | dz1dt = (V10*Y[6])/(K10+Y[6]) - (V7*Y[4]*Y[5])/(K7+Y[5]) 132 | dz2dt = (V7*Y[4]*Y[5])/(K7+Y[5]) + (V9*Y[7])/(K9+Y[7]) - (V8*Y[4]*Y[6])/(K8+Y[6]) - (V10*Y[6])/(K10+Y[6]) 133 | dz3dt = (V8*Y[4]*Y[6])/(K8+Y[6]) - (V9*Y[7])/(K9+Y[7]) 134 | 135 | list(c(dxdt,dy1dt,dy2dt,dy3dt,dz1dt,dz2dt,dz3dt)) 136 | }) 137 | } 138 | 139 | params <- c(V2=1.2,K2=200,V0=0.0015,v=1.5,V1=0.000002,V6=5,K6=1200,V3=0.064,K3=1200,V4=0.064,K4=1200,V5=5,K5=1200,V10=5,K10=300,V7=0.06,K7=300,V8=0.06,K8=300,V9=5,K9=300) 140 | #Y <- c(2,1,0,0,10,0,0) 141 | #Y <- c(2,1,25,0,10,20,0) 142 | Y <- c(2,1,25,10,10,20,10) 143 | 144 | columnNames<-c("Time","Mos","MEK","MEKP","MEKPP","MAPK","MAPKP","MAPKPP") 145 | threeNodeData<-solveModel(MapkSystem,Y,params,columnNames) 146 | SiganllingPlot(threeNodeData,"Three Nodes") 147 | 148 | ############################################################################### 149 | #Models Progesterone control 150 | ############################################################################### 151 | -------------------------------------------------------------------------------- /Modeling/pcr.r: -------------------------------------------------------------------------------- 1 | #MIT License 2 | #Copyright (c) 2021 Octavio Gonzalez-Lugo 3 | 4 | #Permission is hereby granted, free of charge, to any person obtaining a copy 5 | #of this software and associated documentation files (the "Software"), to deal 6 | #in the Software without restriction, including without limitation the rights 7 | #to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | #copies of the Software, and to permit persons to whom the Software is 9 | #furnished to do so, subject to the following conditions: 10 | #The above copyright notice and this permission notice shall be included in all 11 | #copies or substantial portions of the Software. 12 | #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 13 | #IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 14 | #FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 15 | #AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 16 | #LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 17 | #OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 18 | #SOFTWARE. 19 | #@author: Octavio Gonzalez-Lugo 20 | 21 | ############################################################################### 22 | # Loading packages 23 | ############################################################################### 24 | 25 | library(deSolve) 26 | library(ggplot2) 27 | library(tidygraph) 28 | library(ggraph) 29 | 30 | ############################################################################### 31 | # Plot Functions 32 | ############################################################################### 33 | 34 | MakePCRModelPlot<-function(inputData,PlotTitle){ 35 | #Returns a ggplot 36 | #inputData -> model data 37 | #ColumNames -> Names for the columns in the data 38 | #PlotTitle -> Title for the plot 39 | ModelData<-inputData 40 | 41 | graphContainer<-ggplot(data=ModelData,aes(x=Time,y=ReversePrimer+FowardPrimer,color="Primers"))+geom_line()+ 42 | geom_line(aes(y=ReverseStrand+FowardStrand,color="OriginalSample"))+ 43 | geom_line(aes(y=FowardStrand_Primer+ReverseStrand_Primer,color="Amplicon"))+ 44 | geom_line(aes(y=Reannealing,color="Reannealing"))+ 45 | geom_line(aes(y=PrimerDimers,color="PrimerDimers"))+ 46 | scale_color_manual(values=c("Primers"="black","OriginalSample"="red","Amplicon"="green","Reannealing"="blue","PrimerDimers"="Cyan"))+ 47 | labs(title=PlotTitle,color=" ")+ 48 | theme(axis.title.y = element_blank(),axis.text.y = element_blank(),axis.ticks.y = element_blank(),axis.text.x = element_blank(),axis.ticks.x = element_blank()) 49 | show(graphContainer) 50 | } 51 | 52 | ############################################################################### 53 | #Solver function 54 | ############################################################################### 55 | 56 | solveModel<- function(Model,InitialConditions,ModelParameters,ColumnNames,MinMax){ 57 | #Solves numerically an ODE system model,returns a formated dataframe 58 | #Model -> function, Model to be solved 59 | #InitialConditions -> list, Initial conditions for the ODE system 60 | #ModelParameters -> list, Parameters of the ODE model 61 | #ColumnNames -> list, names of the columns for the dataframe 62 | #MinMax -> bool, controlls if a minmax normalization is applied to the data. 63 | times <- seq(0, 5, by = 0.01) 64 | out <- ode(InitialConditions,times,Model,ModelParameters,method="rk4") 65 | if (MinMax){ 66 | dims<-dim(out) 67 | for (k in 2:dims[2]){ 68 | out[,k]<-MinMaxNormalization(out[,k]) 69 | } 70 | } 71 | ModelData<-data.frame(out) 72 | colnames(ModelData)<-ColumnNames 73 | ModelData 74 | } 75 | 76 | ############################################################################### 77 | #Models 78 | ############################################################################### 79 | 80 | PCRModel <- function(t,Y,params){ 81 | 82 | #Simple organic matter decomposition model 83 | #t -> integration time value 84 | # 85 | #Y -> list Values for the function to be evaluated 86 | # Y[1],Y[2]-> Foward and reverse primer content 87 | # Y[3],Y[4]-> Sample DNA foward and reverse strand 88 | # Y[5],Y[6]-> Complex sampleDNA-primer foward and reverse 89 | # Y[7] -> mRNA and protein content of gene 2 90 | # Y[5] -> mRNA and protein content of gene 2 91 | # 92 | #params -> Parameters of the ODE system model 93 | # 94 | # 95 | 96 | with(as.list(c(Y,params)),{ 97 | 98 | dP1dt=-kah1*Y[1]*Y[3]+kdh1*Y[5]-kad*Y[1]*Y[2]+kdd*Y[8] 99 | dP2dt=-kah2*Y[2]*Y[4]+kdh2*Y[6]-kad*Y[1]*Y[2]+kdd*Y[8] 100 | dT1dt=-kah1*Y[1]*Y[3]+kdh1*Y[5]-kau*Y[3]*Y[4]+kdu*Y[7] 101 | dT2dt=-kah2*Y[2]*Y[4]+kdh2*Y[6]-kau*Y[3]*Y[4]+kdu*Y[7] 102 | dH1dt=kah1*Y[1]*Y[3]-kdh1*Y[5] 103 | dH2dt=kah2*Y[2]*Y[4]-kdh2*Y[6] 104 | dUdt=kau*Y[3]*Y[4]-kdu*Y[7] 105 | dDdt=kad*Y[1]*Y[2]-kdd*Y[8] 106 | 107 | list(c(dP1dt,dP2dt,dT1dt,dT2dt,dH1dt,dH2dt,dUdt,dDdt)) 108 | }) 109 | } 110 | 111 | params <- c(kah1=0.1,kah2=0.1,kdh1=10**-4,kdh2=10**-4,kad=1,kdd=0.1,kau=0.001,kdu=0) 112 | Y <- c(100,100,50,50,0,0,0,0) 113 | 114 | columnNames<-c("Time","ReversePrimer","FowardPrimer","ReverseStrand","FowardStrand","ReverseStrand_Primer","FowardStrand_Primer","Reannealing","PrimerDimers") 115 | threeNodeData<-solveModel(PCRModel,Y,params,columnNames,FALSE) 116 | MakePCRModelPlot(threeNodeData,"PCR Primers Dimers") 117 | 118 | -------------------------------------------------------------------------------- /Modeling/progesterone.R: -------------------------------------------------------------------------------- 1 | #MIT License 2 | #Copyright (c) 2020 Octavio Gonzalez-Lugo 3 | 4 | #Permission is hereby granted, free of charge, to any person obtaining a copy 5 | #of this software and associated documentation files (the "Software"), to deal 6 | #in the Software without restriction, including without limitation the rights 7 | #to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | #copies of the Software, and to permit persons to whom the Software is 9 | #furnished to do so, subject to the following conditions: 10 | #The above copyright notice and this permission notice shall be included in all 11 | #copies or substantial portions of the Software. 12 | #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 13 | #IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 14 | #FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 15 | #AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 16 | #LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 17 | #OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 18 | #SOFTWARE. 19 | #@author: Octavio Gonzalez-Lugo 20 | 21 | ############################################################################### 22 | # Loading packages 23 | ############################################################################### 24 | 25 | library(deSolve) 26 | library(ggplot2) 27 | library(tidygraph) 28 | library(ggraph) 29 | 30 | ############################################################################### 31 | #Solver function 32 | ############################################################################### 33 | 34 | solveModel<- function(Model,InitialConditions,ModelParameters,ColumnNames){ 35 | #Solves numerically an ODE system model,returns a formated dataframe 36 | #Model -> function, Model to be solved 37 | #InitialConditions -> list, Initial conditions for the ODE system 38 | #ModelParameters -> list, Parameters of the ODE model 39 | #ColumnNames -> list, names of the columns for the dataframe 40 | #MinMax -> bool, controlls if a minmax normalization is applied to the data. 41 | times <- seq(0, 100, by = 0.1) 42 | out <- ode(InitialConditions,times,Model,ModelParameters,method="rk4") 43 | ModelData<-data.frame(out) 44 | colnames(ModelData)<-ColumnNames 45 | ModelData 46 | } 47 | 48 | ############################################################################### 49 | # Plot Functions 50 | ############################################################################### 51 | 52 | ProgesteronePlot<-function(inputData,PlotTitle){ 53 | #Returns a ggplot 54 | #inputData -> model data 55 | #ColumNames -> Names for the columns in the data 56 | #PlotTitle -> Title for the plot 57 | ModelData<-inputData 58 | 59 | graphContainer<-ggplot(data=ModelData,aes(x=Time,y=cholesterol,color="cholesterol"))+geom_line()+ 60 | geom_line(aes(y=progesterone,color="progesterone"))+ 61 | scale_color_manual(values=c("cholesterol"="black","progesterone"="red"))+ 62 | labs(title=PlotTitle,color=" ")+ 63 | theme(axis.title.y = element_blank(),axis.text.y = element_blank(),axis.ticks.y = element_blank(),axis.text.x = element_blank(),axis.ticks.x = element_blank()) 64 | show(graphContainer) 65 | } 66 | 67 | ProgesteroneIntermediatePlot<-function(inputData,PlotTitle){ 68 | #Returns a ggplot 69 | #inputData -> model data 70 | #ColumNames -> Names for the columns in the data 71 | #PlotTitle -> Title for the plot 72 | ModelData<-inputData 73 | 74 | graphContainer<-ggplot(data=ModelData,aes(x=Time,y=cholesterol,color="cholesterol"))+geom_line()+ 75 | geom_line(aes(y=progesterone,color="progesterone"))+ 76 | geom_line(aes(y=pregnenolone,color="pregnenolone"))+ 77 | scale_color_manual(values=c("cholesterol"="black","progesterone"="red","pregnenolone"="blue"))+ 78 | labs(title=PlotTitle,color=" ")+ 79 | theme(axis.title.y = element_blank(),axis.text.y = element_blank(),axis.ticks.y = element_blank(),axis.text.x = element_blank(),axis.ticks.x = element_blank()) 80 | show(graphContainer) 81 | } 82 | 83 | ############################################################################### 84 | #Model Progesterone 85 | ############################################################################### 86 | 87 | ProgesteroneLinear <- function(t,Y,params){ 88 | 89 | #Simple organic matter decomposition model 90 | #t -> integration time value 91 | # 92 | #Y -> list Values for the function to be evaluated 93 | # Y[1] -> Cholesterol 94 | # Y[2] -> Progesterone 95 | # 96 | #params -> Parameters of the ODE system model 97 | # k1 ->synthesis rate 98 | # k2 ->decay rate 99 | # 100 | 101 | with(as.list(c(Y,params)),{ 102 | 103 | dx1dt= -k1*Y[1] + k2*Y[2] 104 | dx2dt= k1*Y[1] - k2*Y[2] 105 | 106 | list(c(dx1dt,dx2dt)) 107 | }) 108 | } 109 | 110 | params <- c(k1=0.5,k2=0.1) 111 | Y <- c(1,0) 112 | 113 | columnNames<-c("Time","cholesterol","progesterone") 114 | ProgesteroneData<-solveModel(ProgesteroneLinear,Y,params,columnNames) 115 | ProgesteronePlot(ProgesteroneData,"Progesterone Synthesis") 116 | 117 | ############################################################################### 118 | #Models Progesterone control 119 | ############################################################################### 120 | 121 | ProgesteroneControlLinear <- function(t,Y,params){ 122 | 123 | #Simple organic matter decomposition model 124 | #t -> integration time value 125 | # 126 | #Y -> list Values for the function to be evaluated 127 | # Y[1] -> Cholesterol 128 | # Y[2] -> Progesterone 129 | # 130 | #params -> Parameters of the ODE system model 131 | # k1 ->synthesis rate 132 | # k2 ->decay rate 133 | # kc ->cholesterol intake rate 134 | # kc ->cholesterol intake 135 | # 136 | 137 | with(as.list(c(Y,params)),{ 138 | 139 | dx1dt= -k1*Y[1] + k2*Y[2] + kc*u 140 | dx2dt= k1*Y[1] - k2*Y[2] 141 | 142 | list(c(dx1dt,dx2dt)) 143 | }) 144 | } 145 | 146 | params <- c(k1=0.05,k2=0.10,kc=0.1,u=0.025) 147 | Y <- c(1,0) 148 | 149 | columnNames<-c("Time","cholesterol","progesterone") 150 | ProgesteroneControlData<-solveModel(ProgesteroneControlLinear,Y,params,columnNames) 151 | ProgesteronePlot(ProgesteroneControlData,"LowProgesterone Synthesis Rate") 152 | 153 | ############################################################################### 154 | #Models Progesterone Intermediate 155 | ############################################################################### 156 | 157 | ProgesteroneIntermediateLinear <- function(t,Y,params){ 158 | 159 | #Simple organic matter decomposition model 160 | #t -> integration time value 161 | # 162 | #Y -> list Values for the function to be evaluated 163 | # Y[1] -> Cholesterol 164 | # Y[2] -> pregnenolone 165 | # Y[3] -> Progesterone 166 | # 167 | #params -> Parameters of the ODE system model 168 | # k1 ->Chiolesterol convertion rate 169 | # k2 ->Pregenenolone decay rate 170 | # k2 ->Pregnenolone convertion rate 171 | 172 | 173 | with(as.list(c(Y,params)),{ 174 | 175 | dx1dt= -k1*Y[1] + k2*Y[2] 176 | dx2dt= k1*Y[1] - k2*Y[2] 177 | dx3dt= k2*Y[2] - k3*Y[3] 178 | 179 | list(c(dx1dt,dx2dt,dx3dt)) 180 | }) 181 | } 182 | 183 | params <- c(k1=0.15,k2=0.50,k3=0.75) 184 | Y <- c(1,0,0) 185 | columnNames<-c("Time","cholesterol","pregnenolone","progesterone") 186 | ProgesteroneIntermediate<-solveModel(ProgesteroneIntermediateLinear,Y,params,columnNames) 187 | ProgesteroneIntermediatePlot(ProgesteroneIntermediate,"Low Cholesterol Convertion Rate") 188 | -------------------------------------------------------------------------------- /Modeling/signallingnfkb.R: -------------------------------------------------------------------------------- 1 | #MIT License 2 | #Copyright (c) 2021 Octavio Gonzalez-Lugo 3 | 4 | #Permission is hereby granted, free of charge, to any person obtaining a copy 5 | #of this software and associated documentation files (the "Software"), to deal 6 | #in the Software without restriction, including without limitation the rights 7 | #to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | #copies of the Software, and to permit persons to whom the Software is 9 | #furnished to do so, subject to the following conditions: 10 | #The above copyright notice and this permission notice shall be included in all 11 | #copies or substantial portions of the Software. 12 | #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 13 | #IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 14 | #FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 15 | #AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 16 | #LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 17 | #OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 18 | #SOFTWARE. 19 | #@author: Octavio Gonzalez-Lugo 20 | 21 | ############################################################################### 22 | # Loading packages 23 | ############################################################################### 24 | 25 | library(deSolve) 26 | library(ggplot2) 27 | library(tidygraph) 28 | library(ggraph) 29 | 30 | ############################################################################### 31 | #Solver function 32 | ############################################################################### 33 | 34 | solveModel<- function(Model,InitialConditions,ModelParameters,ColumnNames){ 35 | #Solves numerically an ODE system model,returns a formated dataframe 36 | #Model -> function, Model to be solved 37 | #InitialConditions -> list, Initial conditions for the ODE system 38 | #ModelParameters -> list, Parameters of the ODE model 39 | #ColumnNames -> list, names of the columns for the dataframe 40 | #MinMax -> bool, controlls if a minmax normalization is applied to the data. 41 | times <- seq(0, 50, by = 0.1) 42 | out <- ode(InitialConditions,times,Model,ModelParameters,method="rk4") 43 | ModelData<-data.frame(out) 44 | colnames(ModelData)<-ColumnNames 45 | ModelData 46 | } 47 | 48 | ############################################################################### 49 | # Plot Functions 50 | ############################################################################### 51 | 52 | SiganllingPlot<-function(inputData,PlotTitle){ 53 | #Returns a ggplot 54 | #inputData -> model data 55 | #ColumNames -> Names for the columns in the data 56 | #PlotTitle -> Title for the plot 57 | ModelData<-inputData 58 | 59 | graphContainer<-ggplot(data=ModelData,aes(x=Time,y=CytoplasmNFkB,color="CytoplasmNFkB"))+geom_line()+ 60 | geom_line(aes(y=NuclearNFkB,color="NuclearNFkB"))+ 61 | scale_color_manual(values=c("CytoplasmNFkB"="black","NuclearNFkB"="red"))+ 62 | labs(title=PlotTitle,color=" ")+ 63 | theme(axis.title.y = element_blank(),axis.text.y = element_blank(),axis.ticks.y = element_blank(),axis.text.x = element_blank(),axis.ticks.x = element_blank()) 64 | show(graphContainer) 65 | } 66 | 67 | ############################################################################### 68 | #Model Progesterone 69 | ############################################################################### 70 | 71 | SignallingNfkB <- function(t,Y,params){ 72 | 73 | #Simple organic matter decomposition model 74 | #t -> integration time value 75 | # 76 | #Y -> list Values for the function to be evaluated 77 | # Y[1] -> cell receptors 78 | # Y[2] -> gene regulation 79 | # 80 | # 81 | #params -> Parameters of the ODE system model 82 | # a,d -> self regulation rates 83 | # b,g -> feedback regulation 84 | 85 | with(as.list(c(Y,params)),{ 86 | 87 | dx1dt= s - a*Y[1] - b*Y[2] 88 | dx2dt= g*Y[1] - d*Y[2] 89 | 90 | list(c(dx1dt,dx2dt)) 91 | }) 92 | } 93 | 94 | params <- c(s=1,a=0.15,b=1.5,g=1.5,d=0.15) 95 | Y <- c(1,1) 96 | 97 | columnNames<-c("Time","CytoplasmNFkB","NuclearNFkB") 98 | NfKbData<-solveModel(SignallingNfkB,Y,params,columnNames) 99 | SiganllingPlot(NfKbData,"Signaling") 100 | 101 | -------------------------------------------------------------------------------- /Modeling/spread.r: -------------------------------------------------------------------------------- 1 | #MIT License 2 | #Copyright (c) 2020 Octavio Gonzalez-Lugo 3 | 4 | #Permission is hereby granted, free of charge, to any person obtaining a copy 5 | #of this software and associated documentation files (the "Software"), to deal 6 | #in the Software without restriction, including without limitation the rights 7 | #to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | #copies of the Software, and to permit persons to whom the Software is 9 | #furnished to do so, subject to the following conditions: 10 | #The above copyright notice and this permission notice shall be included in all 11 | #copies or substantial portions of the Software. 12 | #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 13 | #IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 14 | #FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 15 | #AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 16 | #LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 17 | #OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 18 | #SOFTWARE. 19 | #@author: Octavio Gonzalez-Lugo 20 | 21 | ############################################################################### 22 | # Automata definitions 23 | ############################################################################### 24 | 25 | rulesPattern=matrix(0,16,4) 26 | 27 | rulesPattern[1,]<-c(1,1,1,1) 28 | rulesPattern[2,]<-c(1,0,1,1) 29 | rulesPattern[3,]<-c(1,1,1,0) 30 | rulesPattern[4,]<-c(1,0,1,0) 31 | rulesPattern[5,]<-c(1,1,0,1) 32 | rulesPattern[6,]<-c(1,0,0,1) 33 | rulesPattern[7,]<-c(1,1,0,0) 34 | rulesPattern[8,]<-c(1,0,0,0) 35 | rulesPattern[9,]<-c(0,1,1,1) 36 | rulesPattern[10,]<-c(0,0,1,1) 37 | rulesPattern[11,]<-c(0,1,1,0) 38 | rulesPattern[12,]<-c(0,0,1,0) 39 | rulesPattern[13,]<-c(0,1,0,1) 40 | rulesPattern[14,]<-c(0,0,0,1) 41 | rulesPattern[15,]<-c(0,1,0,0) 42 | rulesPattern[16,]<-c(0,0,0,0) 43 | 44 | 45 | rulesValues=c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0) 46 | 47 | ############################################################################### 48 | # Automata functions 49 | ############################################################################### 50 | 51 | getPointNeighbours<-function(Xposition,Yposition,currentState){ 52 | 53 | dimensions=dim(currentState) 54 | 55 | if(Yposition+1>dimensions[2] | Yposition+1==0){ 56 | top<-0 57 | } 58 | else{ 59 | top<-currentState[Xposition,Yposition+1] 60 | } 61 | 62 | if (Yposition-1>dimensions[2] | Yposition-1==0){ 63 | bottom<-0 64 | } 65 | else{ 66 | bottom<-currentState[Xposition,Yposition-1] 67 | } 68 | 69 | if(Xposition-1>dimensions[1] | Xposition-1==0){ 70 | left<-0 71 | } 72 | else{ 73 | left<-currentState[Xposition-1,Yposition] 74 | } 75 | 76 | if (Xposition+1>dimensions[1] | Xposition+1==0){ 77 | right<-0 78 | } 79 | else{ 80 | right<-currentState[Xposition+1,Yposition] 81 | } 82 | 83 | positions<-c(top,bottom,left,right) 84 | } 85 | 86 | 87 | findRuleValue<-function(value,RulesPatterns,RulesValues){ 88 | 89 | #find the correspondig value for the current value 90 | #value 91 | # -> list, current fragment to be evaluated 92 | #RulesPatterns 93 | # -> matrix, contains the different patterns 94 | #RulesValues 95 | # -> list, contains the output value for each pattern 96 | 97 | nRules<-length(RulesPatterns[,1]) 98 | out<-2 99 | for(k in 1:nRules){ 100 | if(isTRUE(all.equal(value,RulesPatterns[k,]))){ 101 | out<-RulesValues[k] 102 | break 103 | } 104 | } 105 | out 106 | } 107 | 108 | ############################################################################### 109 | # Functions 110 | ############################################################################### 111 | 112 | randomInitialState<-function(Xsize,Ysize,Threshold){ 113 | 114 | #creates a random list of 0,1 values of variabble size 115 | #size 116 | # -> int, size of the list 117 | Container<-matrix(0,Xsize,Ysize) 118 | for (k in 1:Ysize){ 119 | localDisc<-runif(Xsize) 120 | localVec<-rep(0,Xsize) 121 | for (j in 1:Xsize){ 122 | if (localDisc[j]>=Threshold){ 123 | localVec[j]<-1 124 | } 125 | } 126 | Container[,k]<-localVec 127 | } 128 | Container 129 | } 130 | 131 | spacedGridState<-function(Xsize,Ysize,Space,Counter){ 132 | Container<-matrix(0,Xsize,Ysize) 133 | currentX<-0 134 | currentY<-0 135 | rowState<-0 136 | for(k in 1:Counter){ 137 | if(currentX>=Xsize | currentX+Space>=Xsize){ 138 | if (rowState==0){ 139 | currentX<-0 140 | rowState<-1 141 | } 142 | else{ 143 | rowState<-0 144 | currentX<-as.integer(Space/2) 145 | } 146 | 147 | currentY<-currentY+Space 148 | } 149 | else{ 150 | currentX<-currentX+Space 151 | } 152 | Container[currentX,currentY]<-1 153 | } 154 | Container 155 | } 156 | 157 | SocialDistancingState<-function(State){ 158 | 159 | #creates a random list of 0,1 values of variabble size 160 | #size 161 | # -> int, size of the list 162 | Step<-3 163 | Sizes<-dim(State) 164 | Ysize<-Sizes[2] 165 | Xsize<-Sizes[1] 166 | 167 | for (k in seq(1,Ysize,Step)){ 168 | State[,k]<-rep(0,Xsize) 169 | } 170 | for (k in seq(1,Xsize,Step)){ 171 | State[k,]<-rep(0,Ysize) 172 | } 173 | State 174 | } 175 | 176 | ############################################################################### 177 | # Functions 178 | ############################################################################### 179 | 180 | iterateAutomata<-function(InitialState,Steps,RulesPatterns,RulesValues,distancing){ 181 | 182 | #Wrapper function to evaluate the automata 183 | #Steps 184 | # -> int, Iterations for the rules to be applied 185 | #InitialState 186 | # -> list, Row o be evaluated 187 | #RulesPatterns 188 | # -> matrix, contains the different patterns 189 | #RulesValues 190 | # -> list, contains the output value for each pattern 191 | if(distancing==TRUE){ 192 | automataContainer<-SocialDistancingState(InitialState) 193 | } 194 | else{ 195 | automataContainer<-InitialState 196 | } 197 | 198 | png('automataStep0.png') 199 | image(automataContainer,axes=FALSE,col=gray((0:32)/32),main = paste("Spread fraction",toString(sum(automataContainer)/length(automataContainer)))) 200 | dev.off() 201 | 202 | Sizes<-dim(automataContainer) 203 | Ysize<-Sizes[2] 204 | Xsize<-Sizes[1] 205 | 206 | for(k in 1:Steps){ 207 | newState<-matrix(0,Xsize,Ysize) 208 | for(Ys in 1:Ysize){ 209 | for(Xs in 1:Xsize){ 210 | neighbours<-getPointNeighbours(Xs,Ys,automataContainer) 211 | newVal<-findRuleValue(neighbours,RulesPatterns,RulesValues) 212 | newState[Xs,Ys]<-newVal 213 | } 214 | } 215 | if(distancing==TRUE){ 216 | newState<-SocialDistancingState(newState) 217 | } 218 | png(paste('automataStep',toString(k),'.png',sep='')) 219 | image(newState,axes=FALSE,col=gray((0:32)/32),main = paste("Spread fraction",toString(sum(newState)/length(newState)))) 220 | dev.off() 221 | automataContainer<-newState 222 | } 223 | automataContainer 224 | } 225 | 226 | initState<-spacedGridState(200,250,8,780) 227 | iterateAutomata(initState,30,rulesPattern,rulesValues,TRUE) 228 | -------------------------------------------------------------------------------- /ParameterEstimation/Fragments/ODEFiting01/Fragment01.py: -------------------------------------------------------------------------------- 1 | #Numpy array that contains the integration times 2 | SolverTime=np.linspace(0,2) 3 | 4 | def ODE(C,t): 5 | 6 | return -kc*C 7 | 8 | #Solution of the model 9 | ModelSolution=odeint(ODE,C0,SolverTime) 10 | -------------------------------------------------------------------------------- /ParameterEstimation/Fragments/ODEFiting01/Fragment02.py: -------------------------------------------------------------------------------- 1 | #General function to solve the ODE model 2 | def GeneralSolver(t,k,C0): 3 | 4 | localK=k 5 | localC0=C0 6 | 7 | def ODEModel(C,t): 8 | 9 | return -localK*C 10 | 11 | sol=odeint(ODEModel,localC0,t) 12 | 13 | return sol[:,0] 14 | 15 | #Solves the ODE model using the initial condition provided above 16 | def ODESolution(t,k): 17 | 18 | return GeneralSolver(t,k,C0) 19 | 20 | #Element wise sum of two iterables of the same size, name makes reference to the output rather than the process 21 | def MakeNoisyData(Data,Noise): 22 | 23 | return [val+cal for val,cal in zip(Data,Noise)] 24 | 25 | #Solving the ODE model 26 | t_vals=np.linspace(0,2,num=1000) 27 | solution=ODESolution(t_vals,kc) 28 | 29 | #Making some simulated data to perform regression 30 | WhiteNoise=[np.random.uniform(low=-1,high=1)/20 for val in solution] 31 | WhiteSignal=MakeNoisyData(solution,WhiteNoise) 32 | Kp=curve_fit(ODESolution,t_vals,WhiteSignal)[0][0] 33 | 34 | #Parameter estimation 35 | fitSolution=ODESolution(t_vals,Kp) 36 | 37 | -------------------------------------------------------------------------------- /ParameterEstimation/Fragments/ODEFiting01/Fragment03.py: -------------------------------------------------------------------------------- 1 | #Data size lenghts to test 2 | nums=[1000,500,100,50,25,10] 3 | 4 | #Library of ODE solutions 5 | t_lib=[np.linspace(0,2,num=val) for val in nums] 6 | sol_lib=[ODESolution(clib,kc) for clib in t_lib] 7 | 8 | #Library of simulated data 9 | noises=[[np.random.uniform(low=-1,high=1)/20 for val in sol] for sol in sol_lib] 10 | signal=[MakeNoisyData(sol,nos) for sol,nos in zip(sol_lib,noises)] 11 | 12 | #Parameter estimation an performance evaluation 13 | params=[curve_fit(ODESolution,times,signals)[0][0] for times,signals in zip(t_lib,signal)] 14 | solutions=[ODESolution(times,kS) for times,kS in zip(t_lib,params)] 15 | -------------------------------------------------------------------------------- /ParameterEstimation/Fragments/ODEFiting01/Fragment04.py: -------------------------------------------------------------------------------- 1 | #Generating noise data with mixed signals 2 | PeriodicNoise=[np.random.uniform(low=-1,high=1)/30+np.sin(val/np.pi)/30 for val in range(len(t_data))] 3 | LinearNoise=[np.random.uniform(low=-1,high=1)/30-0.04*(val/30) for val in range(len(t_data))] 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Data Analysis By Example 2 | 3 | Contains several pices of code with different purposes, from data science projecs, optimization, machine learning and animation. Find the explanation and application of those pices of code at https://tavoglc.medium.com/ 4 | -------------------------------------------------------------------------------- /Visualization/BarPlot.py: -------------------------------------------------------------------------------- 1 | """ 2 | 3 | MIT License 4 | 5 | Copyright (c) 2019 Octavio Gonzalez-Lugo 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | @author: Octavio Gonzalez-Lugo 24 | 25 | """ 26 | 27 | ###################################################################### 28 | # Animating the protein. 29 | ###################################################################### 30 | 31 | import bpy 32 | 33 | ###################################################################### 34 | # Intended Use 35 | ###################################################################### 36 | 37 | """ 38 | The following code is intended to be used at the start of the program 39 | before making any changes in the scene. 40 | """ 41 | 42 | ###################################################################### 43 | # Data Preprocessing 44 | ###################################################################### 45 | 46 | 47 | barHeight=[0.25,0.74,0.51,0.89,0.71] 48 | barLabels=['October','November','December','January','February'] 49 | tickLabels=['0.0','0.25','0.5','0.75','1.0'] 50 | 51 | nbars=len(barHeight) 52 | nticks=len(tickLabels) 53 | 54 | BarNames=['Cube'] 55 | for k in range(1,nbars): 56 | BarNames.append('Cube'+'.00'+str(k)) 57 | 58 | LabelNames=['Text'] 59 | for k in range(1,nbars): 60 | LabelNames.append('Text'+'.00'+str(k)) 61 | 62 | TickNames=[] 63 | 64 | for k in range(nbars,nbars+nticks): 65 | TickNames.append('Text.00'+str(k)) 66 | 67 | step=7/(nbars-1) 68 | tickStep=4.5/(len(tickLabels)-1) 69 | 70 | barLocation=[-3.5+(k*step) for k in range(nbars)] 71 | tickLocation=[2.25-(k*tickStep) for k in range(nticks)] 72 | 73 | ###################################################################### 74 | # Removing the cube from the new file. 75 | ###################################################################### 76 | 77 | bpy.data.objects['Cube'].select=True 78 | bpy.ops.object.delete() 79 | 80 | ###################################################################### 81 | # Moving the camera 82 | ###################################################################### 83 | 84 | bpy.data.objects['Camera'].location = (-0.2,-4.0,12) 85 | bpy.data.objects['Camera'].rotation_euler=(-0.0349066,-0.279253,1.5708) 86 | 87 | ###################################################################### 88 | # Adding the grid 89 | ###################################################################### 90 | 91 | bpy.ops.mesh.primitive_grid_add(radius=1,location=(0,0,0)) 92 | bpy.data.objects['Grid'].scale=(2.25,4.25,1) 93 | bpy.data.objects['Grid'].modifiers.new('Wireframe',type='WIREFRAME') 94 | 95 | ###################################################################### 96 | # Adding the bars 97 | ###################################################################### 98 | 99 | for k in range(nbars): 100 | currentBarHeight = 2.25*barHeight[k] 101 | currentBarLocation=2.25-currentBarHeight 102 | bpy.ops.mesh.primitive_cube_add(radius=1,location=(currentBarLocation,barLocation[k],0.5)) 103 | bpy.data.objects[BarNames[k]].scale=(currentBarHeight,0.25,0.25) 104 | 105 | ###################################################################### 106 | # Adding the bar labels 107 | ###################################################################### 108 | 109 | for k in range(nbars): 110 | currentName=barLabels[k] 111 | bpy.ops.object.text_add(location=(2.8,barLocation[k]-0.25,0.5),enter_editmode=True) 112 | for j in range(4): 113 | bpy.ops.font.delete(type='PREVIOUS_OR_SELECTION') 114 | for char in currentName: 115 | bpy.ops.font.text_insert(text=char) 116 | bpy.ops.object.editmode_toggle() 117 | 118 | for k in range(nbars): 119 | currentLabelName=LabelNames[k] 120 | bpy.data.objects[currentLabelName].rotation_euler=(0,0,1.5708) 121 | bpy.data.objects[currentLabelName].scale=(0.34,0.34,0.34) 122 | 123 | ###################################################################### 124 | # Adding the tick labels 125 | ###################################################################### 126 | 127 | for k in range(nticks): 128 | currentName=tickLabels[k] 129 | bpy.ops.object.text_add(location=(tickLocation[k],-4.9,0.5),enter_editmode=True) 130 | for j in range(4): 131 | bpy.ops.font.delete(type='PREVIOUS_OR_SELECTION') 132 | for char in currentName: 133 | bpy.ops.font.text_insert(text=char) 134 | bpy.ops.object.editmode_toggle() 135 | 136 | for k in range(nticks): 137 | currentLabelName=TickNames[k] 138 | bpy.data.objects[currentLabelName].rotation_euler=(0,0,1.5708) 139 | bpy.data.objects[currentLabelName].scale=(0.34,0.34,0.34) 140 | 141 | ###################################################################### 142 | # editing the plot 143 | ###################################################################### 144 | 145 | bpy.context.scene.render.engine = 'CYCLES' 146 | bpy.context.scene.world.horizon_color = (1,1,1) 147 | 148 | #Adding materials to the ticks 149 | for k in range(nticks): 150 | 151 | currentMaterial = bpy.data.materials.new(name='TickMaterial'+str(k)) 152 | currentMaterial.diffuse_color=(0,0,0) 153 | bpy.data.objects[TickNames[k]].data.materials.append(currentMaterial) 154 | 155 | #Adding materials to the labels 156 | for k in range(nbars): 157 | 158 | currentMaterial = bpy.data.materials.new(name='LabelMaterial'+str(k)) 159 | currentMaterial.diffuse_color=(0,0,0) 160 | bpy.data.objects[LabelNames[k]].data.materials.append(currentMaterial) 161 | 162 | #Adding materials to the bars 163 | for k in range(nbars): 164 | 165 | currentMaterial = bpy.data.materials.new(name='Glass BSDF') 166 | currentMaterial.diffuse_color=(0.2,k/10,k/5+0.1) 167 | bpy.data.objects[BarNames[k]].data.materials.append(currentMaterial) 168 | -------------------------------------------------------------------------------- /Visualization/ClassifierDF.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | """ 4 | MIT License 5 | Copyright (c) 2021 Octavio Gonzalez-Lugo 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | @author: Octavio Gonzalez-Lugo 23 | """ 24 | 25 | ############################################################################### 26 | # Loading packages 27 | ############################################################################### 28 | 29 | import csv 30 | import numpy as np 31 | import pandas as pd 32 | import matplotlib.pyplot as plt 33 | 34 | from sklearn import svm 35 | from sklearn import ensemble as ens 36 | from sklearn import preprocessing as pr 37 | from sklearn import decomposition as dec 38 | from sklearn import discriminant_analysis as da 39 | 40 | ############################################################################### 41 | # Exporting Data Functions 42 | ############################################################################### 43 | 44 | def DataExporter(Data,DataDir): 45 | ''' 46 | Simple function to save data into a csv file 47 | 48 | Parameters 49 | ---------- 50 | Data : array-like 51 | Data to be saved. 52 | DataDir : raw string 53 | Location of the file to be saved. 54 | 55 | Returns 56 | ------- 57 | None. 58 | 59 | ''' 60 | 61 | with open(DataDir,'w',newline='') as output: 62 | 63 | writer=csv.writer(output) 64 | 65 | for k in range(len(Data)): 66 | writer.writerow(Data[k]) 67 | 68 | ############################################################################### 69 | # Plotting functions 70 | ############################################################################### 71 | 72 | def PlotStyle(Axes): 73 | """ 74 | Parameters 75 | ---------- 76 | Axes : Matplotlib axes object 77 | Applies a general style to the matplotlib object 78 | 79 | Returns 80 | ------- 81 | None. 82 | """ 83 | Axes.spines['top'].set_visible(False) 84 | Axes.spines['bottom'].set_visible(True) 85 | Axes.spines['left'].set_visible(True) 86 | Axes.spines['right'].set_visible(False) 87 | Axes.xaxis.set_tick_params(labelsize=13) 88 | Axes.yaxis.set_tick_params(labelsize=13) 89 | 90 | 91 | def MakeDesitionFunctionPlot(Data,Masks,Grids,TrainedModelFunction,Name): 92 | ''' 93 | Returns a plot of the desition function of a given trained model. 94 | 95 | Parameters 96 | ---------- 97 | Data : array-like 98 | Data used to train the model. 99 | Masks : array-like 100 | Labels for each data point. 101 | Grids : array-like 102 | mesh for the mesh function. 103 | TrainedModelFunction : sklearn model 104 | Trained sklearn model. 105 | Name : str 106 | name of the file to be saved in the global directory. 107 | 108 | Returns 109 | ------- 110 | None. 111 | 112 | ''' 113 | 114 | GoodMask,BadMask=Masks 115 | xx,yy=Grids 116 | 117 | z=TrainedModelFunction.decision_function(np.c_[xx.ravel(),yy.ravel()]) 118 | z=z.reshape(xx.shape) 119 | 120 | DataExporter(z,GlobalDirectory+Name+'mesh.csv') 121 | 122 | plt.figure(figsize=(8,8)) 123 | 124 | plt.plot(Data[GoodMask,0],Data[GoodMask,1],'bo') 125 | plt.plot(Data[BadMask,0],Data[BadMask,1],'ro') 126 | plt.pcolormesh(xx,yy,z,shading='auto',cmap='RdYlBu') 127 | ax=plt.gca() 128 | PlotStyle(ax) 129 | 130 | 131 | ############################################################################### 132 | # Loading the data 133 | ############################################################################### 134 | 135 | GlobalDirectory=r"/home/tavoglc/LocalData/" 136 | DataDir=GlobalDirectory + "winequality-red.csv" 137 | 138 | Data=pd.read_csv(DataDir,delimiter=";") 139 | DataHeaders=['fixed acidity','volatile acidity','citric acid','residual sugar','chlorides', 140 | 'free sulfur dioxide','total sulfur dioxide','density','pH','sulphates','alcohol'] 141 | 142 | ############################################################################### 143 | # Data manipulations 144 | ############################################################################### 145 | 146 | QualityHeader='quality' 147 | 148 | #Wrapper function to binarize the labels. 149 | def ToGoodBad(val): 150 | if val>5: 151 | return 1 152 | else: 153 | return 0 154 | 155 | Data["GoodBad"]=Data[QualityHeader].apply(ToGoodBad) 156 | 157 | XData=np.array(Data[DataHeaders]) 158 | YData=np.array(Data["GoodBad"]) 159 | 160 | GoodMask=[j for j in range(len(YData)) if YData[j]==1] 161 | BadMask=[j for j in range(len(YData)) if YData[j]==0] 162 | 163 | gridSize=400 164 | dmin,dmax=-1,1 165 | 166 | xx,yy=np.meshgrid(np.linspace(dmin,dmax,gridSize), 167 | np.linspace(dmin,dmax,gridSize)) 168 | 169 | ############################################################################### 170 | # Data Scaling 171 | ############################################################################### 172 | 173 | Scaler=pr.MinMaxScaler() 174 | Scaler.fit(XData) 175 | XData=Scaler.transform(XData) 176 | 177 | DimReduction=dec.PCA(n_components=2) 178 | DimReduction.fit(XData) 179 | XData=DimReduction.transform(XData) 180 | 181 | DataExporter(XData,GlobalDirectory+'PCAData.csv') 182 | DataExporter(np.array([YData,Data[QualityHeader]]),GlobalDirectory+'Mask.csv') 183 | 184 | ############################################################################### 185 | # Model training 186 | ############################################################################### 187 | 188 | Models=[da.LinearDiscriminantAnalysis,da.QuadraticDiscriminantAnalysis, 189 | svm.LinearSVC,svm.NuSVC] 190 | 191 | Names=['lda','qda','linearsvc','nusvc'] 192 | 193 | for nme,model in zip(Names,Models): 194 | 195 | cModel=model() 196 | cModel.fit(XData,YData) 197 | MakeDesitionFunctionPlot(XData,[GoodMask,BadMask],[xx,yy],cModel,nme) 198 | 199 | 200 | 201 | -------------------------------------------------------------------------------- /Visualization/Heatmapsmpl.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | """ 4 | Created on Mon May 10 00:59:58 2021 5 | 6 | @author: tavoglc 7 | """ 8 | 9 | ############################################################################### 10 | # Loading packages 11 | ############################################################################### 12 | 13 | import csv 14 | import numpy as np 15 | import pandas as pd 16 | import matplotlib.pyplot as plt 17 | from scipy.spatial import distance as ds 18 | 19 | ############################################################################### 20 | # Exporting Data Functions 21 | ############################################################################### 22 | 23 | def DataExporter(Data,DataDir): 24 | ''' 25 | Simple function to save data into a csv file 26 | 27 | Parameters 28 | ---------- 29 | Data : array-like 30 | Data to be saved. 31 | DataDir : raw string 32 | Location of the file to be saved. 33 | 34 | Returns 35 | ------- 36 | None. 37 | 38 | ''' 39 | 40 | with open(DataDir,'w',newline='') as output: 41 | 42 | writer=csv.writer(output) 43 | 44 | for k in range(len(Data)): 45 | writer.writerow(Data[k]) 46 | 47 | ############################################################################### 48 | # Plotting functions 49 | ############################################################################### 50 | 51 | def MakeMatrixPlot(Data,Labels): 52 | """ 53 | Parameters 54 | ---------- 55 | Data : array-like 56 | Data to be analized. 57 | Labels : list 58 | List with the data headers for correlation analysis. 59 | 60 | Returns 61 | ------- 62 | None. 63 | 64 | """ 65 | 66 | plt.figure(figsize=(10,10)) 67 | plt.imshow(Data) 68 | ax=plt.gca() 69 | ax.set_xticks(np.arange(len(Labels))) 70 | ax.set_yticks(np.arange(len(Labels))) 71 | ax.set_xticklabels(Labels) 72 | ax.set_yticklabels(Labels) 73 | ax.spines['top'].set_visible(False) 74 | ax.spines['bottom'].set_visible(False) 75 | ax.spines['left'].set_visible(False) 76 | ax.spines['right'].set_visible(False) 77 | ax.xaxis.set_tick_params(labelsize=13,rotation=90) 78 | ax.yaxis.set_tick_params(labelsize=13) 79 | 80 | ############################################################################### 81 | # Loading the data 82 | ############################################################################### 83 | 84 | GlobalDirectory=r"/home/tavoglc/LocalData/" 85 | DataDir=GlobalDirectory + "winequality-red.csv" 86 | 87 | Data=pd.read_csv(DataDir,delimiter=";") 88 | DataHeaders=['fixed acidity','volatile acidity','citric acid','residual sugar','chlorides', 89 | 'free sulfur dioxide','total sulfur dioxide','density','pH','sulphates','alcohol'] 90 | 91 | 92 | CorrData=Data.corr() 93 | CorrData.to_csv(GlobalDirectory+"Correlation.csv") 94 | 95 | ############################################################################### 96 | # Plotting functions 97 | ############################################################################### 98 | 99 | MakeMatrixPlot(Data.corr(),Data.corr().keys()) 100 | 101 | ############################################################################### 102 | # Loading the data 103 | ############################################################################### 104 | 105 | UniqueQuality=np.sort(Data["quality"].unique()) 106 | QualityLabels=["Quality Score "+str(val) for val in UniqueQuality] 107 | 108 | def GetSimilarityMatrix(Data,Mask,Similarity): 109 | """ 110 | Parameters 111 | ---------- 112 | Data : array-like 113 | Data to be analized. 114 | Mask : array-like 115 | values to be grouped for comparison. 116 | Similarity : function 117 | Function for pairwise comparison. 118 | 119 | Returns 120 | ------- 121 | ContainerMatrix : array 122 | Comparison data. 123 | 124 | """ 125 | 126 | ContainerMatrix=np.zeros((len(Mask),len(Mask))) 127 | 128 | for k in range(len(Mask)): 129 | kvals= Data[Data["quality"]==Mask[k]].mean() 130 | for j in range(k,len(Mask)): 131 | jvals= Data[Data["quality"]==Mask[j]].mean() 132 | ContainerMatrix[k,j]=Similarity(kvals,jvals) 133 | ContainerMatrix[j,k]=Similarity(kvals,jvals) 134 | 135 | return ContainerMatrix 136 | 137 | ############################################################################### 138 | # Loading the data 139 | ############################################################################### 140 | 141 | SimilarityMeasures=[ds.euclidean,ds.correlation,ds.cosine,ds.canberra,ds.cityblock] 142 | SimilarityNames=["euclidean","correlation","cosine","canberra","cityblock"] 143 | 144 | for val,sal in zip(SimilarityMeasures,SimilarityNames): 145 | 146 | MatrixData0=GetSimilarityMatrix(Data,UniqueQuality,val) 147 | MakeMatrixPlot(MatrixData0,QualityLabels) 148 | DataExporter(MatrixData0,GlobalDirectory+sal+".csv") -------------------------------------------------------------------------------- /Visualization/Scatter.py: -------------------------------------------------------------------------------- 1 | 2 | """ 3 | MIT License 4 | Copyright (c) 2020 Octavio Gonzalez-Lugo 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | SOFTWARE. 21 | @author: Octavio Gonzalez-Lugo 22 | 23 | """ 24 | 25 | ###################################################################### 26 | # Intended Use 27 | ###################################################################### 28 | 29 | """ 30 | The following code is intended to be used at the start of the program 31 | before making any changes in the scene. 32 | """ 33 | 34 | ############################################################################### 35 | # Loading packages 36 | ############################################################################### 37 | 38 | import bpy 39 | import random 40 | 41 | ############################################################################### 42 | # Data 43 | ############################################################################### 44 | 45 | nPoints=250 46 | 47 | Xvalues=[(j/nPoints) + (random.random()/5) for j in range(nPoints)] 48 | Yvalues=[(j/nPoints) + (random.random()/5) for j in range(nPoints)] 49 | 50 | ############################################################################### 51 | # GeneralFunctions 52 | ############################################################################### 53 | 54 | #Generates a list with the names of the objects 55 | def MakeObjectNames(ObjectName,NumberOfObjects): 56 | 57 | """ 58 | ObjectName -> Base name of the object created 59 | NumberOfObjects -> Number of objects to be created 60 | """ 61 | 62 | NamesContainer=[] 63 | NamesContainer.append(ObjectName) 64 | 65 | for k in range(1,NumberOfObjects): 66 | if k<=9: 67 | NamesContainer.append(ObjectName+".00"+str(k)) 68 | elif k>9 and k<=99: 69 | NamesContainer.append(ObjectName+".0"+str(k)) 70 | elif k>99 and k<=999: 71 | NamesContainer.append(ObjectName+"."+str(k)) 72 | 73 | return NamesContainer 74 | 75 | def InsertAndChangeText(TextLocation,TextLabel): 76 | 77 | bpy.ops.object.text_add(location=TextLocation,enter_editmode=True) 78 | for k in range(4): 79 | bpy.ops.font.delete(type='PREVIOUS_OR_SELECTION') 80 | for char in TextLabel: 81 | bpy.ops.font.text_insert(text=char) 82 | bpy.ops.object.editmode_toggle() 83 | 84 | #Wrapper function to create a list of shpere object names 85 | def MakeDotNames(NumberOfElements): 86 | return MakeObjectNames("Sphere",NumberOfElements) 87 | 88 | #Wrapper function to create a list of shpere object names 89 | def MakeTextNames(NumberOfElements): 90 | return MakeObjectNames("Text",NumberOfElements) 91 | 92 | ############################################################################### 93 | # Removing the original cube 94 | ############################################################################### 95 | 96 | bpy.data.objects["Cube"].select=True 97 | bpy.ops.object.delete() 98 | 99 | ############################################################################### 100 | # Moving the camera 101 | ############################################################################### 102 | 103 | bpy.data.objects["Camera"].location=(1.43,0.53,3.4) 104 | bpy.data.objects["Camera"].rotation_euler=(0,0.2799,0) 105 | 106 | ############################################################################### 107 | # Adding the axes 108 | ############################################################################### 109 | 110 | bpy.ops.mesh.primitive_cube_add(location=(-0.025,0.6,0)) 111 | bpy.ops.mesh.primitive_cube_add(location=(0.6,-0.025,0)) 112 | 113 | bpy.data.objects["Cube"].scale=(0.01,0.6,0.01) 114 | bpy.data.objects["Cube.001"].scale=(0.6,0.01,0.01) 115 | 116 | ############################################################################### 117 | # Adding the axes 118 | ############################################################################### 119 | 120 | counter=0 121 | for k in range(3): 122 | InsertAndChangeText((counter,-0.25,0),str(counter)) 123 | counter=counter+0.5 124 | 125 | counter=0 126 | for k in range(3): 127 | InsertAndChangeText((-0.3,counter,0),str(counter)) 128 | counter=counter+0.5 129 | 130 | TextNames=MakeTextNames(6) 131 | 132 | for val in TextNames: 133 | bpy.data.objects[val].scale=(0.15,0.15,0.15) 134 | 135 | ############################################################################### 136 | # Adding the data points 137 | ############################################################################### 138 | 139 | DotNames=MakeDotNames(nPoints) 140 | 141 | for k in range(nPoints): 142 | bpy.ops.mesh.primitive_uv_sphere_add(location=(Xvalues[k],Yvalues[k],0)) 143 | bpy.data.objects[DotNames[k]].scale=(0.01,0.01,0.01) 144 | 145 | ############################################################################### 146 | # Adding the material 147 | ############################################################################### 148 | 149 | bpy.context.scene.render.engine = 'CYCLES' 150 | bpy.context.scene.world.horizon_color = (1,1,1) 151 | 152 | #Adding Material to the axis 153 | 154 | for val in ['Cube','Cube.001']: 155 | 156 | currentMaterial = bpy.data.materials.new(name='TickMaterial'+val) 157 | currentMaterial.diffuse_color=(0,0,0) 158 | bpy.data.objects[val].data.materials.append(currentMaterial) 159 | 160 | #Adding materials to the ticks 161 | for k in range(6): 162 | 163 | currentMaterial = bpy.data.materials.new(name='TickMaterial'+str(k)) 164 | currentMaterial.diffuse_color=(0,0,0) 165 | bpy.data.objects[TextNames[k]].data.materials.append(currentMaterial) 166 | 167 | #Adding materials to the dots 168 | for k in range(nPoints): 169 | 170 | currentMaterial = bpy.data.materials.new(name='Glass BSDF') 171 | currentMaterial.diffuse_color=(0.2,k/nPoints,k/nPoints) 172 | bpy.data.objects[DotNames[k]].data.materials.append(currentMaterial) 173 | 174 | ############################################################################### 175 | # Adding the analytics 176 | ############################################################################### 177 | 178 | import numpy as np 179 | 180 | A =np.transpose(np.vstack([Xvalues, np.ones(len(Xvalues))])) 181 | slope,intercept = np.linalg.lstsq(A,Yvalues)[0] 182 | Rsquared=np.corrcoef(Xvalues,Yvalues)[0,0] 183 | 184 | Rsquared="R = " + str(round(Rsquared,3)) 185 | 186 | bpy.ops.mesh.primitive_cube_add(location=(0.5,(slope*0.5)+intercept,0)) 187 | bpy.data.objects["Cube.002"].scale=(0.01,0.6,0.01) 188 | bpy.data.objects["Cube.002"].rotation_euler=(0,0,-np.arctan(slope)) 189 | currentMaterial = bpy.data.materials.new(name='TickMaterial'+'Cube.002') 190 | currentMaterial.diffuse_color=(0.75,0.75,0.75) 191 | bpy.data.objects['Cube.002'].data.materials.append(currentMaterial) 192 | 193 | InsertAndChangeText((0.75,0.05,0.1),Rsquared) 194 | 195 | bpy.data.objects['Text.006'].scale=(0.15,0.15,0.15) 196 | currentMaterial = bpy.data.materials.new(name='TickMaterial'+str(k)) 197 | currentMaterial.diffuse_color=(0.75,0.75,0.75) 198 | bpy.data.objects['Text.006'].data.materials.append(currentMaterial) 199 | -------------------------------------------------------------------------------- /education/mathexcercises.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | """ 4 | Created on Wed Jun 7 21:12:31 2023 5 | 6 | @author: tavo 7 | """ 8 | 9 | import numpy as np 10 | import matplotlib.pyplot as plt 11 | 12 | ############################################################################### 13 | # Loading packages 14 | ############################################################################### 15 | 16 | def ImageStyle(Axes): 17 | """ 18 | Parameters 19 | ---------- 20 | Axes : Matplotlib axes object 21 | Applies a general style to the matplotlib object 22 | 23 | Returns 24 | ------- 25 | None. 26 | """ 27 | Axes.spines['top'].set_visible(False) 28 | Axes.spines['bottom'].set_visible(False) 29 | Axes.spines['left'].set_visible(False) 30 | Axes.spines['right'].set_visible(False) 31 | Axes.set_xticks([]) 32 | Axes.set_yticks([]) 33 | 34 | def BottomStyle(Axes): 35 | """ 36 | Parameters 37 | ---------- 38 | Axes : Matplotlib axes object 39 | Applies a general style to the matplotlib object 40 | 41 | Returns 42 | ------- 43 | None. 44 | """ 45 | Axes.spines['top'].set_visible(False) 46 | Axes.spines['bottom'].set_visible(True) 47 | Axes.spines['left'].set_visible(False) 48 | Axes.spines['right'].set_visible(False) 49 | Axes.set_yticks([]) 50 | 51 | ############################################################################### 52 | # Loading packages 53 | ############################################################################### 54 | 55 | numbers = np.arange(1,10) 56 | 57 | plt.figure(figsize=(8.27,11.7)) 58 | 59 | ax = plt.gca() 60 | 61 | for k in range(19): 62 | for j in range(4): 63 | string = str(np.random.choice(numbers)) + ' + ' + str(np.random.choice(numbers)) + ' =' 64 | ax.text(0.05 + (j*0.25) ,0.95-(k*0.05),string,fontsize=18) 65 | 66 | ImageStyle(ax) 67 | plt.tight_layout() 68 | plt.savefig("foo.png") 69 | 70 | ############################################################################### 71 | # Loading packages 72 | ############################################################################### 73 | 74 | numbers = np.arange(1,10) 75 | operations = ['+','-','*'] 76 | plt.figure(figsize=(8.27,11.7)) 77 | 78 | ax = plt.gca() 79 | 80 | for k in range(19): 81 | for j in range(4): 82 | string = str(np.random.choice(numbers)) + ' ' + np.random.choice(operations) + ' ' + str(np.random.choice(numbers)) + ' =' 83 | ax.text(0.05 + (j*0.25) ,0.95-(k*0.05),string,fontsize=18) 84 | 85 | ImageStyle(ax) 86 | plt.tight_layout() 87 | plt.savefig("foo2.png") 88 | 89 | ############################################################################### 90 | # Loading packages 91 | ############################################################################### 92 | 93 | numbers = np.arange(1,10) 94 | operations = ['+','-'] 95 | 96 | fig,axs = plt.subplots(10,1,figsize=(8.27,11.7)) 97 | 98 | for k,val in enumerate(axs): 99 | 100 | val.plot() 101 | first = np.random.choice(numbers) 102 | second = np.random.choice(numbers) 103 | operation = np.random.choice(operations) 104 | 105 | string = str(first) + ' ' + operation + ' ' + str(second) + ' =' 106 | 107 | if operation=='-': 108 | result = first - second 109 | else: 110 | result = first + second 111 | 112 | minv = min([result,first,second]) - 4 113 | maxv = max([result,first,second]) + 4 114 | 115 | rangeval = np.arange(minv,maxv) 116 | 117 | val.set_ylim([0,1]) 118 | val.set_xlim([minv,maxv]) 119 | val.set_xticks(rangeval) 120 | 121 | val.text(minv,0.75,string,fontsize=18) 122 | BottomStyle(val) 123 | 124 | plt.tight_layout() 125 | plt.savefig("foo3.png") 126 | 127 | 128 | -------------------------------------------------------------------------------- /scrapping/ageofmithologyunits.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | """ 4 | MIT License 5 | Copyright (c) 2022 Octavio Gonzalez-Lugo 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | @author: Octavio Gonzalez-Lugo 23 | """ 24 | 25 | ############################################################################### 26 | # Importing packages 27 | ############################################################################### 28 | 29 | import numpy as np 30 | import pandas as pd 31 | import requests as req 32 | import seaborn as sns 33 | import matplotlib.pyplot as plt 34 | 35 | from bs4 import BeautifulSoup 36 | 37 | ############################################################################### 38 | # Plot functions 39 | ############################################################################### 40 | 41 | def PlotStyle(Axes): 42 | """ 43 | Parameters 44 | ---------- 45 | Axes : Matplotlib axes object 46 | Applies a general style to the matplotlib object 47 | 48 | Returns 49 | ------- 50 | None. 51 | """ 52 | Axes.spines['top'].set_visible(False) 53 | Axes.spines['bottom'].set_visible(True) 54 | Axes.spines['left'].set_visible(True) 55 | Axes.spines['right'].set_visible(False) 56 | Axes.xaxis.set_tick_params(labelsize=12) 57 | Axes.yaxis.set_tick_params(labelsize=12) 58 | 59 | ############################################################################### 60 | # Web scrapping 61 | ############################################################################### 62 | 63 | #Wrapper function to acces the html data 64 | def GetPageData(PageUrl): 65 | 66 | headers = {'User-Agent': "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:72.0) Gecko/20100101 Firefox/72.0"} 67 | resp = req.get(PageUrl,headers=headers) 68 | soup = BeautifulSoup(resp.text, 'lxml') 69 | 70 | return soup 71 | 72 | def FormatTableData(PageData): 73 | ''' 74 | Formats the table data inside an HTML page into a 75 | numpy 2D array 76 | Parameters 77 | ---------- 78 | PageData : bs4.BeautifulSoup 79 | Parsed HTML data. 80 | 81 | Returns 82 | ------- 83 | headers : list 84 | list of strings with the table headers. 85 | tableData : array 86 | Data inside the table without the headers. 87 | 88 | ''' 89 | 90 | headers = [val.text for val in PageData.find_all('th')] 91 | 92 | tableRows = PageData.find_all('tr') 93 | tableData = [] 94 | 95 | for row in tableRows[1:len(tableRows)]: 96 | 97 | rowData = [val.text for val in row.find_all('td')] 98 | tableData.append(rowData) 99 | 100 | tableData = np.array(tableData) 101 | 102 | #Index of duplicated headers 103 | duplicates = [k for k,x in enumerate(headers) if headers.count(x) >= 2] 104 | 105 | #Check for duplicates in the headers 106 | if len(duplicates)>0: 107 | for val in duplicates: 108 | headers[val] = headers[val] + str(val) 109 | 110 | return headers, tableData 111 | 112 | pageData = GetPageData('https://www.unitstatistics.com/age-of-mythology/') 113 | headers,tableData = FormatTableData(pageData) 114 | 115 | ############################################################################### 116 | # Data cleaning 117 | ############################################################################### 118 | 119 | DataFrame = pd.DataFrame() 120 | 121 | Numeric = [3,4,5,6,7,8,9,10,11,12,13,14,15,16] 122 | 123 | for k,val in enumerate(headers): 124 | 125 | currentData = tableData[:,k] 126 | 127 | if k in Numeric: 128 | container = [] 129 | for sal in currentData: 130 | if sal == '\xa0': 131 | container.append(0) 132 | else: 133 | container.append(float(''.join(e for e in sal if e!='%'))) 134 | else: 135 | container = ['None' if xal =='\xa0' else xal for xal in currentData] 136 | 137 | 138 | DataFrame[val] = container 139 | 140 | ToFullName = {'A':'Atlantean','G':'Greek','E':'Egyptian','N':'Nors'} 141 | 142 | DataFrame['Civ'] = [ToFullName[val] for val in DataFrame['Civ']] 143 | 144 | container = [] 145 | for val in DataFrame['Range']: 146 | 147 | if val=='None': 148 | container.append(0) 149 | else: 150 | disc = val.find('-') 151 | if disc==-1: 152 | container.append(int(val)) 153 | else: 154 | container.append(int(val[disc+1:len(val)])) 155 | 156 | DataFrame['Range'] = container 157 | 158 | ############################################################################### 159 | # Data visualization 160 | ############################################################################### 161 | 162 | wrapperDF = DataFrame.groupby(['Civ','Type2'])['Unit'].count().to_frame().reset_index().pivot(index='Civ',columns='Type2',values='Unit') 163 | plt.figure() 164 | wrapperDF.T.plot(kind='bar') 165 | ax = plt.gca() 166 | PlotStyle(ax) 167 | 168 | wrapperDF = DataFrame.groupby(['Civ','Type19'])['Unit'].count().to_frame().reset_index().pivot(index='Civ',columns='Type19',values='Unit') 169 | plt.figure() 170 | wrapperDF.T.plot(kind='bar') 171 | ax = plt.gca() 172 | PlotStyle(ax) 173 | 174 | Numeric.append(20) 175 | 176 | for val in Numeric: 177 | plt.figure() 178 | sns.barplot(data=DataFrame,x='Type2',y=headers[val],hue='Civ') 179 | plt.legend(bbox_to_anchor=(1.0, 1.0)) 180 | plt.xticks(rotation=45) 181 | ax = plt.gca() 182 | PlotStyle(ax) 183 | -------------------------------------------------------------------------------- /scrapping/market.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | """ 4 | MIT License 5 | Copyright (c) 2021 Octavio Gonzalez-Lugo 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | @author: Octavio Gonzalez-Lugo 23 | """ 24 | 25 | import re 26 | import time 27 | import numpy as np 28 | import pandas as pd 29 | import requests as req 30 | 31 | from bs4 import BeautifulSoup 32 | import matplotlib.pyplot as plt 33 | 34 | ############################################################################### 35 | # Getting the options 36 | ############################################################################### 37 | 38 | def PlotStyle(Axes): 39 | """ 40 | Parameters 41 | ---------- 42 | Axes : Matplotlib axes object 43 | Applies a general style to the matplotlib object 44 | 45 | Returns 46 | ------- 47 | None. 48 | """ 49 | Axes.spines['top'].set_visible(False) 50 | Axes.spines['bottom'].set_visible(True) 51 | Axes.spines['left'].set_visible(True) 52 | Axes.spines['right'].set_visible(False) 53 | Axes.xaxis.set_tick_params(labelsize=13) 54 | Axes.yaxis.set_tick_params(labelsize=13) 55 | 56 | ############################################################################### 57 | # Getting the options 58 | ############################################################################### 59 | 60 | #Wrapper function to acces the html data 61 | def GetPageData(PageUrl): 62 | 63 | headers = {'User-Agent': "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:72.0) Gecko/20100101 Firefox/72.0"} 64 | resp = req.get(PageUrl,headers=headers,verify=False) 65 | soup = BeautifulSoup(resp.text, 'lxml') 66 | 67 | return soup 68 | 69 | #Wrapper function to acces and format the scrapped data 70 | def FormatProductData(PageData): 71 | 72 | entries = PageData.find_all("div",class_="product details product-item-details") 73 | 74 | itemNames = [val.find("a").text for val in entries] 75 | itemPrices = [val.find('span',class_="price").text for val in entries] 76 | itemNames = [' '.join(val.split()) for val in itemNames ] 77 | itemPrices = [float(val[1::]) for val in itemPrices ] 78 | 79 | return itemNames,itemPrices 80 | 81 | def ListFlatten(ListOfLists): 82 | return [item for sublist in ListOfLists for item in sublist] 83 | 84 | ############################################################################### 85 | # Getting the options 86 | ############################################################################### 87 | 88 | pages = [] 89 | pages.append(r'https://tiendasneto.com.mx/productos') 90 | skeleton = r"https://tiendasneto.com.mx/productos?p=" 91 | 92 | for k in range(2,27): 93 | pages.append(skeleton+str(k)) 94 | 95 | data = [] 96 | for val in pages: 97 | localData = GetPageData(val) 98 | time.sleep(20) 99 | data.append(FormatProductData(localData)) 100 | 101 | ############################################################################### 102 | # Getting the options 103 | ############################################################################### 104 | 105 | Names = ListFlatten([val[0] for val in data]) 106 | Prices = ListFlatten([val[1] for val in data]) 107 | 108 | plt.figure(figsize=(10,5)) 109 | plt.hist(Prices,bins=50) 110 | ax=plt.gca() 111 | ax.set_xlabel("Price") 112 | PlotStyle(ax) 113 | 114 | ############################################################################### 115 | # Getting the options 116 | ############################################################################### 117 | 118 | #Wrapper function to set the class of a given produt 119 | #Iterates trough a list of classes until it finds the appropiate class 120 | def AssignClass(Product,Classes): 121 | 122 | responce = 'None' 123 | for val in Classes: 124 | if re.match('(.*)' + val + '(.*)', Product): 125 | responce = val 126 | 127 | return responce 128 | 129 | 130 | Categories = ['ACEITE','ACONDICIONADOR','AROMATIZANTE','AVENA', 131 | 'BLANQUEADOR','BOTANA','CEREAL','CERVEZA','DESECHABLE', 132 | 'DESENGRASANTE', 'DESODORANTE', 'DETERGENTE','DULCE', 133 | 'GALLETA','LECHE','MERMELADA','PAN','PAPEL','PERRO', 134 | 'QUESO','REFRESCO','SALSA','YOGHURT'] 135 | 136 | ScrappedDF = pd.DataFrame() 137 | 138 | ScrappedDF["name"] = Names 139 | ScrappedDF["price"] = Prices 140 | ScrappedDF["class"] = [AssignClass(val,Categories) for val in Names] 141 | 142 | ############################################################################### 143 | # Getting the options 144 | ############################################################################### 145 | 146 | plt.figure(figsize=(10,5)) 147 | ax = plt.gca() 148 | ScrappedDF.boxplot(column = "price",by="class",grid=False,ax=ax) 149 | ax.xaxis.set_tick_params(rotation=85) 150 | ax.set_title('') 151 | fig = ax.get_figure() 152 | fig.suptitle('') 153 | PlotStyle(ax) 154 | 155 | ############################################################################### 156 | # Getting the options 157 | ############################################################################### 158 | 159 | PurchaseCategories= ['ACEITE','ACONDICIONADOR','AROMATIZANTE','AVENA', 160 | 'BLANQUEADOR','BOTANA','CEREAL','DESECHABLE', 161 | 'DESENGRASANTE', 'DESODORANTE', 'DETERGENTE', 162 | 'DULCE','GALLETA','LECHE','MERMELADA','PAN', 163 | 'PAPEL','QUESO','REFRESCO','SALSA','YOGHURT', 164 | 'CERVEZA'] 165 | 166 | def MakeRandomPurchase(dataframe,categories): 167 | ''' 168 | Simulation of a random single item purchase in the categories 169 | list 170 | 171 | Parameters 172 | ---------- 173 | dataframe : pandas dataframe 174 | Data frame with the price and class information of the scrapped 175 | products. 176 | categories : list 177 | Contains the different categories for a single item purchase. 178 | 179 | Returns 180 | ------- 181 | cost : float 182 | cost of the purchase. 183 | 184 | ''' 185 | 186 | index = np.arange(dataframe.shape[0]) 187 | np.random.shuffle(index) 188 | NameToLoc = {nme:k for nme,k in zip(categories,np.arange(len(categories)))} 189 | counter = np.zeros(len(categories)) 190 | cost = 0 191 | 192 | for val in index: 193 | if np.sum(counter)==len(counter): 194 | break 195 | else: 196 | product = dataframe.iloc[val] 197 | productClass = product['class'] 198 | InCategories = productClass in categories 199 | 200 | if InCategories: 201 | location = NameToLoc[productClass] 202 | if counter[location]==0: 203 | cost = cost + product['price'] 204 | counter[location] = 1 205 | 206 | return cost 207 | 208 | container = [] 209 | 210 | for k in range(2000): 211 | 212 | container.append(MakeRandomPurchase(ScrappedDF,PurchaseCategories)) 213 | 214 | plt.figure(figsize=(10,5)) 215 | plt.hist(container,bins=50) 216 | ax = plt.gca() 217 | PlotStyle(ax) 218 | -------------------------------------------------------------------------------- /scrapping/weather.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | """ 4 | MIT License 5 | Copyright (c) 2022 Octavio Gonzalez-Lugo 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | @author: Octavio Gonzalez-Lugo 24 | """ 25 | ############################################################################### 26 | # Importing packages 27 | ############################################################################### 28 | 29 | import time 30 | import numpy as np 31 | import pandas as pd 32 | import matplotlib.pyplot as plt 33 | 34 | from meteostat import Daily 35 | from datetime import datetime 36 | from meteostat import Stations 37 | 38 | from math import radians 39 | from sklearn.neighbors import KNeighborsRegressor 40 | 41 | ############################################################################### 42 | # Plotting functions 43 | ############################################################################### 44 | 45 | def ImageStyle(Axes): 46 | """ 47 | Parameters 48 | ---------- 49 | Axes : Matplotlib axes object 50 | Applies a general style to the matplotlib object 51 | 52 | Returns 53 | ------- 54 | None. 55 | """ 56 | Axes.spines['top'].set_visible(False) 57 | Axes.spines['bottom'].set_visible(False) 58 | Axes.spines['left'].set_visible(False) 59 | Axes.spines['right'].set_visible(False) 60 | Axes.set_xticks([]) 61 | Axes.set_yticks([]) 62 | 63 | ############################################################################### 64 | # Loading locations data 65 | ############################################################################### 66 | 67 | GlobalDirectory=r"/media/tavoglc/Datasets/datasets/DABE/LifeSciences/COVIDSeqs/nov2021" 68 | datadir = GlobalDirectory+'/mergedDF.csv' 69 | dst = pd.read_csv(datadir) 70 | 71 | uniqueLocations = np.array([list(x) for x in set(tuple(x) for x in np.array(dst[['geo_lat','geo_long','geo_alt']]))]) 72 | 73 | ############################################################################### 74 | # Downloading weather data 75 | ############################################################################### 76 | 77 | start = datetime(2021, 1, 1) 78 | end = datetime(2021, 12, 31) 79 | 80 | container = [] 81 | retrived = [] 82 | retrivedIndex = [] 83 | notRetrived = [] 84 | notRetrivedIndex = [] 85 | 86 | for k,val in enumerate(uniqueLocations): 87 | 88 | stations = Stations() 89 | stations = stations.nearby(val[0],val[1]) 90 | station = stations.fetch(1) 91 | 92 | data = Daily(station, start, end) 93 | data = data.fetch() 94 | 95 | if len(data)>0: 96 | 97 | data['week'] = data.index.isocalendar().week 98 | ndst = data.groupby('week').mean() 99 | localdf = ndst[['tavg','prcp','pres']] 100 | disc = localdf.isna().sum().sum() 101 | 102 | if disc>40: 103 | notRetrived.append(val) 104 | notRetrivedIndex.append(k) 105 | print('Wait') 106 | time.sleep(4) 107 | print('Go') 108 | else: 109 | container.append(ndst[['tavg','prcp','pres']]) 110 | retrived.append(val) 111 | retrivedIndex.append(k) 112 | print('Wait') 113 | time.sleep(4) 114 | print('Go') 115 | 116 | else: 117 | notRetrived.append(val) 118 | notRetrivedIndex.append(k) 119 | 120 | ############################################################################### 121 | # Formating the data 122 | ############################################################################### 123 | 124 | temperature = pd.DataFrame() 125 | for k,val in zip(retrivedIndex,container): 126 | temperature[str(k)] = val['tavg'] 127 | 128 | temperature = temperature.fillna(temperature.mean()) 129 | 130 | precipitation = pd.DataFrame() 131 | for k,val in zip(retrivedIndex,container): 132 | precipitation[str(k)] = val['prcp'] 133 | 134 | precipitation = precipitation.fillna(precipitation.mean()) 135 | 136 | pressure = pd.DataFrame() 137 | for k,val in zip(retrivedIndex,container): 138 | pressure[str(k)] = val['pres'] 139 | 140 | pressure = pressure.fillna(pressure.mean()) 141 | 142 | ############################################################################### 143 | # Predicting missing data 144 | ############################################################################### 145 | 146 | retrivedLocations = np.array(retrived)[:,0:2] 147 | retrivedLocations = np.array([[radians(val[0]),radians(val[1])] for val in retrivedLocations]) 148 | 149 | notRetrivedLocations = np.array(notRetrived)[:,0:2] 150 | notRetrivedLocations = np.array([[radians(val[0]),radians(val[1])] for val in notRetrivedLocations]) 151 | 152 | tempPredictor = KNeighborsRegressor(n_neighbors=5,metric='haversine') 153 | tempPredictor.fit(retrivedLocations,np.array(temperature).T) 154 | 155 | prcpPredictor = KNeighborsRegressor(n_neighbors=5,metric='haversine') 156 | prcpPredictor.fit(retrivedLocations,np.array(precipitation).T) 157 | 158 | presPredictor = KNeighborsRegressor(n_neighbors=5,metric='haversine') 159 | presPredictor.fit(retrivedLocations,np.array(pressure).T) 160 | 161 | predictedTemp = tempPredictor.predict(notRetrivedLocations) 162 | predictedPrcp = prcpPredictor.predict(notRetrivedLocations) 163 | predictedPres = presPredictor.predict(notRetrivedLocations) 164 | 165 | ############################################################################### 166 | # Adding it to the data frame 167 | ############################################################################### 168 | 169 | for k,val in enumerate(notRetrivedIndex): 170 | temperature[str(val)] = predictedTemp[k,:] 171 | 172 | for k,val in enumerate(notRetrivedIndex): 173 | precipitation[str(val)] = predictedPrcp[k,:] 174 | 175 | for k,val in enumerate(notRetrivedIndex): 176 | pressure[str(val)] = predictedPres[k,:] 177 | 178 | locationsDF = pd.DataFrame(uniqueLocations,columns=['geo_lat','geo_long','geo_alt']) 179 | 180 | temperature.to_csv(GlobalDirectory+'/temperature2021.csv') 181 | precipitation.to_csv(GlobalDirectory+'/precipitation2021.csv') 182 | pressure.to_csv(GlobalDirectory+'/pressure2021.csv') 183 | locationsDF.to_csv(GlobalDirectory+'/locations.csv') 184 | 185 | ############################################################################### 186 | # Data visualization 187 | ############################################################################### 188 | 189 | locationToIndex = dict([(tuple(sal),val) for val,sal in zip(np.arange(uniqueLocations.shape[0]),uniqueLocations[:,0:2])]) 190 | 191 | plt.figure() 192 | temperature.mean(axis=1).plot() 193 | 194 | plt.figure() 195 | precipitation.mean(axis=1).plot() 196 | 197 | plt.figure() 198 | pressure.mean(axis=1).plot() 199 | 200 | for i in range(53): 201 | plt.figure() 202 | plt.scatter(uniqueLocations[:,1],uniqueLocations[:,0],c=[precipitation[str(k)][i+1] for k in range(pressure.shape[1])],label='week - '+str(i+1)) 203 | plt.ylim([24,50]) 204 | plt.xlim([-130,-65]) 205 | plt.legend(loc=3) 206 | ax = plt.gca() 207 | ImageStyle(ax) 208 | plt.savefig(GlobalDirectory+'/fig'+str(i)+'.png') 209 | 210 | --------------------------------------------------------------------------------