<commit_before># Crime Modelling and Prediction
## Libraries
NOTE: Some of this libraries may fail to install depending on the ubuntu version you have on your machine.
``` shell
sudo apt-get install binutils libproj-dev postgresql-9.4-postgis-2.1 postgresql-server-dev-9.4 postgresql-contrib-9.4 libatlas-base-dev libblas-dev
```

## Clone
``` shell
git clone <EMAIL>:jayArnel/crimemapping.git
cd crimemapping
```

## Create Virtualenv
``` shell
sudo pip install virtualenvwrapper
mkvirtualenv <name>
workon <name>
```

## Install Package Dependencies
``` shell
pip install -r requirements.txt
```

## Create the Database
``` shell
sudo su - postgres
createdb <dbname>
```
## Create User for the Database
``` shell
sudo su - postgres 
createuser -s -P <username>
    *setup password*
psql
GRANT ALL PRIVILEGES ON DATABASE <dbname> TO <username>;
```

## Extend Database to postgis
``` shell
sudo su - postgres 
psql <dbname>
CREATE EXTENSION postgis;
```

## Setup Database with Django
create  `local_settings.py` file in `crimemapping` folder
``` python
DATABASES = {
    'default': {
        'ENGINE': 'django.contrib.gis.db.backends.postgis',
        'NAME': '<dbname>',
        'USER': '<username>',
        'PASSWORD': '<password>',
        'HOST': 'localhost',
        'PORT': '',
    }
}
```


## Run Migrations
make sure your virtualenv `<env>` is activated by calling `workon <env>`
``` shell
python manage.py migrate
```

## Collect static files
``` shell
python manage.py collectstatic -l --no-input
```

## Setup Nginx
setup a local server in your machine via Nginx

### Install nginx
``` shell
sudo apt-get install nginx
```

### Setup Nginx configuration
create configuration file
``` shell
sudo nano /etc/nginx/sites-available/crimemapping
```
copy-paste configuration
``` nginx
upstream localhost {
    server localhost:8000;
}
server {
    listen 0.0.0.0:80;
    server_name localhost;

    keepalive_timeout 5;
    # path for static files
    root /path/to/project/storage; #! important! update this path
    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        if (!-f $request_filename) {
            proxy_pass http://localhost;
            break;
        }
    }
}
```
enable configuration
``` shell
sudo ln -s /etc/nginx/sites-available/crimemapping /etc/nginx/sites-enabled/crimemapping
``
check for errors, then restart nginx
``` nginx
sudo nginx -t
sudo service nginx restart
```

<commit_msg>show sample of running the model in readme<commit_after># Crime Modelling and Prediction
## Libraries
NOTE: Some of this libraries may fail to install depending on the ubuntu version you have on your machine.
``` shell
sudo apt-get install binutils libproj-dev postgresql-9.4-postgis-2.1 postgresql-server-dev-9.4 postgresql-contrib-9.4 libatlas-base-dev libblas-dev
```

## Clone
``` shell
git clone git@github.com:jayArnel/crimemapping.git
cd crimemapping
```

## Create Virtualenv
``` shell
sudo pip install virtualenvwrapper
mkvirtualenv <name>
workon <name>
```

## Install Package Dependencies
``` shell
pip install -r requirements.txt
```

## Create the Database
``` shell
sudo su - postgres
createdb <dbname>
```
## Create User for the Database
``` shell
sudo su - postgres 
createuser -s -P <username>
    *setup password*
psql
GRANT ALL PRIVILEGES ON DATABASE <dbname> TO <username>;
```

## Extend Database to postgis
``` shell
sudo su - postgres 
psql <dbname>
CREATE EXTENSION postgis;
```

## Setup Database with Django
create  `local_settings.py` file in `crimemapping` folder
``` python
DATABASES = {
    'default': {
        'ENGINE': 'django.contrib.gis.db.backends.postgis',
        'NAME': '<dbname>',
        'USER': '<username>',
        'PASSWORD': '<password>',
        'HOST': 'localhost',
        'PORT': '',
    }
}
```


## Run Migrations
make sure your virtualenv `<env>` is activated by calling `workon <env>`
``` shell
python manage.py migrate
```

## Collect static files
``` shell
python manage.py collectstatic -l --no-input
```

## Setup Nginx
setup a local server in your machine via Nginx

### Install nginx
``` shell
sudo apt-get install nginx
```

### Setup Nginx configuration
create configuration file
``` shell
sudo nano /etc/nginx/sites-available/crimemapping
```
copy-paste configuration
``` nginx
upstream localhost {
    server localhost:8000;
}
server {
    listen 0.0.0.0:80;
    server_name localhost;

    keepalive_timeout 5;
    # path for static files
    root /path/to/project/storage; #! important! update this path
    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        if (!-f $request_filename) {
            proxy_pass http://localhost;
            break;
        }
    }
}
```
enable configuration
``` shell
sudo ln -s /etc/nginx/sites-available/crimemapping /etc/nginx/sites-enabled/crimemapping
``
check for errors, then restart nginx
``` nginx
sudo nginx -t
sudo service nginx restart
```


## Starting the Web App
``` shell
python manage.py runserver
```
Open 'localhost' on your web browser

## Running the Model
``` shell
python manage.py shell
```
```Python
from crimeprediction import network

network.run_network(1000, 'monthly', crime_type=None, seasonal=True)
```
{'fraction_non_alphanumeric': 0.12143788414974856, 'fraction_numerical': 0.007822685788787484, 'mean_word_length': 3.633304572907679, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 3, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3326469', 'n_tokens_mistral': 1828, 'n_tokens_neox': 1675, 'n_words': 531}