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

Code overlaps in computing gradients of objective functions removed. #1284

Merged
merged 2 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
42 changes: 8 additions & 34 deletions src/xSTIR/cSTIR/cstir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1198,26 +1198,13 @@ void*
cSTIR_objectiveFunctionGradient(void* ptr_f, void* ptr_i, int subset)
{
try {
ObjectiveFunction3DF& fun = objectFromHandle< ObjectiveFunction3DF>(ptr_f);
xSTIR_ObjFun3DF& fun = objectFromHandle<xSTIR_ObjFun3DF>(ptr_f);
STIRImageData& id = objectFromHandle<STIRImageData>(ptr_i);
evgueni-ovtchinnikov marked this conversation as resolved.
Show resolved Hide resolved
Image3DF& image = id.data();
STIRImageData* ptr_id = new STIRImageData(image);
shared_ptr<STIRImageData> sptr(ptr_id);
Image3DF& grad = sptr->data();
if (subset >= 0)
fun.compute_sub_gradient(grad, image, subset);
else {
int nsub = fun.get_num_subsets();
grad.fill(0.0);
STIRImageData* ptr_id = new STIRImageData(image);
shared_ptr<STIRImageData> sptr_sub(ptr_id);
Image3DF& subgrad = sptr_sub->data();
for (int sub = 0; sub < nsub; sub++) {
fun.compute_sub_gradient(subgrad, image, sub);
grad += subgrad;
}
}
return newObjectHandle(sptr);
STIRImageData* ptr_gd = new STIRImageData(id);
shared_ptr<STIRImageData> sptr_gd(ptr_gd);
STIRImageData& gd = *sptr_gd;
fun.compute_gradient(id, subset, gd);
evgueni-ovtchinnikov marked this conversation as resolved.
Show resolved Hide resolved
return newObjectHandle(sptr_gd);
}
CATCH;
}
Expand All @@ -1227,23 +1214,10 @@ void*
cSTIR_computeObjectiveFunctionGradient(void* ptr_f, void* ptr_i, int subset, void* ptr_g)
{
try {
ObjectiveFunction3DF& fun = objectFromHandle< ObjectiveFunction3DF>(ptr_f);
xSTIR_ObjFun3DF& fun = objectFromHandle<xSTIR_ObjFun3DF>(ptr_f);
STIRImageData& id = objectFromHandle<STIRImageData>(ptr_i);
STIRImageData& gd = objectFromHandle<STIRImageData>(ptr_g);
Image3DF& image = id.data();
Image3DF& grad = gd.data();
if (subset >= 0)
fun.compute_sub_gradient(grad, image, subset);
else {
int nsub = fun.get_num_subsets();
grad.fill(0.0);
shared_ptr<STIRImageData> sptr_sub(new STIRImageData(image));
Image3DF& subgrad = sptr_sub->data();
for (int sub = 0; sub < nsub; sub++) {
fun.compute_sub_gradient(subgrad, image, sub);
grad += subgrad;
}
}
fun.compute_gradient(id, subset, gd);
return (void*) new DataHandle;
}
CATCH;
Expand Down
29 changes: 21 additions & 8 deletions src/xSTIR/cSTIR/include/sirf/STIR/stir_x.h
Original file line number Diff line number Diff line change
Expand Up @@ -1105,11 +1105,28 @@ The actual algorithm is described in
}
};

class xSTIR_GeneralisedObjectiveFunction3DF :
public stir::GeneralisedObjectiveFunction < Image3DF > {
class xSTIR_GeneralisedObjectiveFunction3DF : public ObjectiveFunction3DF {
public:
void compute_gradient(const STIRImageData& id, int subset, STIRImageData& gd)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a doxygen string please.

By the way, does SIRF not use the same convention as STIR, where the output arg gd is placed first?

Copy link
Contributor Author

@evgueni-ovtchinnikov evgueni-ovtchinnikov Aug 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doxygen: done

STIR convention: no, SIRF does not use it

{
const Image3DF& image = id.data();
Image3DF& grad = gd.data();
if (subset >= 0)
compute_sub_gradient(grad, image, subset);
else {
int nsub = get_num_subsets();
grad.fill(0.0);
shared_ptr<STIRImageData> sptr_sub(new STIRImageData(image));
Image3DF& subgrad = sptr_sub->data();
for (int sub = 0; sub < nsub; sub++) {
compute_sub_gradient(subgrad, image, sub);
grad += subgrad;
}
}
}

void multiply_with_Hessian(Image3DF& output, const Image3DF& curr_image_est,
const Image3DF& input, const int subset) const
const Image3DF& input, const int subset) const
{
output.fill(0.0);
if (subset >= 0)
Expand All @@ -1120,13 +1137,9 @@ The actual algorithm is described in
}
}
}

// bool post_process() {
// return post_processing();
// }
};

//typedef xSTIR_GeneralisedObjectiveFunction3DF ObjectiveFunction3DF;
typedef xSTIR_GeneralisedObjectiveFunction3DF xSTIR_ObjFun3DF;

class xSTIR_PoissonLogLikelihoodWithLinearModelForMeanAndProjData3DF :
public stir::PoissonLogLikelihoodWithLinearModelForMeanAndProjData < Image3DF > {
Expand Down
Loading