Skip to content

Commit

Permalink
packaging
Browse files Browse the repository at this point in the history
  • Loading branch information
tdincer committed Jan 20, 2023
1 parent d82d78b commit f389816
Show file tree
Hide file tree
Showing 12 changed files with 547 additions and 0 deletions.
116 changes: 116 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# User data
.DS_Store
temp*

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
.idea/

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy
scratchpaper.*

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# dotenv
.env

# virtualenv
.venv
venv/
ENV/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/

# datajoint
dj_local_conf.json
dj_local_conf_old.json

# emacs
**/*~
**/#*#
**/.#*
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Changelog

Observes [Semantic Versioning](https://semver.org/spec/v2.0.0.html) standard and [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) convention.

## [0.1.0] - YYYY-MM-DD
+ First release

[0.4.2]: https://github.com/datajoint/compress-multiphoton/releases/tag/0.1.0
133 changes: 133 additions & 0 deletions CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@

# Contributor Covenant Code of Conduct

## Our Pledge

We as members, contributors, and leaders pledge to make participation in our
community a harassment-free experience for everyone, regardless of age, body
size, visible or invisible disability, ethnicity, sex characteristics, gender
identity and expression, level of experience, education, socio-economic status,
nationality, personal appearance, race, caste, color, religion, or sexual
identity and orientation.

We pledge to act and interact in ways that contribute to an open, welcoming,
diverse, inclusive, and healthy community.

## Our Standards

Examples of behavior that contributes to a positive environment for our
community include:

* Demonstrating empathy and kindness toward other people
* Being respectful of differing opinions, viewpoints, and experiences
* Giving and gracefully accepting constructive feedback
* Accepting responsibility and apologizing to those affected by our mistakes,
and learning from the experience
* Focusing on what is best not just for us as individuals, but for the overall
community

Examples of unacceptable behavior include:

* The use of sexualized language or imagery, and sexual attention or advances of
any kind
* Trolling, insulting or derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or email address,
without their explicit permission
* Other conduct which could reasonably be considered inappropriate in a
professional setting

## Enforcement Responsibilities

Community leaders are responsible for clarifying and enforcing our standards of
acceptable behavior and will take appropriate and fair corrective action in
response to any behavior that they deem inappropriate, threatening, offensive,
or harmful.

Community leaders have the right and responsibility to remove, edit, or reject
comments, commits, code, wiki edits, issues, and other contributions that are
not aligned to this Code of Conduct, and will communicate reasons for moderation
decisions when appropriate.

## Scope

This Code of Conduct applies within all community spaces, and also applies when
an individual is officially representing the community in public spaces.
Examples of representing our community include using an official e-mail address,
posting via an official social media account, or acting as an appointed
representative at an online or offline event.

## Enforcement

Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported to the community leaders responsible for enforcement at
[[email protected]](mailto:[email protected]).
All complaints will be reviewed and investigated promptly and fairly.

All community leaders are obligated to respect the privacy and security of the
reporter of any incident.

## Enforcement Guidelines

Community leaders will follow these Community Impact Guidelines in determining
the consequences for any action they deem in violation of this Code of Conduct:

### 1. Correction

**Community Impact**: Use of inappropriate language or other behavior deemed
unprofessional or unwelcome in the community.

**Consequence**: A private, written warning from community leaders, providing
clarity around the nature of the violation and an explanation of why the
behavior was inappropriate. A public apology may be requested.

### 2. Warning

**Community Impact**: A violation through a single incident or series of
actions.

**Consequence**: A warning with consequences for continued behavior. No
interaction with the people involved, including unsolicited interaction with
those enforcing the Code of Conduct, for a specified period of time. This
includes avoiding interactions in community spaces as well as external channels
like social media. Violating these terms may lead to a temporary or permanent
ban.

### 3. Temporary Ban

**Community Impact**: A serious violation of community standards, including
sustained inappropriate behavior.

**Consequence**: A temporary ban from any sort of interaction or public
communication with the community for a specified period of time. No public or
private interaction with the people involved, including unsolicited interaction
with those enforcing the Code of Conduct, is allowed during this period.
Violating these terms may lead to a permanent ban.

### 4. Permanent Ban

**Community Impact**: Demonstrating a pattern of violation of community
standards, including sustained inappropriate behavior, harassment of an
individual, or aggression toward or disparagement of classes of individuals.

**Consequence**: A permanent ban from any sort of public interaction within the
community.

## Attribution

This Code of Conduct is adapted from the [Contributor Covenant][homepage],
version 2.1, available at
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].

