Skip to content

Commit

Permalink
Fixed Blush issue with new line character in Python return string.
Browse files Browse the repository at this point in the history
  • Loading branch information
dkimanius committed Aug 23, 2023
1 parent 607ddce commit a75ccec
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 37 deletions.
70 changes: 37 additions & 33 deletions cmake/FindCondaPython.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -38,38 +38,42 @@ if (PYTHON_EXE_PATH STREQUAL "")

find_program(CONDA_EXECUTABLE conda)

message(STATUS "Found Conda executable: ${CONDA_EXECUTABLE}")

# Run the conda command and capture its output
execute_process(COMMAND ${CONDA_EXECUTABLE} config --show envs_dirs
OUTPUT_VARIABLE CONDA_ENVS_OUTPUT
OUTPUT_STRIP_TRAILING_WHITESPACE
RESULT_VARIABLE CONDA_RESULT)

# Check if the conda command was successful
if(CONDA_RESULT EQUAL 0)
# Split the output into lines
string(REPLACE "\n" ";" CONDA_ENVS_LIST ${CONDA_ENVS_OUTPUT})

# Loop through the list of paths
foreach(path ${CONDA_ENVS_LIST})
string(STRIP "${path}" path)
string(REPLACE " " ";" path "${path}")
list(GET path -1 path)
set(path "${path}/${CONDA_ENV_NAME}/bin/python")
if(EXISTS "${path}")
set(PYTHON_EXE_PATH "${path}")
message(STATUS "Found Conda environment (name: ${CONDA_ENV_NAME}) and its Python executable.")
break() # Exit the loop once a valid path is found
endif()
endforeach()
endif()
if (PYTHON_EXE_PATH STREQUAL "")
message(
WARNING
"Failed to find path to Conda environment (name: ${CONDA_ENV_NAME}) and its Python executable.\n"
"You can specify it directly with -DPYTHON_EXE_PATH=<path>\n"
)
if(CONDA_EXECUTABLE)
message(STATUS "Found Conda executable: ${CONDA_EXECUTABLE}")

# Run the conda command and capture its output
execute_process(COMMAND ${CONDA_EXECUTABLE} config --show envs_dirs
OUTPUT_VARIABLE CONDA_ENVS_OUTPUT
OUTPUT_STRIP_TRAILING_WHITESPACE
RESULT_VARIABLE CONDA_RESULT)

# Check if the conda command was successful
if(CONDA_RESULT EQUAL 0)
# Split the output into lines
string(REPLACE "\n" ";" CONDA_ENVS_LIST ${CONDA_ENVS_OUTPUT})

# Loop through the list of paths
foreach(path ${CONDA_ENVS_LIST})
string(STRIP "${path}" path)
string(REPLACE " " ";" path "${path}")
list(GET path -1 path)
set(path "${path}/${CONDA_ENV_NAME}/bin/python")
if(EXISTS "${path}")
set(PYTHON_EXE_PATH "${path}")
message(STATUS "Found Conda environment (name: ${CONDA_ENV_NAME}) and its Python executable.")
break() # Exit the loop once a valid path is found
endif()
endforeach()
endif()
if (PYTHON_EXE_PATH STREQUAL "")
message(
WARNING
"Could NOT find path to Conda environment (name: ${CONDA_ENV_NAME}) and its Python executable.\n"
"You can specify it directly with -DPYTHON_EXE_PATH=<path>\n"
)
endif()
else (CONDA_EXECUTABLE)
message(WARNING "Could NOT find Conda executable...")
endif()
endif ()

Expand Down Expand Up @@ -97,7 +101,7 @@ if (NOT PYTHON_EXE_PATH STREQUAL "")
else ()
message(
WARNING
"Failed to find Torch home directory for Conda environment (name: ${CONDA_ENV_NAME}).\n"
"Could NOT find Torch home directory for Conda environment (name: ${CONDA_ENV_NAME}).\n"
"You can specify it directly with -DTORCH_HOME_PATH=<path>\n"
)
endif ()
Expand Down
5 changes: 3 additions & 2 deletions src/backprojector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1297,8 +1297,9 @@ void BackProjector::externalReconstruct(MultidimArray<RFLOAT> &vol_out,

pclose(pipe);

if (result != "success") {
std::cerr << "Something when wrong in the external Python call..." << std::endl;
if (trim2(result) != "success")
{
std::cerr << std::endl << "Something went wrong in the external Python call..." << std::endl;
std::cerr << "Command: " << cmd << std::endl;
std::cerr << result << std::endl;
exit(1);
Expand Down
1 change: 1 addition & 0 deletions src/backprojector.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

#define DEFAULT_EXTERNAL_RECONSTRUCT "relion_external_reconstruct"

#include "src/strings.h"
#include "src/projector.h"
#include "src/mask.h"
#include "src/tabfuncs.h"
Expand Down
5 changes: 3 additions & 2 deletions src/class_ranker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1910,8 +1910,9 @@ void ClassRanker::deployTorchModel(std::vector<float> &features, std::vector<flo
scores.push_back(std::stof(token, &sz));
s.erase(0, pos + delimiter.length());
}
if (scores.size() != count) {
std::cerr << "Something when wrong in the external Python call..." << std::endl;
if (scores.size() != count)
{
std::cerr << std::endl << "Something went wrong in the external Python call..." << std::endl;
std::cerr << "Command: " << cmd << std::endl;
std::cerr << result << std::endl;
exit(1);
Expand Down
19 changes: 19 additions & 0 deletions src/strings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,25 @@ void trim(std::string& str)
str.clear();
}

// C++11 doesn't have lambdas with capture by value, so we need a helper function
bool IsNotSpace(unsigned char ch) {
return !std::isspace(ch);
}

/** Trim all spaces and new lines from the begining and the end */
std::string trim2(const std::string& input)
{
std::string result = input;

// Remove spaces and new lines from the beginning
result.erase(result.begin(), std::find_if(result.begin(), result.end(), IsNotSpace));

// Remove spaces and new lines from the end
result.erase(std::find_if(result.rbegin(), result.rend(), IsNotSpace).base(), result.end());

return result;
}

/* NOTE: not a very safe implemenation but standard c functions do not retrieve
* more than 6 significative digits */
double textToDouble(const char* str, int _errno, std::string errmsg)
Expand Down
5 changes: 5 additions & 0 deletions src/strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
#include <string>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cctype>
#include <string.h>
#include <stdio.h>
#include "src/macros.h"
Expand Down Expand Up @@ -302,6 +304,9 @@ std::string simplify( const std::string& str );
/** Remove trailing spaces */
void trim(std::string& str);

/** Remove trailing spaces and new lines */
std::string trim2(const std::string& str);

/** Remove consecutive spaces.
*
* All consecutive spaces are replaced by a single one and starting and
Expand Down

0 comments on commit a75ccec

Please sign in to comment.