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

Change metadata merge logic #7

Draft
wants to merge 3 commits into
base: dev
Choose a base branch
from
Draft
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
6 changes: 3 additions & 3 deletions mofax/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1330,9 +1330,9 @@ def get_sample_r2(
views: Optional[Union[str, int, List[str], List[int]]] = None,
df: bool = True,
) -> pd.DataFrame:
findices, factors = self.__check_factors(factors, unique=True)
groups = self.__check_groups(groups)
views = self.__check_views(views)
findices, factors = self._check_factors(factors, unique=True)
groups = self._check_groups(groups)
views = self._check_views(views)

r2s = []
for view in views:
Expand Down
48 changes: 31 additions & 17 deletions mofax/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def _load_samples_metadata(model):
],
columns=["sample", "group"],
)

if "samples_metadata" in model.model:
if len(list(model.model["samples_metadata"][model.groups[0]].keys())) > 0:
_samples_metadata = pd.concat(
Expand All @@ -41,23 +42,36 @@ def _load_samples_metadata(model):
if "sample" in _samples_metadata.columns:
del _samples_metadata["sample"]

samples_metadata = pd.concat(
[
samples_metadata.reset_index(drop=True),
_samples_metadata.reset_index(drop=True),
],
axis=1,
)

# Decode objects as UTF-8 strings
for column in samples_metadata.columns:
if samples_metadata[column].dtype == "object":
try:
samples_metadata[column] = [
i.decode() for i in samples_metadata[column].values
]
except (UnicodeDecodeError, AttributeError):
pass
for df in [samples_metadata, _samples_metadata]:
for column in df.columns:
if df[column].dtype == "object":
try:
df[column] = [i.decode() for i in df[column].values]
except (UnicodeDecodeError, AttributeError):
pass

if "sample" in _samples_metadata.columns:

samples_metadata = pd.merge(
left=samples_metadata,
left_on="sample",
right=_samples_metadata,
right_on="index",
)

if "index" in samples_metadata.columns:
del samples_metadata["index"]

else:

samples_metadata = pd.concat(
[
samples_metadata.reset_index(drop=True),
_samples_metadata.reset_index(drop=True),
],
axis=1,
)

samples_metadata = samples_metadata.set_index("sample")
return samples_metadata
Expand Down Expand Up @@ -230,7 +244,7 @@ def _make_iterable(x):

def calculate_r2(Z, W, Y):
a = np.nansum((Y - Z.T.dot(W)) ** 2.0)
b = np.nansum(Y ** 2)
b = np.nansum(Y**2)
r2 = (1.0 - a / b) * 100
return r2

Expand Down