Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull request for #8, #9, #10: Incorporate new gslab_make and update template accordingly #12

Merged
merged 36 commits into from
Dec 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
7c4a79c
Begin updating config and analysis make.py for #8
Nov 29, 2018
95bca1d
Update make.py scrips for #8
Nov 29, 2018
8d558c7
Add functionalities to config for #8
Dec 4, 2018
4954ac3
Clean up config for #8
Dec 4, 2018
9347b53
Change to requirements.txt for #8
Dec 5, 2018
47e6840
Add gslab_make submodule for #9
Dec 6, 2018
1404e4c
Update gslab_make submodule to track issue8_update_template
Dec 7, 2018
e4d7c27
Test git submodule
Dec 7, 2018
cb90f66
Test git submodule
Dec 7, 2018
4ed5a4f
Test git submodule
Dec 7, 2018
7be0450
Change config to setup for #9
Dec 7, 2018
00f4715
Separate input and external folders for #9
Dec 7, 2018
c048cd3
Implement linking changes for #10
Dec 13, 2018
f1d83f7
Clean up code
Dec 13, 2018
ce7122c
Add to setup documentation
Dec 14, 2018
6ad5882
Make local import more robust
Dec 15, 2018
2586604
#10 Correct files inadvertenly committed directly instead of as LFS p…
gentzkow Dec 15, 2018
f868395
#10 Bug fixes in setup_repository.py
gentzkow Dec 15, 2018
bc74204
Bugfix setup_repository.py for #10
Dec 15, 2018
0d8fc6c
Removed submodule for #10
Dec 18, 2018
ee33aa5
Commit gslab_make, update template for new external.txt formatting
Dec 18, 2018
22981ef
Bugfix gslab_make commit
Dec 18, 2018
a7e14a6
Fix formatting for input.txt and external.txt for #10
Dec 18, 2018
17605ee
Add *external/ to .gitignore and organize lib directory for #10
Dec 19, 2018
797a7bb
#10 Update .gitignore
gentzkow Dec 19, 2018
0c17fbb
#10 Fix LFS pointer error for gslab_make/tests/input/zip_test_file.zip
gentzkow Dec 19, 2018
240eb41
Merge branch 'issue10_update_linking' of github.com:gentzkow/template…
gentzkow Dec 19, 2018
14ffa20
#10 Fix LFS pointer errors for paper_slides PDFs
gentzkow Dec 19, 2018
05c0712
#10 Add input directories to .gitignore
gentzkow Dec 19, 2018
c81917e
Update path to look for gslab_make for #10
Dec 19, 2018
1149658
Reorder error checking for bugfixing
Dec 19, 2018
4ccc637
Escape special characters in path for run_stata for #10
Dec 19, 2018
2dc585c
#10 Remove input directories -- will be ignored by git going forward
gentzkow Dec 19, 2018
a0deee9
#10 Correct format of external and input in .gitignore
gentzkow Dec 19, 2018
6274e13
Fix typo
Dec 20, 2018
730345c
Update documentation for #10
Dec 20, 2018
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
*config_user.yaml
*external/
*input/
*.pyc
*.lyx~
*.lyx#
Expand Down
66 changes: 65 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,67 @@
# README
# README

## Setup
1. Install Python dependencies listed in `requirements.txt` using pip. One way to do this is to use the following bash command from the `setup` subdirectory:

```
pip install --user -r requirements.txt
```

2. Run `setup_repository.py`. One way to do this is to use the following bash command from the `setup` subdirectory:
```
python setup_repository.py
```

3. Install Stata dependencies using `setup_stata.do`. One way to do this is to use the following bash command from the `setup` subdirectory:
```
stata-mp -e setup_stata.do
```

4. Install R dependencies using `setup_r.r`. One way to do this is to use the following bash command from the `setup` subdirector:y
```
Rscript setup_r.r
```

## FAQ

1. Help! I'm running into permission errors when trying to install Python dependencies!

<br>