Community Impact Guidelines were inspired by
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].

For answers to common questions about this code of conduct, see the FAQ at
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
[https://www.contributor-covenant.org/translations][translations].

[homepage]: https://www.contributor-covenant.org
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
[Mozilla CoC]: https://github.com/mozilla/diversity
[FAQ]: https://www.contributor-covenant.org/faq
[translations]: https://www.contributor-covenant.org/translations
Empty file added LICENSE
Empty file.
4 changes: 4 additions & 0 deletions compress_multiphoton/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from .compression import compute_quantal_size
from .analysis import analyze

__all__ = ["compute_quantal_size", "analyze"]
44 changes: 44 additions & 0 deletions compress_multiphoton/analysis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import numpy as np
from time import time
import plotly.graph_objects as go
from .compression import compute_quantal_size


def timer_func(func):
# This function shows the execution time of the function object passed
def wrap_func(*args, **kwargs):
t1 = time()
result = func(*args, **kwargs)
t2 = time()
print(f"Function {func.__name__!r} executed in {(t2-t1):.4f}s")
return result

return wrap_func


def analyze(scan):
(
model,
min_intensity,
max_intensity,
unique_pixels,
unique_variances,
quantal_size,
zero_level,
) = compute_quantal_size(scan)

print(f"Quantal size: {quantal_size}")
print(f"Intercept: {zero_level}")

x = np.arange(len(unique_pixels))
f = go.Figure(go.Scatter(x=unique_pixels, y=unique_variances, mode="markers"))
f.add_trace(go.Scatter(x=x, y=model.predict(x.reshape(-1, 1))))
f.update_layout(
width=700,
height=700,
yaxis=dict(title_text="Variance"),
xaxis=dict(title_text="Pixel Intensity"),
showlegend=False,
)

return f
51 changes: 51 additions & 0 deletions compress_multiphoton/compression.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import numpy as np
from sklearn.linear_model import TheilSenRegressor


def compute_quantal_size(scan):
# Set some params
num_frames = scan.shape[2]
# min_count = num_frames * 0.1 # pixel values with fewer appearances will be ignored
min_count = scan.min()
max_acceptable_intensity = 3000 # pixel values higher than this will be ignored
# max_acceptable_intensity = 100000

# Make sure field is at least 32 bytes (int16 overflows if summed to itself)
scan = scan.astype(np.float32, copy=False)

# Create pixel values at each position in field
eps = 1e-4 # needed for np.round to not be biased towards even numbers (0.5 -> 1, 1.5 -> 2, 2.5 -> 3, etc.)
pixels = np.round((scan[:, :, :-1] + scan[:, :, 1:]) / 2 + eps)

pixels = pixels.astype(np.int16 if np.max(abs(pixels)) < 2**15 else np.int32)

# Compute a good range of pixel values (common, not too bright values)
unique_pixels, counts = np.unique(pixels, return_counts=True)
min_intensity = min(unique_pixels[counts > min_count])
max_intensity = max(unique_pixels[counts > min_count])
max_acceptable_intensity = min(max_intensity, max_acceptable_intensity)
pixels_mask = np.logical_and(pixels >= min_intensity, pixels <= max_acceptable_intensity)

# Select pixels in good range
pixels = pixels[pixels_mask]
unique_pixels, counts = np.unique(pixels, return_counts=True)

# Compute noise variance
variances = ((scan[:, :, :-1] - scan[:, :, 1:]) ** 2 / 2)[pixels_mask]
pixels -= min_intensity
variance_sum = np.zeros(len(unique_pixels)) # sum of variances per pixel value
for i in range(0, len(pixels), int(1e8)): # chunk it for memory efficiency
variance_sum += np.bincount(
pixels[i : i + int(1e8)], weights=variances[i : i + int(1e8)], minlength=np.ptp(unique_pixels) + 1
)[unique_pixels - min_intensity]
unique_variances = variance_sum / counts # average variance per intensity

# Compute quantal size (by fitting a linear regressor to predict the variance from intensity)
X = unique_pixels.reshape(-1, 1)
y = unique_variances
model = TheilSenRegressor() # robust regression
model.fit(X, y)
quantal_size = model.coef_[0]
zero_level = -model.intercept_ / model.coef_[0]

return (model, min_intensity, max_intensity, unique_pixels, unique_variances, quantal_size, zero_level)
3 changes: 3 additions & 0 deletions compress_multiphoton/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""Package metadata"""

__version__ = "0.1.0"
Loading

0 comments on commit f389816

Please sign in to comment.