def calculate_area_of_testing_predictions(text: str, classes: list,
                                          archive_path: Path) -> dict:
    """ Calculates area of testing predictions.

    Parameters:
    ----------
    text:
        Bug description.
    classes:
        Model's classes.
    archive_path:
        Path to user archive.

    Returns:
    ----------
        Calculated predictions.
    """
    prediction = {
        parameter: get_probabilities(
            text,
            BINARY_CLASSES,
            read_from_archive(archive_path, parameter + ".sav"),
        )[1]
        for parameter in classes
    }
    return prediction
def calculate_predictions(df: pd.DataFrame, training_parameters: dict,
                          archive_path: Path) -> pd.DataFrame:
    """ Calculates predictions for received DataFrame.

    Parameters:
    ----------
    df:
        Bugs.
    training_parameters:
        Training parameters.
    archive_path:
        Path to user archive.

    Returns:
    ----------
        DataFrame with calculated predictions.
    """
    for parameter in training_parameters:
        if parameter == "Time to Resolve":
            df[parameter + "_prediction"] = df["Description_tr"].apply(
                lambda descr: get_probabilities(
                    descr,
                    training_parameters[parameter],
                    read_from_archive(archive_path, parameter + ".sav"),
                ))
        elif parameter == "Resolution":
            df[parameter + "_prediction"] = df["Description_tr"].apply(
                lambda descr: calculate_resolution_predictions(
                    descr, training_parameters[parameter], archive_path))
        elif parameter == "areas_of_testing":
            df[parameter + "_prediction"] = df["Description_tr"].apply(
                lambda descr: calculate_area_of_testing_predictions(
                    descr, training_parameters[parameter], archive_path))

    return df
def calculate_predictions(issues: pd.DataFrame, training_parameters: dict,
                          models: dict) -> pd.DataFrame:
    """Calculates predictions for received DataFrame and writes them to the DB.

    Parameters:
    ----------
    issues:
        Bug reports.
    training_parameters:
        Training parameters.
    models:
        Models' pipelines.

    Returns:
    ----------
        Predictions.
    """
    for parameter in training_parameters:
        if parameter == "Time to Resolve":
            issues[parameter + "_prediction"] = issues["Description_tr"].apply(
                lambda descr: get_probabilities(
                    descr,
                    training_parameters[parameter],
                    models[parameter],
                ))
        elif parameter == "Resolution":
            issues[parameter + "_prediction"] = issues["Description_tr"].apply(
                lambda descr: calculate_resolution_predictions(
                    descr, training_parameters[parameter], models[parameter]))
        elif parameter == "areas_of_testing":
            issues[parameter + "_prediction"] = issues["Description_tr"].apply(
                lambda descr: calculate_area_of_testing_predictions(
                    descr, training_parameters[parameter], models[parameter]))

    return issues
Exemple #4
0
    def post(self, request):
        description = clean_text(request.data.get("description"))

        if not description.strip():
            raise CannotAnalyzeDescriptionWarning

        training_parameters = get_training_parameters(request.user)

        models = load_models(request.user)

        probabilities = dict()
        for parameter in training_parameters:
            if parameter in ["Time to Resolve", "Priority"]:
                probabilities[parameter] = get_probabilities(
                    description,
                    training_parameters[parameter],
                    models[parameter],
                )
            elif parameter == "Resolution":
                probabilities["resolution"] = calculate_resolution_predictions(
                    description,
                    training_parameters[parameter],
                    models[parameter],
                )
            elif parameter == "areas_of_testing":
                probabilities[
                    parameter] = calculate_area_of_testing_predictions(
                        description,
                        training_parameters[parameter],
                        models[parameter],
                    )

        for probability in probabilities:
            if probability == "resolution":
                for resolution in probabilities[probability]:
                    resolution_obj = probabilities[probability][resolution]
                    for metric in resolution_obj:
                        resolution_obj[metric] = convert_to_integer(
                            resolution_obj[metric])
            else:
                for metric in probabilities[probability]:
                    probabilities[probability][metric] = convert_to_integer(
                        probabilities[probability][metric])

        redis_conn.set(
            f"user:{request.user.id}:description_assessment:description",
            dumps(description),
        )
        redis_conn.set(
            f"user:{request.user.id}:description_assessment:probabilities",
            dumps(probabilities),
        )

        context = {"probabilities": probabilities}

        return Response(context)
Exemple #5
0
    def post(self, request):
        def _convert_to_integer(value):
            return int(floor((value * 100) + 0.5))

        description = clean_text(request.data.get("description"))

        if not description.strip():
            raise DescriptionCantAnalyzedWarning

        archive_path = get_archive_path(request.user)
        training_parameters = read_from_archive(
            archive_path, TRAINING_PARAMETERS_FILENAME
        )
        probabilities = {}
        probabilities["resolution"] = calculate_resolution_predictions(
            description, training_parameters["Resolution"], archive_path
        )
        probabilities[
            "areas_of_testing"
        ] = calculate_area_of_testing_predictions(
            description, training_parameters["areas_of_testing"], archive_path
        )

        for metric in ["Time to Resolve", "Priority"]:
            probabilities[metric] = get_probabilities(
                description,
                training_parameters[metric],
                read_from_archive(archive_path, metric + ".sav"),
            )

        for probability in probabilities:
            if probability == "resolution":
                for resolution in probabilities[probability]:
                    resolution_obj = probabilities[probability][resolution]
                    for metric in resolution_obj:
                        resolution_obj[metric] = _convert_to_integer(
                            resolution_obj[metric]
                        )
            else:
                for metric in probabilities[probability]:
                    probabilities[probability][metric] = _convert_to_integer(
                        probabilities[probability][metric]
                    )

        redis_conn.set(f"description:{request.user.id}", dumps(description))
        redis_conn.set(
            f"probabilities:{request.user.id}", dumps(probabilities)
        )

        context = {"probabilities": probabilities}

        return Response(context)
def calculate_resolution_predictions(text: str, classes: dict,
                                     models: dict) -> dict:
    """Calculates predictions for defect resolutions.

    Parameters:
    ----------
    text:
        Bug description.
    classes:
        Model's classes.
    archive_path:
        Path to user archive.

    Returns:
    ----------
        Calculated predictions.
    """
    prediction = {
        parameter: get_probabilities(text, classes, models[parameter])
        for parameter, classes in classes.items()
    }
    return prediction
def calculate_resolution_predictions(text: str, classes: list,
                                     archive_path: Path) -> dict:
    """ Calculates predictions for defect resolutions.

    Parameters:
    ----------
    text:
        Bug description.
    classes:
        Model's classes.
    archive_path:
        Path to user archive.

    Returns:
    ----------
        Calculated predictions.
    """
    prediction = {
        parameter:
        get_probabilities(text, classes,
                          read_from_archive(archive_path, parameter + ".sav"))
        for parameter, classes in classes.items()
    }
    return prediction