Skip to content
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
40 changes: 26 additions & 14 deletions Python/shapeworks/shapeworks/network_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def __init__(self, project):
self.pvalue_of_interest = 0.05
self.pvalue_threshold = 0.05
self.num_iterations = 10000
self.target_group_values = None

def set_run_1dspm(self, run_1dspm):
self.flag_run_1dspm = run_1dspm
Expand All @@ -84,6 +85,10 @@ def set_target_feature(self, target_feature):
def set_target_group(self, target_group):
self.target_group = target_group

def set_target_group_values(self, value1, value2):
"""Select the two group values to compare (required when the group has more than two values)"""
self.target_group_values = [value1, value2]

def set_model_type(self, model_type):
self.flag_model = model_type

Expand Down Expand Up @@ -139,12 +144,22 @@ def run(self):

target_group = self.target_group
group_values = project.get_group_values(target_group)
group = map(lambda x: x.get_group_value(target_group), project.get_subjects())
group = np.array(list(group))
for i in range(len(group_values)):
group[group == group_values[i]] = i
group = group.astype(int)
group.mean()
if self.target_group_values is not None:
selected_values = sorted(self.target_group_values)
unknown = [v for v in selected_values if v not in group_values]
if unknown or selected_values[0] == selected_values[1]:
raise ValueError(f"Network analysis needs two different values of group '{target_group}', "
f"but got {self.target_group_values}. Available values: {group_values}")
group_values = selected_values
elif len(group_values) > 2:
raise ValueError(f"Group '{target_group}' has {len(group_values)} values: {group_values}. "
"Select the two to compare.")

# analyze only the subjects in the two compared groups
subject_values = [x.get_group_value(target_group) for x in project.get_subjects()]
selected_subjects = [s for s, v in enumerate(subject_values) if v in group_values]
group = np.array([group_values.index(subject_values[s]) for s in selected_subjects], dtype=int)
num_subjects = len(selected_subjects)

group_sizes = [int(np.sum(group == i)) for i in range(len(group_values))]
if len(group_values) < 2 or min(group_sizes[:2]) < 2:
Expand All @@ -156,18 +171,15 @@ def run(self):
f"({sizes_text} subjects), {num_pts} particles")
sw_message(f"Settings: {self.num_iterations} permutations, p-value of interest {self.pvalue_of_interest}, "
f"p-value threshold {self.pvalue_threshold}")
if len(group_values) > 2:
sw_message(f"Warning: group '{target_group}' has {len(group_values)} values, "
f"comparing only '{group_values[0]}' and '{group_values[1]}'")

all_data = np.ndarray(
(num_pts, timepoints, num_subjects, len(activities))); # particles,time,subjects,activity

activity = 0
subjects = project.get_subjects()
sw_message(f"Loading feature values for {num_subjects} subjects")
for s in range(num_subjects):
sw_progress(0.1 * s / num_subjects, "Loading feature values")
for index, s in enumerate(selected_subjects):
sw_progress(0.1 * index / num_subjects, "Loading feature values")
features = analyze.get_subject_features(s, self.target_feature)
if features.size != num_pts:
raise ValueError(
Expand All @@ -176,7 +188,7 @@ def run(self):
f"subject's mesh has a point data array named '{self.target_feature}'.")
# reshape into a matrix
features = features.reshape(-1, 1)
all_data[:, :, s, activity] = features
all_data[:, :, index, activity] = features

# analyze only particles whose values vary across subjects (zero-variance particles
# produce undefined test statistics)
Expand Down Expand Up @@ -339,7 +351,7 @@ def run(self):
# number of categories for analysis
ng = len(np.unique(group))
na = len(activities)
ns = len(subjects)
ns = len(group)

if os.path.exists(result_path) is not True:
os.mkdir(result_path)
Expand Down Expand Up @@ -803,7 +815,7 @@ def run(self):

# In[19]:

poi = 0.05 # p-value of interest
poi = pthresh[0]
# save data to data frame
if flag_model == 'spatiotemporal': # temporal anova analysis
clustdf_size = pd.DataFrame({
Expand Down
2 changes: 2 additions & 0 deletions Studio/Analysis/AnalysisTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,8 @@ void AnalysisTool::network_analysis_clicked() {
network_analysis_job_ =
QSharedPointer<NetworkAnalysisJob>::create(session_->get_project(), ui_->group_combo->currentText().toStdString(),
ui_->network_feature->currentText().toStdString());
network_analysis_job_->set_target_group_values(ui_->group_left->currentText().toStdString(),
ui_->group_right->currentText().toStdString());
network_analysis_job_->set_pvalue_of_interest(ui_->network_pvalue_of_interest->text().toDouble());
network_analysis_job_->set_pvalue_threshold(ui_->network_pvalue_threshold->text().toDouble());
network_analysis_job_->set_num_iterations(ui_->network_iterations->text().toInt());
Expand Down
7 changes: 7 additions & 0 deletions Studio/Job/NetworkAnalysisJob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ void NetworkAnalysisJob::run() {
py::type network_analysis_class = sw.attr("NetworkAnalysis");
py::object network_analysis = network_analysis_class(project_);
network_analysis.attr("set_target_group")(target_group_);
network_analysis.attr("set_target_group_values")(target_group_value1_, target_group_value2_);
network_analysis.attr("set_target_feature")(target_feature_);
network_analysis.attr("set_pvalue_of_interest")(pvalue_of_interest_);
network_analysis.attr("set_pvalue_threshold")(pvalue_threshold_);
Expand Down Expand Up @@ -66,6 +67,12 @@ Eigen::VectorXd NetworkAnalysisJob::get_tvalues() { return tvalues_; }
//---------------------------------------------------------------------------
Eigen::VectorXd NetworkAnalysisJob::get_spm_values() { return spm_values_; }

//---------------------------------------------------------------------------
void NetworkAnalysisJob::set_target_group_values(std::string value1, std::string value2) {
target_group_value1_ = value1;
target_group_value2_ = value2;
}

//---------------------------------------------------------------------------
void NetworkAnalysisJob::set_pvalue_of_interest(double pvalue_of_interest) { pvalue_of_interest_ = pvalue_of_interest; }

Expand Down
4 changes: 4 additions & 0 deletions Studio/Job/NetworkAnalysisJob.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class NetworkAnalysisJob : public Job {
void run() override;
QString name() override;

//! Set the two group values to compare
void set_target_group_values(std::string value1, std::string value2);
void set_pvalue_of_interest(double pvalue_of_interest);
void set_pvalue_threshold(double pvalue_threshold);
void set_num_iterations(int num_iterations);
Expand All @@ -26,6 +28,8 @@ class NetworkAnalysisJob : public Job {
Eigen::VectorXd tvalues_;
Eigen::VectorXd spm_values_;
std::string target_group_;
std::string target_group_value1_;
std::string target_group_value2_;
std::string target_feature_;

double pvalue_of_interest_ = 0;
Expand Down
Loading