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

packaging #6

Closed
wants to merge 12 commits into from
Closed
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
28 changes: 28 additions & 0 deletions .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Python package

on: [push]

jobs:
build:

runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.8", "3.10"]

steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
# This case we are only updating pip, but you could add other dependencies needed.
run: |
python -m pip install --upgrade pip
- name: Test with pytest
run: |
pytest
- name: Install package #reinstall the package everytime it tests
run: |
pip install .
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
brain_average.csv/
brain_expected.csv/

4 changes: 4 additions & 0 deletions CITATION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
If you wish to refer to this course, please cite the URL
http://github-pages.ucl.ac.uk/rsd-engineeringcourse/

Portions of the material are taken from [Software Carpentry](http://software-carpentry.org/)
3 changes: 3 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(C) University College London 2022

This "sagittal_average" example package is granted into the public domain.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Sagittal Average

This is a very simple example package used as part of the UCL
[Research Software Engineering with Python](development.rc.ucl.ac.uk/training/engineering) course.

## Installation

Browse to the directory where this file lives, and run:
```bash
pip install .
```
That command will download any dependencies we have


## Usage

Invoke the tool with `sagittal_average_run <input_file_Dir>` or use it on your own library:

```python
from sagittal_average import sagittal_brain

sagittal_brain.run_averages("input_file.csv", "ouput_file.csv")
```
2 changes: 1 addition & 1 deletion brain_average.csv
Original file line number Diff line number Diff line change
@@ -1 +1 @@
10.5,11.5,12.5,13.5,14.5,15.5,16.5,17.5,18.5,19.5,20.5,21.5,22.5,23.5,24.5,25.5,26.5,27.5,28.5,29.5
0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0
3 changes: 3 additions & 0 deletions brain_expected.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
0.000000
0.000000
1.000000
40 changes: 20 additions & 20 deletions brain_sample.csv
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21
3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22
4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23
5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24
6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25
7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26
8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27
9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28
10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29
11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30
12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32
14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33
15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34
16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35
17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36
18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37
19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38
20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
4 changes: 4 additions & 0 deletions mt_dir_4_testing/mt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@




10 changes: 10 additions & 0 deletions sagittal_average.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Metadata-Version: 2.1
Name: sagittal-average
Version: 0.1.0
Summary: UNKNOWN
Home-page: UNKNOWN
License: UNKNOWN
Platform: UNKNOWN

UNKNOWN

13 changes: 13 additions & 0 deletions sagittal_average.egg-info/SOURCES.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
setup.py
sagittal_average/__init__.py
sagittal_average/command.py
sagittal_average/sagittal_brain.py
sagittal_average.egg-info/PKG-INFO
sagittal_average.egg-info/SOURCES.txt
sagittal_average.egg-info/dependency_links.txt
sagittal_average.egg-info/entry_points.txt
sagittal_average.egg-info/requires.txt
sagittal_average.egg-info/top_level.txt
sagittal_average/test/__init__.py
sagittal_average/test/test_call_command.py
sagittal_average/test/test_sagittal_brain.py
1 change: 1 addition & 0 deletions sagittal_average.egg-info/dependency_links.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

3 changes: 3 additions & 0 deletions sagittal_average.egg-info/entry_points.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[console_scripts]
sagittal_average_run = sagittal_average.command:process

1 change: 1 addition & 0 deletions sagittal_average.egg-info/requires.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
numpy
1 change: 1 addition & 0 deletions sagittal_average.egg-info/top_level.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sagittal_average
Empty file added sagittal_average/__init__.py
Empty file.
Binary file added sagittal_average/__pycache__/__init__.cpython-39.pyc
Binary file not shown.
16 changes: 16 additions & 0 deletions sagittal_average/command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from sagittal_average.sagittal_brain import *

def process():
parser = ArgumentParser(description="Calculates the average for each sagittal-horizontal plane.",
formatter_class=ArgumentDefaultsHelpFormatter)
parser.add_argument('file_input', nargs='?', default="brain_sample.csv",
help="Input CSV file with the results from scikit-brain binning algorithm.")
parser.add_argument('--file_output', '-o', default="brain_average.csv",
help="Name of the output CSV file.")
arguments = parser.parse_args()

run_averages(arguments.file_input, arguments.file_output)


if __name__ == "__main__":
process()
34 changes: 34 additions & 0 deletions sagittal_average/sagittal_brain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter

import numpy as np


def run_averages(file_input='brain_sample.csv', file_output='brain_average.csv'):
"""
Calculates the average through the coronal planes
The input file should has as many columns as coronal planes
The rows are intersections of the sagittal/horizontal planes

The result is the average for each sagittal/horizontal plane (rows)
"""
# Open the file to analyse
planes = np.loadtxt(file_input, dtype=int, delimiter=',')

# Calculates the averages through the sagittal/horizontal planes
# and makes it as a row vector
averages = planes.mean(axis=1)[np.newaxis, :] #axis must =1 to average over rows and not =0 which will av over columns

# write it out on my file
np.savetxt(file_output, averages, fmt='%.1f', delimiter=',')


# if __name__ == "__main__":
# parser = ArgumentParser(description="Calculates the average for each sagittal-horizontal plane.",
# formatter_class=ArgumentDefaultsHelpFormatter)
# parser.add_argument('file_input', nargs='?', default="brain_sample.csv",
# help="Input CSV file with the results from scikit-brain binning algorithm.")
# parser.add_argument('--file_output', '-o', default="brain_average.csv",
# help="Name of the output CSV file.")
# arguments = parser.parse_args()

# run_averages(arguments.file_input, arguments.file_output)
Empty file.
Binary file not shown.
Binary file not shown.
12 changes: 12 additions & 0 deletions sagittal_average/test/test_call_command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import subprocess


#must run this file in terminal
#these basically just let us run terminal commands when we run this python file from a terminal

subprocess.run(["ls", "-aF"], shell=True) #lists all files in current dir
subprocess.run(["ls", "-lh"], shell=True) #lists all files in current dir (in seperate linesw details)
subprocess.run(["echo", 'hello world'], shell=True) #prints hello world in the screen
subprocess.run(["date", '+%Y%m%d'], shell=True) #prints today's date with the selected format
subprocess.run(["wc", "-l", "sagittal_brain.py"], shell=True) #counts the number of lines of the sagittal_brain.py file
subprocess.run(["wc", "-c", "AFileThatDoesNot.exist"], shell=True) #which should counts the number of characters, but should fail as the file doesn't exist
47 changes: 47 additions & 0 deletions sagittal_average/test/test_sagittal_brain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from sagittal_average.sagittal_brain import *
import subprocess
#RUN FROM BASH TERMINAL

import numpy as np


# sample = np.array([[0,0,0], [0,0,0], [1,1,1]])
# np.savetxt('brain_sample.csv', sample, delimiter=',', fmt='%i') #save as csv and integers

# expected = np.asarray([0., 0., 1.])
# np.savetxt('brain_expected.csv',expected, delimiter=',', fmt='%f') #save as csv and integers
# #NB: ik the expected csv looks different format to the output the arrays are the same format which is what matters for testing




# subprocess.run(['python', 'sagittal_brain.py'], shell=True) #run the main py file and create the brain_average result file

# read_output = np.loadtxt('brain_average.csv', delimiter=',') #read the file (ie convert to array)


# #test:
# np.testing.assert_array_equal(read_output, expected)



def test_brain(): #create the above into a function for pytest
data_input = np.zeros((20, 20)) #20x20 instead of 1x3 like before
data_input[-1, :] = 1 #make last row = 1

np.savetxt("brain_sample.csv", data_input, fmt='%d', delimiter=',')


# The expeted result is all zeros, except the last one, it should be 1
expected = np.zeros(20)
expected[-1] = 1

run_averages(file_input="brain_sample.csv",
file_output="brain_average.csv")

result = np.loadtxt( "brain_average.csv", delimiter=',')


return np.testing.assert_array_equal(result, expected)


34 changes: 0 additions & 34 deletions sagittal_brain.py

This file was deleted.

14 changes: 14 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from setuptools import setup, find_packages

setup(
name="sagittal_average",
version="0.1.0",
packages=find_packages(),
install_requires=['numpy'],
entry_points={
'console_scripts': [
'sagittal_average_run = sagittal_average.command:process']}
)

#The name of the package above is NOT the name that you type when doing `import ...`
#bc that name is the name of the folder which command.py is in ???