![https://imgs.xkcd.com/comics/python_environment_2x.png](https://imgs.xkcd.com/comics/python_environment_2x.png){:height="50%" width="50%"}

<br>

The standard bash command for pip installing `requirements.txt` often runs into issues as depending on your Python environment, pip will attempt to install to a root directory (i.e., a directory that by default you should not have write permission)
```
pip install -r requirements.txtt
```

One way to get around this is to include `sudo` (or the Windows equivalent) in your bash command.
```
sudo pip install -r requirements.tx
```

However, we caution against this given the potential security risks. Instead, we recommend including the `--user` flag to your bash command.
```
pip install --user -r requirements.txt
```

The `--user` flag instructs pip to install to a local directory (i.e., a directory that by default you should have write permission). If you are still running into permission errors, we recommend the following diagnostic steps:

* Find the local directory that pip is attempting to install to. You can do this by either looking at the permission error message or using the following bash commands:
```
python
import site
site.USER_SITE
```

* If this is a directory that you should have write permission to but do not, use the `sudo chown` bash command (or the Windows equivalent) to get ownership.

* If this is a directory that you should not have write permission to, change your `PYTHONUSERBASE` environment variable to a diretory that you should and do have write permission to.

If you are using Anaconda, we recommend using the following bash command:
```
conda install --file requirements.txt"
```
Note that you may run into issues if any of the Python depencencies are not available on the conda channels. If this is the case, revert back to using `pip`.
11 changes: 7 additions & 4 deletions analysis/code/descriptive.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
import matplotlib.pyplot as plt

def main():
data = np.genfromtxt('../input/data.txt', skip_header = 1)
data = np.genfromtxt('input/data_table.txt', skip_header = 1)

with open('../output/tables.txt', 'w') as f:
with open('output/tables.txt', 'w') as f:
f.write('<tab:table>\n')
f.write('%s\n%.3f\n%d\n%d' % (np.mean(data), np.std(data, ddof = 1), np.max(data), np.min(data)))

data = np.genfromtxt('input/data_graph.txt', skip_header = 1)

plt.hist(data)
plt.savefig('../output/plot.eps')
plt.savefig('output/plot.eps')

main()
# EXECUTE
main()
4 changes: 3 additions & 1 deletion analysis/externals.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# This file will contain paths to files that should be copied to /input/
# Each line of instruction should contain the key for an external dependency listed in `config_user.yaml`

# Key
7 changes: 6 additions & 1 deletion analysis/inputs.txt
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
# This file will contain paths to files that should be copied to /input/
# Each line of instruction should contain a symlink and target delimited by a `|`
# Lines beginning with # are ignored

# Symlink | Target
data_table.txt | {root}/data/output/data_table.txt
data_graph.txt | {root}/data/output/data_graph.txt
28 changes: 28 additions & 0 deletions analysis/log/link_heads.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
File headers
--------------------------------------------------------------------------------
/Users/zong/template/data/output/data_table.txt
--------------------------------------------------------------------------------
x
1
2
3
4
5
6
7
8
9
--------------------------------------------------------------------------------
/Users/zong/template/data/output/data_graph.txt
--------------------------------------------------------------------------------
x
1
2
3
4
5
6
7
8
9
--------------------------------------------------------------------------------
3 changes: 3 additions & 0 deletions analysis/log/link_map.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
symlink target
input/data_table.txt /Users/zong/template/data/output/data_table.txt
input/data_graph.txt /Users/zong/template/data/output/data_graph.txt
3 changes: 3 additions & 0 deletions analysis/log/link_stats.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
file name last modified file size
/Users/zong/template/data/output/data_table.txt 2018-12-19 19:19:34 1988897
/Users/zong/template/data/output/data_graph.txt 2018-12-19 19:19:35 1988897
13 changes: 13 additions & 0 deletions analysis/log/make.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
--------------------------------------------------------------------------------
Makelog started: 2018-12-19 11:19:54
Working directory: /Users/zong/template/analysis
--------------------------------------------------------------------------------
Links successfully created!
Links successfully created!
Link logs successfully written!
Executing: "python "/Users/zong/template/analysis/code/descriptive.py" "
Output logs successfully written!
--------------------------------------------------------------------------------
Makelog ended: 2018-12-19 11:19:57
Working directory: /Users/zong/template/analysis
--------------------------------------------------------------------------------
24 changes: 24 additions & 0 deletions analysis/log/output_heads.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
File headers
--------------------------------------------------------------------------------
/Users/zong/template/analysis/output/plot.eps
--------------------------------------------------------------------------------
%!PS-Adobe-3.0 EPSF-3.0
%%Title: output/plot.eps
%%Creator: matplotlib version 2.2.3, http://matplotlib.org/
%%CreationDate: Wed Dec 19 11:19:57 2018
%%Orientation: portrait
%%BoundingBox: 75 223 536 568
%%EndComments
%%BeginProlog
/mpldict 8 dict def
mpldict begin
--------------------------------------------------------------------------------
/Users/zong/template/analysis/output/tables.txt
--------------------------------------------------------------------------------
<tab:table>
150000.5
86602.685
300000
1
Head not readable or less than 10 lines
--------------------------------------------------------------------------------
3 changes: 3 additions & 0 deletions analysis/log/output_stats.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
file name last modified file size
/Users/zong/template/analysis/output/plot.eps 2018-12-19 19:19:57 12340
/Users/zong/template/analysis/output/tables.txt 2018-12-19 19:19:56 39
60 changes: 47 additions & 13 deletions analysis/make.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,54 @@
#! /usr/bin/env python
# ENVIRONMENT
import git
import imp
import os
import yaml

import shutil, os
import gslab_make as gs
ROOT = git.Repo('.', search_parent_directories = True).working_tree_dir
f, path, desc = imp.find_module('gslab_make', [os.path.join(ROOT, 'lib')])
gs = imp.load_module('gslab_make', f, path, desc)

for dir in ['output', 'input', 'temp']:
shutil.rmtree(dir, ignore_errors='true')
os.mkdir(dir)
os.chdir('code')
PATHS = {
'config_user' : '../config_user.yaml',
'input_dir' : 'input',
'external_dir' : 'external',
'output_dir' : 'output/',
'pdf_dir' : 'output/',
'makelog' : 'log/make.log',
'output_statslog' : 'log/output_stats.log',
'output_headslog' : 'log/output_heads.log',
'link_maplog' : 'log/link_map.log',
'link_statslog' : 'log/link_stats.log',
'link_headslog' : 'log/link_heads.log'
}

gs.start_make_logging()
PATH_MAPPINGS = {
'root': ROOT
}

# GET EXTERNAL INPUT FILES
os.symlink('../../data/output/data.txt', '../input/data.txt')
# CONFIG USER
config_user = yaml.load(open(PATHS['config_user'], 'rb'))
if config_user['local']['executables']:
gs.private.metadata.default_executables[os.name].update(config_user['local']['executables'])
if config_user['external']:
PATH_MAPPINGS.update(config_user['external'])

# START
gs.remove_dir(['input', 'external'])
gs.clear_dir(['output', 'log'])
gs.start_makelog(PATHS)

# GET INPUT FILES
inputs = gs.create_input_links(PATHS, ['inputs.txt'], PATH_MAPPINGS)
externals = gs.create_external_links(PATHS, ['externals.txt'], PATH_MAPPINGS)
gs.write_link_logs(PATHS, inputs + externals)

# RUN SCRIPTS
gs.run_python(program = 'descriptive.py')
gs.run_python(PATHS, program = 'code/descriptive.py')

# LOG OUTPUTS
gs.log_files_in_output(PATHS)

gs.end_make_logging()
raw_input('\n Press <Enter> to exit.')
# END
gs.end_makelog(PATHS)
raw_input('\nPress <Enter> to exit.')
9 changes: 0 additions & 9 deletions analysis/output/make.log

This file was deleted.

6 changes: 3 additions & 3 deletions analysis/output/plot.eps

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ git_lfs_required: Yes
#####################################################################
# Other required software
#####################################################################
gslab_make_required: Yes

software_required:
git_lfs: Yes
gslab_make: Yes
r: Yes
stata: Yes
lyx: Yes
Expand Down
21 changes: 0 additions & 21 deletions config/setup_python.py

This file was deleted.

10 changes: 0 additions & 10 deletions data/code/build_individual_data.py

This file was deleted.

7 changes: 0 additions & 7 deletions data/code/build_state_data.do

This file was deleted.

11 changes: 11 additions & 0 deletions data/code/create_graph_data.do
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
clear all
set more off

program main
set obs 300000
gen x = _n
export delimited "output/data_graph.txt", delimiter("|") replace
end

* EXECUTE
main
9 changes: 9 additions & 0 deletions data/code/create_table_data.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
library(yaml)

main <- function() {
x <- 1:300000
write.table(x, "output/data_table.txt", row.names = FALSE, col.names = TRUE, quote = FALSE)
}

# EXECUTE
main()
10 changes: 0 additions & 10 deletions data/code/join_data.r

This file was deleted.

4 changes: 3 additions & 1 deletion data/externals.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# This file will contain paths to files that should be copied to /input/
# Each line of instruction should contain the key for an external dependency listed in `config_user.yaml`

# Key
5 changes: 4 additions & 1 deletion data/inputs.txt
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
# This file will contain paths to files that should be copied to /input/
# Each line of instruction should contain a symlink and target delimited by a `|`
# Lines beginning with # are ignored

# Symlink | Target
2 changes: 2 additions & 0 deletions data/log/link_heads.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
File headers
--------------------------------------------------------------------------------
1 change: 1 addition & 0 deletions data/log/link_map.log
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
symlink target
1 change: 1 addition & 0 deletions data/log/link_stats.log
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
file name last modified file size
Loading