Skip to content

Commit

Permalink
Merge pull request #113 from rs-station/stats-refactor
Browse files Browse the repository at this point in the history
resolves #107
  • Loading branch information
kmdalton authored Jun 24, 2023
2 parents 11ce0f4 + 4f47e0c commit 5a1dbf5
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 87 deletions.
29 changes: 14 additions & 15 deletions careless/stats/ccanom.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import seaborn as sns


class ArgumentParser(argparse.ArgumentParser):
from careless.stats.parser import BaseParser
class ArgumentParser(BaseParser):
def __init__(self):
super().__init__(
formatter_class=argparse.RawTextHelpFormatter,
description=__doc__
)

Expand All @@ -27,24 +27,18 @@ def __init__(self):
"--method",
default="spearman",
choices=["spearman", "pearson"],
help=("Method for computing correlation coefficient (spearman or pearson)"),
help="Method for computing correlation coefficient (spearman or pearson)",
)

self.add_argument(
"-b",
"--bins",
default=10,
type=int,
help=("Number of resolution bins to use, the default is 10."),
help="Number of resolution bins to use, the default is 10.",
)

self.add_argument(
"-o",
"--output",
type=str,
default=None,
help=("Optionally save CCanom values to this file in csv format."),
)




Expand Down Expand Up @@ -92,7 +86,7 @@ def analyze_ccanom_mtz(mtzpath, bins=10, return_labels=True, method="spearman"):
return result


def run_analysis(args, show=True):
def run_analysis(args):
results = []
labels = None
for m in args.mtz:
Expand All @@ -114,6 +108,8 @@ def run_analysis(args, show=True):

if args.output is not None:
results.to_csv(args.output)
else:
print(results.to_string())

sns.lineplot(
data=results, x="bin", y="CCanom", hue="filename", palette="Dark2"
Expand All @@ -123,11 +119,14 @@ def run_analysis(args, show=True):
plt.xlabel("Resolution ($\mathrm{\AA}$)")
plt.grid(which='both', axis='both', ls='dashdot')
plt.tight_layout()
if show:
print(results.to_string())

if args.image is not None:
plt.savefig(args.image)

if args.show:
plt.show()

def main():
parser = ArgumentParser().parse_args()
run_analysis(parser, True)
run_analysis(parser)

26 changes: 11 additions & 15 deletions careless/stats/cchalf.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import seaborn as sns


class ArgumentParser(argparse.ArgumentParser):
from careless.stats.parser import BaseParser
class ArgumentParser(BaseParser):
def __init__(self):
super().__init__(
formatter_class=argparse.RawTextHelpFormatter,
description=__doc__
)

Expand All @@ -37,15 +37,6 @@ def __init__(self):
help=("Number of resolution bins to use, the default is 10."),
)

self.add_argument(
"-o",
"--output",
type=str,
default=None,
help=("Optionally save CChalf values to this file in csv format."),
)


def make_halves_cchalf(mtz, bins=10):
"""Construct half-datasets for computing CChalf"""

Expand Down Expand Up @@ -87,7 +78,7 @@ def analyze_cchalf_mtz(mtzpath, bins=10, return_labels=True, method="spearman"):
return result


def run_analysis(args, show=True):
def run_analysis(args):
results = []
labels = None
for m in args.mtz:
Expand All @@ -110,6 +101,8 @@ def run_analysis(args, show=True):

if args.output is not None:
results.to_csv(args.output)
else:
print(results.to_string())

sns.lineplot(
data=results, x="bin", y="CChalf", hue="filename", palette="viridis"
Expand All @@ -119,12 +112,15 @@ def run_analysis(args, show=True):
plt.xlabel("Resolution ($\mathrm{\AA}$)")
plt.grid(which='both', axis='both', ls='dashdot')
plt.tight_layout()
if show:
print(results.to_string())

if args.image is not None:
plt.savefig(args.image)

if args.show:
plt.show()


def main():
parser = ArgumentParser().parse_args()
run_analysis(parser, True)
run_analysis(parser)

27 changes: 9 additions & 18 deletions careless/stats/ccpred.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
import seaborn as sns


class ArgumentParser(argparse.ArgumentParser):
from careless.stats.parser import BaseParser
class ArgumentParser(BaseParser):
def __init__(self):
super().__init__(
formatter_class=argparse.RawTextHelpFormatter,
description=__doc__
)

Expand Down Expand Up @@ -48,20 +48,6 @@ def __init__(self):
help="Pool all prediction mtz files into a single calculation rather than treating each file individually.",
)

self.add_argument(
"-o",
"--output",
type=str,
default=None,
help="Optionally save CCpred values to this file in csv format.",
)

self.add_argument(
"--plot",
action="store_true",
help="Make a plot of the results with seaborn and display it using matplotlib.",
)


