Skip to content

Commit

Permalink
Merge branch 'dev' into feature/2508-sort-folders-study-tree
Browse files Browse the repository at this point in the history
  • Loading branch information
skamril authored Jan 23, 2025
2 parents 104cbd8 + a7dd4ab commit 90a0077
Show file tree
Hide file tree
Showing 24 changed files with 172 additions and 1,567 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/commitlint.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
name: Lint Commit Messages
name: commitlint
on: [pull_request]

permissions:
contents: read
pull-requests: read

jobs:
commitlint:
commit-messages-lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: check license headers
name: license-header
on:
push:
branches:
Expand Down
13 changes: 13 additions & 0 deletions .github/workflows/pr-title.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: pr-title
on:
pull_request:
types: ['opened', 'edited', 'reopened', 'synchronize']

jobs:
pr-title-lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Dependencies
run: npm install @commitlint/[email protected]
- uses: JulienKode/[email protected]
5 changes: 5 additions & 0 deletions antarest/core/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,11 @@ def __init__(self, message: str) -> None:
super().__init__(HTTPStatus.UNPROCESSABLE_ENTITY, message)


class MatrixImportFailed(HTTPException):
def __init__(self, message: str) -> None:
super().__init__(HTTPStatus.UNPROCESSABLE_ENTITY, message)


class ConstraintTermNotFound(HTTPException):
"""
Exception raised when a constraint term is not found.
Expand Down
31 changes: 17 additions & 14 deletions antarest/study/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import base64
import collections
import contextlib
import csv
import http
import io
import logging
Expand All @@ -25,6 +24,7 @@
from uuid import uuid4

import numpy as np
import numpy.typing as npt
import pandas as pd
from antares.study.version import StudyVersion
from fastapi import HTTPException, UploadFile
Expand All @@ -39,6 +39,7 @@
CommandApplicationError,
FolderCreationNotAllowed,
IncorrectPathError,
MatrixImportFailed,
NotAManagedStudyException,
OutputAlreadyArchived,
OutputAlreadyUnarchived,
Expand Down Expand Up @@ -197,6 +198,20 @@ def get_disk_usage(path: t.Union[str, Path]) -> int:
return total_size


def _imports_matrix_from_bytes(data: bytes) -> npt.NDArray[np.float64]:
"""Tries to convert bytes to a numpy array when importing a matrix"""
str_data = data.decode("utf-8")
if not str_data:
return np.zeros(shape=(0, 0))
for delimiter in [",", ";", "\t"]:
with contextlib.suppress(Exception):
df = pd.read_csv(io.BytesIO(data), delimiter=delimiter, header=None).replace(",", ".", regex=True)
df = df.dropna(axis=1, how="all") # We want to remove columns full of NaN at the import
matrix = df.to_numpy(dtype=np.float64)
return matrix
raise MatrixImportFailed("Could not parse the given matrix")


def _get_path_inside_user_folder(
path: str, exception_class: t.Type[t.Union[FolderCreationNotAllowed, ResourceDeletionNotAllowed]]
) -> str:
Expand Down Expand Up @@ -1591,19 +1606,7 @@ def _create_edit_study_command(
elif isinstance(tree_node, InputSeriesMatrix):
if isinstance(data, bytes):
# noinspection PyTypeChecker
str_data = data.decode("utf-8")
if not str_data:
matrix = np.zeros(shape=(0, 0))
else:
size_to_check = min(len(str_data), 64) # sniff a chunk only to speed up the code
try:
delimiter = csv.Sniffer().sniff(str_data[:size_to_check], delimiters=r"[,;\t]").delimiter
except csv.Error:
# Can happen with data with only one column. In this case, we don't care about the delimiter.
delimiter = "\t"
df = pd.read_csv(io.BytesIO(data), delimiter=delimiter, header=None).replace(",", ".", regex=True)
df = df.dropna(axis=1, how="all") # We want to remove columns full of NaN at the import
matrix = df.to_numpy(dtype=np.float64)
matrix = _imports_matrix_from_bytes(data)
matrix = matrix.reshape((1, 0)) if matrix.size == 0 else matrix
return ReplaceMatrix(
target=url, matrix=matrix.tolist(), command_context=context, study_version=study_version
Expand Down
11 changes: 8 additions & 3 deletions commitlint.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { RuleConfigSeverity } from "@commitlint/types";
// Config used by 'commitlint' and 'pr-title' GitHub Actions

// Config used by 'commitlint' GitHub action.
export default {
const RuleConfigSeverity = {
Disabled: 0,
Warning: 1,
Error: 2,
}

module.exports = {
extends: ["@commitlint/config-conventional"],
// Rules: https://commitlint.js.org/reference/rules.html
rules: {
Expand Down
Loading

0 comments on commit 90a0077

Please sign in to comment.