fedbiomed.researcher.responses

Module: fedbiomed.researcher.responses

Classes

Responses

CLASS
Responses(data)

Class parsing Nodes' responses Reconfigures input data into either a dictionary in a list (List[dict]), or a list with unique values.

Parameters:

Name Type Description Default
data Union[list, dict]

input response

required
Source code in fedbiomed/researcher/responses.py
def __init__(self, data: Union[list, dict]):
    """Constructor of `Responses` class.

    Args:
        data: input response
    """
    self._map_node: Dict[str, int] = {}
    if isinstance(data, dict):
        self._data = [data]
        self._update_map_node(data)
    elif isinstance(data, list):
        self._data = []
        # create a list containing unique fields
        for d in data:
            if d not in self._data:
                self._data.append(d)
                self._update_map_node(d)

    else:
        raise FedbiomedResponsesError(f"data argument should be of type list or dict, not {type(data)}")

Functions

append(response)

Appends new responses to existing responses

Parameters:

Name Type Description Default
response Union[List[Dict], Dict, _R]

List of response as dict or single response as dict that will be appended

required

Returns:

Type Description
list

List of dict as responses

Raises:

Type Description
FedbiomedResponsesError

When response argument is not in valid type

Source code in fedbiomed/researcher/responses.py
def append(self, response: Union[List[Dict], Dict, _R]) -> list:
    """ Appends new responses to existing responses

    Args:
        response: List of response as dict or single response as dict that will be appended

    Returns:
        List of dict as responses

    Raises:
        FedbiomedResponsesError: When `response` argument is not in valid type
    """

    if isinstance(response, List):
        #self._data = self._data + response
        for resp in response:
            if isinstance(resp, (dict, self.__class__)):
                self.append(resp)
            else:
                self._data = self._data + response
    elif isinstance(response, Dict):
        self._data = self._data + [response]
    elif isinstance(response, self.__class__):
        self._data = self._data + response.data()
    else:
        raise FedbiomedResponsesError(f'`The argument must be instance of List, '
                                      f'Dict or Responses` not {type(response)}')

    self._update_map_node(response)
    return self._data
data()

Gets all responses that are received

Returns:

Type Description
list

Data of the class Responses

Source code in fedbiomed/researcher/responses.py
def data(self) -> list:
    """Gets all responses that are received

    Returns:
        Data of the class `Responses`
    """
    return self._data
dataframe()

This method converts the list that includes responses to pandas dataframe

Returns:

Type Description
pd.DataFrame

Pandas DataFrame includes node responses. Each row of dataframe represent single response that comes from a node.

Source code in fedbiomed/researcher/responses.py
def dataframe(self) -> pd.DataFrame:
    """ This method converts the list that includes responses to pandas dataframe

    Returns:
        Pandas DataFrame includes node responses. Each row of dataframe represent single response that comes
            from a node.
    """

    return pd.DataFrame(self._data)
get_index_from_node_id(node_id)

Helper that allows to retrieve the index of a given node_id, assuming that all content of the object Responses are nodes' answers

Parameters:

Name Type Description Default
node_id str

id of the node

required

Returns:

Type Description
Union[int, None]

Union[int, None]: returns the index of the corresponding

Union[int, None]

node_id. If not found, returns None

Source code in fedbiomed/researcher/responses.py
def get_index_from_node_id(self, node_id: str) -> Union[int, None]:
    """
    Helper that allows to retrieve the index of a given node_id,
    assuming that all content of the object Responses are nodes' answers

    Args:
        node_id (str): id of the node

    Returns:
        Union[int, None]: returns the index of the corresponding
        node_id. If not found, returns None
    """
    return self._map_node.get(node_id)
set_data(data)

Setter for ._data attribute

Parameters:

Name Type Description Default
data List[Dict]

List of responses as python dictionary

required

Returns:

Type Description
List

List of responses as Dict

Raises:

Type Description
FedbiomedResponsesError

When data argument is not in valid type

Source code in fedbiomed/researcher/responses.py
def set_data(self, data: List[Dict]) -> List:
    """Setter for ._data attribute

    Args:
        data: List of responses as python dictionary

    Returns:
        List of responses as Dict

    Raises:
        FedbiomedResponsesError: When `data` argument is not in valid type
    """

    # TODO: Check elements of list are Dict
    if not isinstance(data, List):
        raise FedbiomedResponsesError(f'`data` argument should instance of list not {type(data)}')

    self._data = data
    for datum in data:
        self._update_map_node(datum)
    return self._data