def compute_ccpred(
dataset, overall=False, bins=10, return_labels=True, method="spearman"
Expand Down Expand Up @@ -121,6 +107,8 @@ def run_analysis(args):

if args.output is not None:
results.to_csv(args.output)
else:
print(results.to_string())

plot_kwargs = {
'data' : results,
Expand All @@ -143,8 +131,11 @@ def run_analysis(args):
plt.xlabel("Resolution ($\mathrm{\AA}$)")
plt.grid(which='both', axis='both', ls='dashdot')
plt.tight_layout()
print(results.to_string())
if args.plot:

if args.image is not None:
plt.savefig(args.image)

if args.show:
plt.show()

def main():
Expand Down
27 changes: 11 additions & 16 deletions careless/stats/completeness.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
import seaborn as sns
import numpy as np


class ArgumentParser(argparse.ArgumentParser):
from careless.stats.parser import BaseParser
class ArgumentParser(BaseParser):
def __init__(self):
super().__init__(
formatter_class=argparse.RawTextHelpFormatter,
description=__doc__
)

Expand All @@ -29,20 +28,14 @@ def __init__(self):
help=("Number of resolution bins to use, the default is 10."),
)

self.add_argument(
"-o",
"--output",
type=str,
default=None,
help=("Optionally save completeness values to this file in csv format."),
)

def run_analysis(args, show=True):
def run_analysis(args):
ds = rs.read_mtz(args.mtz)
results = rs.stats.compute_completeness(ds, bins=args.bins)

if args.output is not None:
results.to_csv(args.output)
else:
print(results.to_string())

#Move overall to the beginning
results = results.iloc[np.roll(np.arange(len(results)), 1)]
Expand All @@ -60,12 +53,14 @@ def run_analysis(args, show=True):
plt.xlabel("Resolution ($\mathrm{\AA}$)")
plt.grid(which='both', axis='both', ls='dashdot')
plt.tight_layout()
if show:
print(results.to_string())
plt.show()

if args.image is not None:
plt.savefig(args.image)

if args.show:
plt.show()

def main():
parser = ArgumentParser().parse_args()
run_analysis(parser, True)
run_analysis(parser)

48 changes: 48 additions & 0 deletions careless/stats/parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""
Compute CCpred from careless output.
"""
import argparse
import numpy as np
import reciprocalspaceship as rs
import gemmi

import matplotlib.pyplot as plt
import seaborn as sns


class BaseParser(argparse.ArgumentParser):
def __init__(self, **kwargs):
super().__init__(
formatter_class=argparse.RawTextHelpFormatter,
**kwargs
)

self.add_argument(
"-s",
"--show",
action="store_true",
help="Make a plot of the results and display it using matplotlib.",
)

self.add_argument(
"-i",
"--image",
type=str,
default=None,
help="Make a plot of the results and save it to this filename. "
"The filetype will be determined from the filename. "
"Any filetype supported by your matplotlib version will be available.",
)

self.add_argument(
"-o",
"--output",
type=str,
default=None,
help="Optionally save results to this file in csv format instead of printing "
"them to the terminal.",
)



25 changes: 11 additions & 14 deletions careless/stats/rsplit.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
import numpy as np


class ArgumentParser(argparse.ArgumentParser):
from careless.stats.parser import BaseParser
class ArgumentParser(BaseParser):
def __init__(self):
super().__init__(
formatter_class=argparse.RawTextHelpFormatter,
description=__doc__
)

Expand All @@ -37,14 +37,6 @@ def __init__(self):
help=("Optionally use intensities instead of structure factors to facilitate comparisons with other softwares."),
)

self.add_argument(
"-o",
"--output",
type=str,
default=None,
help=("Optionally save Rsplit values to this file in csv format."),
)


def make_halves_cchalf(mtz, bins=10):
"""Construct half-datasets for computing Rsplit"""
Expand Down Expand Up @@ -98,7 +90,7 @@ def analyze_cchalf_mtz(mtzpath, bins=10, return_labels=True, keys=("F1", "F2")):
return result


def run_analysis(args, show=True):
def run_analysis(args):
results = []
labels = None

Expand Down Expand Up @@ -127,6 +119,8 @@ def run_analysis(args, show=True):

if args.output is not None:
results.to_csv(args.output)
else:
print(results.to_string())

sns.lineplot(
data=results, x="bin", y="Rsplit", hue="filename", palette="Dark2"
Expand All @@ -136,12 +130,15 @@ def run_analysis(args, show=True):
plt.xlabel("Resolution ($\mathrm{\AA}$)")
plt.grid(which='both', axis='both', ls='dashdot')
plt.tight_layout()
if show:
print(results.to_string())

if args.image is not None:
plt.savefig(args.image)

if args.show:
plt.show()


def main():
parser = ArgumentParser().parse_args()
run_analysis(parser, True)
run_analysis(parser)

Loading

0 comments on commit 5a1dbf5

Please sign in to comment.