Skip to content

Commit

Permalink
Minor modifications to prefit
Browse files Browse the repository at this point in the history
  • Loading branch information
rlaplaza committed Jan 13, 2025
1 parent 126cb99 commit d9e4664
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 10 deletions.
12 changes: 10 additions & 2 deletions navicat_spock/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ def augment(df, level, verb=0):
if verb > 0:
print(f"Doing level 1 feature augmentation...")
for i in x_full.keys():
if "TARGET" in i.upper():
continue
inv = f"1/{i}"
pow2 = f"{i}^2"
# pow3 = f"{i}^3"
Expand All @@ -159,6 +161,8 @@ def augment(df, level, verb=0):
if verb > 0:
print(f"Doing level 2 feature augmentation...")
for i, j in zip(x_full.keys(), x_full.keys()[1:]):
if "TARGET" in i.upper() or "TARGET" in j.upper():
continue
prod = f"{i}x{j}"
div12 = f"{i}/{j}"
div21 = f"{j}/{i}"
Expand All @@ -176,6 +180,8 @@ def augment(df, level, verb=0):
continue
i = x_full.keys()[i]
j = x_full.keys()[j]
if "TARGET" in i.upper() or "TARGET" in j.upper():
continue
prod = f"{i}x{j}"
div12 = f"{i}/{j}"
div21 = f"{j}/{i}"
Expand Down Expand Up @@ -225,7 +231,9 @@ def __exit__(self, *args):

def namefixer(filename):
assert isinstance(filename, str)
return re.sub("[^a-zA-Z0-9 \n\.]", "_", filename).replace(" ", "_")
return (
re.sub("[^a-zA-Z0-9 \n\.]", "_", filename).replace(" ", "_").replace("__", "_")
)


def normalize(a, axis=-1, order=2):
Expand Down Expand Up @@ -367,7 +375,7 @@ def processargs(arguments):
epilog="Remember to cite the spock paper (when its out!) \n \n - and enjoy!",
)
vbuilder.add_argument(
"-version", "--version", action="version", version="%(prog)s 0.0.4"
"-version", "--version", action="version", version="%(prog)s 0.0.5"
)
vbuilder.add_argument(
"-i",
Expand Down
17 changes: 13 additions & 4 deletions navicat_spock/plotting2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def plot_2d(
breakpoints=None,
estimates=None,
plotmode=1,
return_value=True,
save_fig=True,
):
# Labels and key
Expand Down Expand Up @@ -132,7 +133,11 @@ def plot_2d(
plt.savefig(filename)
if os.name != "posix" and "DISPLAY" in os.environ:
plt.show()
return fig, ax
if return_value:
return fig, ax
else:
plt.close()
return None, None


def plot_and_save(
Expand Down Expand Up @@ -193,6 +198,7 @@ def plot_and_save(
estimates=estimates,
plotmode=plotmode,
filename=f"{namefixer(tags[idx].strip())}_volcano.png",
return_value=return_value,
save_fig=save_fig,
)

Expand All @@ -204,10 +210,13 @@ def plot_and_save(
zdata = list(zip(xint, yint))
csvname = f"{namefixer(tags[idx].strip())}_volcano.csv"
np.savetxt(
csvname, zdata, fmt="%.4e", delimiter=",", header="{tags[idx]},{tags[tidx]}"
csvname,
zdata,
fmt="%.4e",
delimiter=",",
header="f{tags[idx]},{tags[tidx]}",
)
if return_value:
return fig, ax
else:
plt.close()
return None
return None, None
25 changes: 23 additions & 2 deletions navicat_spock/spock.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ def run_spock_from_args(
fig, ax = plt.subplots(
frameon=False, figsize=[4.2, 3], dpi=300, constrained_layout=True
)
if prefit:
fig_r, ax_r = plt.subplots(
frameon=False, figsize=[4.2, 3], dpi=300, constrained_layout=True
)

# Atttempts to group data points based on shared characters in names.
if setcbms:
Expand Down Expand Up @@ -211,16 +215,25 @@ def run_spock_from_args(
print(
f"Prefitting volcano with {n} breakpoints and descriptor index {idx}: {tags[idx]}, for which a BIC of {min_bic} was obtained."
)
_, _ = plot_and_save(
if fig_r is None and ax_r is None:
fig_r, ax_r = plt.subplots(
frameon=False,
figsize=[4.2, 3],
dpi=300,
constrained_layout=True,
)
fig_r, ax_r = plot_and_save(
pw_fit,
tags,
idx,
tidx,
cb,
ms,
plotmode=plotmode,
fig=fig,
ax=ax,
plotmode=plotmode,
save_fig=save_fig,
save_csv=save_csv,
return_value=False,
)

Expand Down Expand Up @@ -318,6 +331,13 @@ def run_spock_from_args(
pw_fit.best_muggeo = msels[idx - 1].models[n - 1].best_muggeo
if verb > 2:
pw_fit.summary()
if fig is None and ax is None:
fig, ax = plt.subplots(
frameon=False,
figsize=[4.2, 3],
dpi=300,
constrained_layout=True,
)
# Plot the data, fit, breakpoints and confidence intervals
fig, ax = plot_and_save(
pw_fit,
Expand All @@ -331,6 +351,7 @@ def run_spock_from_args(
plotmode=plotmode,
save_fig=save_fig,
save_csv=save_csv,
return_value=True,
)
return fig, ax
else:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "navicat-spock"
version = "0.0.4"
version = "0.0.5"
authors = [
{ name="R. Laplaza", email="rlaplaza@duck.com" },
]
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
setup(
name="spock",
packages=["navicat_spock"],
version="0.0.4",
version="0.0.5",
description="Volcano Plot fitting tool",
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down

0 comments on commit d9e4664

Please sign in to comment.