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

Fix write_gct too much strip #87

Merged
merged 4 commits into from
Jul 26, 2020
Merged
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
1 change: 0 additions & 1 deletion pycytominer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@
from .normalize import normalize
from .sparse_random_projection import sparse_random_projection
from .variance_threshold import variance_threshold
from .write_gct import write_gct
1 change: 1 addition & 0 deletions pycytominer/cyto_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@
Whiten,
RobustMAD,
)
from .write_gct import write_gct
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import csv
import numpy as np
import pandas as pd
from pycytominer.cyto_utils.features import infer_cp_features
from pycytominer.cyto_utils import infer_cp_features


def write_gct(
Expand Down Expand Up @@ -60,7 +60,7 @@ def write_gct(
.rename({"index": "id"}, axis="columns")
.transpose()
)
metadata_part.index = metadata_part.index.str.lstrip("Metadata_")
metadata_part.index = [x.replace("Metadata_", "") for x in metadata_part.index]

nrow_feature, ncol_features = feature_df.shape
_, ncol_metadata = metadata_df.shape
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import tempfile
import pytest
import pandas as pd
from pycytominer import write_gct
from pycytominer.cyto_utils import write_gct

# Build data to use in tests
data_replicate_df = pd.concat(
[
pd.DataFrame(
{
"Metadata_g": "a",
"Metadata_t": "t",
"Cells_x": [1, 1, -1],
"Cytoplasm_y": [5, 5, -5],
"Nuclei_z": [2, 2, -2],
Expand All @@ -19,6 +20,7 @@
pd.DataFrame(
{
"Metadata_g": "b",
"Metadata_t": "u",
"Cells_x": [1, 3, 5],
"Cytoplasm_y": [8, 3, 1],
"Nuclei_z": [5, -2, 1],
Expand Down Expand Up @@ -59,7 +61,7 @@ def test_write_gct():
gct_row_list.append(row)

assert gct_row_list[0] == ["#1.3"]
assert gct_row_list[1] == ["3", "6", "1", "2"]
assert gct_row_list[1] == ["3", "6", "1", "3"]
assert gct_row_list[2] == [
"id",
"cp_feature_name",
Expand All @@ -71,9 +73,10 @@ def test_write_gct():
"SAMPLE_5",
]
assert gct_row_list[3] == ["g", "nan", "a", "a", "a", "b", "b", "b"]
assert gct_row_list[4] == ["h", "nan", "c", "c", "c", "d", "d", "d"]
assert gct_row_list[5] == ["Cells_x", "Cells_x", "1", "1", "-1", "1", "3", "5"]
assert gct_row_list[6] == [
assert gct_row_list[4] == ["t", "nan", "t", "t", "t", "u", "u", "u"]
assert gct_row_list[5] == ["h", "nan", "c", "c", "c", "d", "d", "d"]
assert gct_row_list[6] == ["Cells_x", "Cells_x", "1", "1", "-1", "1", "3", "5"]
assert gct_row_list[7] == [
"Cytoplasm_y",
"Cytoplasm_y",
"5",
Expand All @@ -83,7 +86,7 @@ def test_write_gct():
"3",
"1",
]
assert gct_row_list[7] == ["Nuclei_z", "Nuclei_z", "2", "2", "-2", "5", "-2", "1"]
assert gct_row_list[8] == ["Nuclei_z", "Nuclei_z", "2", "2", "-2", "5", "-2", "1"]


def test_write_gct_infer_features():
Expand Down Expand Up @@ -131,7 +134,7 @@ def test_write_gct_with_feature_metadata():
"id": ["color", "shape"],
"Cells_x": ["blue", "triangle"],
"Cytoplasm_y": ["red", "square"],
"Nuclei_z": ["green", "oval"]
"Nuclei_z": ["green", "oval"],
}
).transpose()

Expand All @@ -150,7 +153,7 @@ def test_write_gct_with_feature_metadata():
gct_row_list.append(row)

assert gct_row_list[0] == ["#1.3"]
assert gct_row_list[1] == ["3", "6", "2", "2"]
assert gct_row_list[1] == ["3", "6", "2", "3"]
assert gct_row_list[2] == [
"id",
"color",
Expand All @@ -163,10 +166,41 @@ def test_write_gct_with_feature_metadata():
"SAMPLE_5",
]
assert gct_row_list[3] == ["g", "nan", "nan", "a", "a", "a", "b", "b", "b"]
assert gct_row_list[4] == ["h", "nan", "nan", "c", "c", "c", "d", "d", "d"]
assert gct_row_list[5] == ["Cells_x", "blue", "triangle", "1", "1", "-1", "1", "3", "5"]
assert gct_row_list[6] == ["Cytoplasm_y", "red", "square", "5", "5", "-5", "8", "3", "1"]
assert gct_row_list[7] == ["Nuclei_z", "green", "oval", "2", "2", "-2", "5", "-2", "1"]
assert gct_row_list[4] == ["t", "nan", "nan", "t", "t", "t", "u", "u", "u"]
assert gct_row_list[5] == ["h", "nan", "nan", "c", "c", "c", "d", "d", "d"]
assert gct_row_list[6] == [
"Cells_x",
"blue",
"triangle",
"1",
"1",
"-1",
"1",
"3",
"5",
]
assert gct_row_list[7] == [
"Cytoplasm_y",
"red",
"square",
"5",
"5",
"-5",
"8",
"3",
"1",
]
assert gct_row_list[8] == [
"Nuclei_z",
"green",
"oval",
"2",
"2",
"-2",
"5",
"-2",
"1",
]


def test_write_gct_assert_error():
Expand All @@ -176,7 +210,7 @@ def test_write_gct_assert_error():
{
"Cells_x": ["blue", "triangle"],
"Cytoplasm_y": ["red", "square"],
"Nuclei_z": ["green", "oval"]
"Nuclei_z": ["green", "oval"],
}
).transpose()

Expand All @@ -188,4 +222,4 @@ def test_write_gct_assert_error():
feature_metadata=feature_metadata,
version="#1.3",
)
assert "make sure feature metadata has row named 'id'" in str(ae.value)
assert "make sure feature metadata has row named 'id'" in str(ae.value)