Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
FROM python:3.6-slim
MAINTAINER Salvo Rinzivillo <rinzivillo@isti.cnr.it>
FROM nikolaik/python-nodejs:latest

MAINTAINER Giulio Rossetti <giulio.rossetti@gmail.com>

COPY requirements.txt /tmp/



RUN pip install --upgrade pip
RUN pip install --upgrade pip
RUN pip install gunicorn
RUN pip install -r /tmp/requirements.txt

RUN apt-get update
RUN apt-get -y install git

COPY . /app
WORKDIR /app

EXPOSE 5000
RUN git clone https://github.com/GiulioRossetti/NDLib_viz.git

WORKDIR NDLib_viz
RUN npm install

EXPOSE 5000 8080

WORKDIR /app
ENTRYPOINT ["/bin/bash"]
CMD ["gunicorn.sh"]
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ This project offers a REST interface for the [ndlib](https://github.com/GiulioRo

Python 3.6 required dependencies:

- [ndlib](https://github.com/GiulioRossetti/ndlib)=3.0.0
- [ndlib](https://github.com/GiulioRossetti/ndlib)==4.0.2
- flask==0.12
- flask-cors==3.0.2
- flask_restful==0.3.5
Expand Down Expand Up @@ -42,7 +42,7 @@ apidoc -i ndlib-rest/ -o ndlib-rest/static/docs


#### Docker Container
The web application is shipped in a [Docker](https://www.docker.com/) container.
The web application (REST service and Viz framework) is shipped as a [Docker](https://www.docker.com/) container.
You can use the Dockerfile to create a new image and run the web application using the gunicorn application server.

To create the Docker image, install Docker on your machine.
Expand All @@ -54,7 +54,7 @@ docker build -t [tagname_for_your_image] .
The command create a new image with the specified name. Pay attention to the ```.``` a the end of the command.

```
docker run -d -i -p 5000:5000 [tagname_for_your_image]
docker run -d -i -p 5000:5000 -p 8080:8080 [tagname_for_your_image]
```
This command execute a container with the previous image, bind the local port 5000 to the internal port of the container.
The option ```-d``` make the container to run in the background (detached)
Expand All @@ -68,6 +68,19 @@ To stop a container

```
docker stop container_name
```
##### Prebuilt Docker image

To avoid docker image building just download the full container (beta version)

```
docker pull rossetti/ndrest:ndrest_beta
```

Once the image is ready run it as:

```
docker run -d -i -p 5000:5000 -p 8080:8080 rossetti/ndrest:ndrest_beta
```

## REST service details
Expand Down
13 changes: 12 additions & 1 deletion gunicorn.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
#!/bin/bash
set -e
exec gunicorn -w 4 -b 0.0.0.0:5000 ndrest:app &

pip install -r requirements.txt
exec gunicorn -w 4 -b 0.0.0.0:5000 ndrest:app > /dev/null &

if [ -d NDLib_viz ]; then
rm -rf NDLib_viz
fi

git clone https://github.com/GiulioRossetti/NDLib_viz.git > /dev/null
cd NDLib_viz
npm install
npm run dev &
52 changes: 52 additions & 0 deletions ndrest.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import ndlib.models.epidemics.SISModel as sis
import ndlib.models.epidemics.SEIRModel as seir
import ndlib.models.epidemics.SEISModel as seis
import ndlib.models.epidemics.SWIRModel as swir
import ndlib.models.epidemics.ProfileModel as ac
import ndlib.models.epidemics.ProfileThresholdModel as pt
import ndlib.models.epidemics.IndependentCascadesModel as ic
Expand Down Expand Up @@ -1453,6 +1454,56 @@ def put(self):
return {'Message': 'Resource created'}, success


class SWIR(Resource):

def put(self):
"""
@api {put} /api/SWIR SWIR
@ApiDescription Instantiate a SWIR Model on the network bound to the provided token.
@apiVersion 2.0.0
@apiParam {String} token The token.
@apiParam {Number{0-1}} infected The initial percentage of infected nodes.
@apiParam {Number{0-1}} kappa Infection rate.
@apiParam {Number{0-1}} mu Recovery rate.
@apiParam {Number{0-1}} nu Incubation period.
@apiName swir
@apiGroup Epidemics
@apiExample [python request] Example usage:
put('http://localhost:5000/api/SWIR', data={'kappa': kappa, 'mu': mu, 'nu': nu, 'infected': percentage, 'token': token})
"""
token = str(request.form['token'])

if not os.path.exists("data/db/%s" % token):
return {"Message": "Wrong Token"}, bad_request

try:
kappa = request.form['kappa']
mu = request.form['mu']
nu = request.form['nu']
infected = request.form['infected']
if infected == '':
infected = 0.05

db_net = load_data("data/db/%s/net" % token)

g = db_net['net']['g']
db_net.close()

model = swir.SWIRModel(g)
config = mc.Configuration()
config.add_model_parameter('percentage_infected', float(infected))
config.add_model_parameter('kappa', float(kappa))
config.add_model_parameter('mu', float(mu))
config.add_model_parameter('nu', float(nu))
model.set_initial_status(config)

config_model(token, "SWIR", model)
except:
return {'Message': 'Parameter error'}, bad_request

return {'Message': 'Resource created'}, success


class Profile(Resource):

def put(self):
Expand Down Expand Up @@ -2486,6 +2537,7 @@ def get(self):
api.add_resource(MaJorityRule, '/api/MajorityRule')
api.add_resource(Sznajd, '/api/Sznajd')
api.add_resource(AlgorithmicBias, '/api/AlgorithmicBias')
api.add_resource(SWIR, '/api/SWIR')

api.add_resource(dSI, '/api/dSI')
api.add_resource(dSIR, '/api/dSIR')
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ flask_cors
requests
numpy
scipy
ndlib
ndlib==4.0.2
gunicorn
Loading