Skip to content

backgroundtasks

  • Name: cognitivefactory.interactive_clustering_gui.backgroundtasks
  • Description: Definition of bakgroundtasks for interactive clustering graphical user interface.
  • Author: Erwan Schild
  • Created: 22/10/2021
  • Licence: CeCILL-C License v1.0 (https://cecill.info/licences.fr.html)

get_projects()

Get the list of existing project IDs. (A project is represented by a subfolder in .data folder.)

Returns:

Type Description
List[str]

List[str]: The list of existing project IDs.

Source code in src\cognitivefactory\interactive_clustering_gui\backgroundtasks.py
55
56
57
58
59
60
61
62
63
64
65
def get_projects() -> List[str]:
    """
    Get the list of existing project IDs.
    (A project is represented by a subfolder in `.data` folder.)

    Returns:
        List[str]: The list of existing project IDs.
    """

    # Return the list of project IDs.
    return [project_id for project_id in os.listdir(DATA_DIRECTORY) if os.path.isdir(DATA_DIRECTORY / project_id)]

run_constrained_clustering_task(project_id)

Background task for constraints clustering task. It performs the following actions : constrained clustering.

Parameters:

Name Type Description Default
project_id str

The ID of the project.

required
Source code in src\cognitivefactory\interactive_clustering_gui\backgroundtasks.py
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
def run_constrained_clustering_task(
    project_id: str,
) -> None:
    """
    Background task for constraints clustering task.
    It performs the following actions : constrained clustering.

    Args:
        project_id (str): The ID of the project.
    """

    ###
    ### Check parameters.
    ###

    # Check project id : Case of unknown.
    if project_id not in get_projects():
        return

    # Lock status file in order to check project status for this step.
    with FileLock(str(DATA_DIRECTORY / project_id / "status.json.lock")):
        # Load status file.
        with open(DATA_DIRECTORY / project_id / "status.json", "r") as status_fileobject_r:
            project_status: Dict[str, Any] = json.load(status_fileobject_r)

        # Check project status.
        if project_status["state"] != ICGUIStates.CLUSTERING_PENDING:
            return

        # Update status.
        update_project_status(
            project_id=project_id,
            task_progression=5,
            task_detail="Lock the project for constrained clustering step.",
            state=ICGUIStates.CLUSTERING_WORKING,
        )

    # Get current iteration.
    iteration_id: int = project_status["iteration_id"]

    ###
    ### Settings loading.
    ###
    with FileLock(str(DATA_DIRECTORY / project_id / "status.json.lock")):
        update_project_status(
            project_id=project_id,
            task_progression=10,
            task_detail="Load settings.",
        )

    # Load settings file.
    with open(DATA_DIRECTORY / project_id / "settings.json", "r") as settings_fileobject:
        settings: Dict[str, Any] = json.load(settings_fileobject)

    ###
    ### Constraints manager loading.
    ###
    with FileLock(str(DATA_DIRECTORY / project_id / "status.json.lock")):
        update_project_status(
            project_id=project_id,
            task_progression=20,
            task_detail="Load constraints manager.",
        )

    # Load constraints manager.
    with open(DATA_DIRECTORY / project_id / "constraints_manager.pkl", "rb") as constraints_manager_fileobject:
        constraints_manager: BinaryConstraintsManager = pickle.load(  # noqa: S301  # Usage of Pickle
            constraints_manager_fileobject
        )

    ###
    ### Vectors loading.
    ###
    with FileLock(str(DATA_DIRECTORY / project_id / "status.json.lock")):
        update_project_status(
            project_id=project_id,
            task_progression=30,
            task_detail="Load vectors.",
        )

    # Load vectors.
    with open(DATA_DIRECTORY / project_id / "vectors.pkl", "rb") as vectors_fileobject:
        dict_of_managed_vectors: Dict[str, csr_matrix] = pickle.load(  # noqa: S301  # Usage of Pickle
            vectors_fileobject
        )

    ###
    ### Clustering model initialization.
    ###
    with FileLock(str(DATA_DIRECTORY / project_id / "status.json.lock")):
        update_project_status(
            project_id=project_id,
            task_progression=40,
            task_detail="Initialize clustering model.",
        )

    # Initialize clustering model.
    kwargs_clustering_init: Dict[str, Any] = (
        settings[str(iteration_id)]["clustering"]["init_kargs"]
        if (settings[str(iteration_id)]["clustering"]["init_kargs"] is not None)
        else {}
    )
    clustering_model: AbstractConstrainedClustering = clustering_factory(
        algorithm=settings[str(iteration_id)]["clustering"]["algorithm"],
        random_seed=settings[str(iteration_id)]["clustering"]["random_seed"],
        **kwargs_clustering_init,
    )

    ###
    ### Constrained clustering.
    ###
    with FileLock(str(DATA_DIRECTORY / project_id / "status.json.lock")):
        update_project_status(
            project_id=project_id,
            task_progression=50,
            task_detail="Run constrained clustering.",
        )

    # Run constrained clustering.
    clustering_result: Dict[str, int] = clustering_model.cluster(
        constraints_manager=constraints_manager,
        vectors=dict_of_managed_vectors,
        nb_clusters=settings[str(iteration_id)]["clustering"]["nb_clusters"],
    )

    ###
    ### Clustering results storage.
    ###
    with FileLock(str(DATA_DIRECTORY / project_id / "status.json.lock")):
        update_project_status(
            project_id=project_id,
            task_progression=90,
            task_detail="Store clustering results.",
        )

    # Load clustering results file.
    with open(DATA_DIRECTORY / project_id / "clustering.json", "r") as clustering_fileobject_r:
        history_of_clustering_results: Dict[str, Dict[str, int]] = json.load(clustering_fileobject_r)

    # Update clustering results.
    history_of_clustering_results[str(iteration_id)] = clustering_result

    # Store clustering results.
    with open(DATA_DIRECTORY / project_id / "clustering.json", "w") as clustering_fileobject_w:
        json.dump(
            history_of_clustering_results,
            clustering_fileobject_w,
            indent=4,
        )

    ###
    ### End of task.
    ###

    # Lock status file in order to update project status.
    with FileLock(str(DATA_DIRECTORY / project_id / "status.json.lock")):
        update_project_status(
            project_id=project_id,
            task_progression=None,
            task_detail=None,
            state=ICGUIStates.ITERATION_END,
        )

run_constraints_sampling_task(project_id)

Background task route for constraints sampling task. It performs the following actions : constraints sampling.

Parameters:

Name Type Description Default
project_id str

The ID of the project.

required
Source code in src\cognitivefactory\interactive_clustering_gui\backgroundtasks.py
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
def run_constraints_sampling_task(
    project_id: str,
) -> None:
    """
    Background task route for constraints sampling task.
    It performs the following actions : constraints sampling.

    Args:
        project_id (str): The ID of the project.
    """

    ###
    ### Check parameters.
    ###

    # Check project id : Case of unknown.
    if project_id not in get_projects():
        return

    # Lock status file in order to check project iteration and project status for this step.
    with FileLock(str(DATA_DIRECTORY / project_id / "status.json.lock")):
        # Load status file.
        with open(DATA_DIRECTORY / project_id / "status.json", "r") as status_fileobject_r:
            project_status: Dict[str, Any] = json.load(status_fileobject_r)

        # Check project status.
        if project_status["state"] != ICGUIStates.SAMPLING_PENDING:
            return

        # Update status.
        update_project_status(
            project_id=project_id,
            task_progression=5,
            task_detail="Lock the project for constraints sampling step.",
            state=ICGUIStates.SAMPLING_WORKING,
        )

    # Get current iteration.
    iteration_id: int = project_status["iteration_id"]

    ###
    ### Settings loading.
    ###
    with FileLock(str(DATA_DIRECTORY / project_id / "status.json.lock")):
        update_project_status(
            project_id=project_id,
            task_progression=10,
            task_detail="Load settings.",
        )

    # Load settings file.
    with open(DATA_DIRECTORY / project_id / "settings.json", "r") as settings_fileobject:
        settings: Dict[str, Any] = json.load(settings_fileobject)

    # Get previous iteration id.
    previous_iteration_id: int = iteration_id - 1

    ###
    ### Constraints manager loading.
    ###
    with FileLock(str(DATA_DIRECTORY / project_id / "status.json.lock")):
        update_project_status(
            project_id=project_id,
            task_progression=20,
            task_detail="Load constraints manager.",
        )

    # Load constraints manager.
    with open(DATA_DIRECTORY / project_id / "constraints_manager.pkl", "rb") as constraints_manager_fileobject:
        constraints_manager: BinaryConstraintsManager = pickle.load(  # noqa: S301  # Usage of Pickle
            constraints_manager_fileobject
        )

    ###
    ### Clustering results loading.
    ###
    with FileLock(str(DATA_DIRECTORY / project_id / "status.json.lock")):
        update_project_status(
            project_id=project_id,
            task_progression=30,
            task_detail="Load previous clustering results.",
        )

    # Get previous clustering result.
    with open(DATA_DIRECTORY / project_id / "clustering.json", "r") as clustering_fileobject:
        clustering_results_for_previous_iteration: Dict[str, int] = json.load(clustering_fileobject)[
            str(previous_iteration_id)
        ]

    ###
    ### Vectors loading.
    ###
    with FileLock(str(DATA_DIRECTORY / project_id / "status.json.lock")):
        update_project_status(
            project_id=project_id,
            task_progression=40,
            task_detail="Load vectors.",
        )

    # Load vectors.
    with open(DATA_DIRECTORY / project_id / "vectors.pkl", "rb") as vectors_fileobject:
        dict_of_managed_vectors: Dict[str, csr_matrix] = pickle.load(  # noqa: S301  # Usage of Pickle
            vectors_fileobject
        )

    ###
    ### Constraints sampling initialization.
    ###
    with FileLock(str(DATA_DIRECTORY / project_id / "status.json.lock")):
        update_project_status(
            project_id=project_id,
            task_progression=50,
            task_detail="Initialize constraints sampler.",
        )

    # Initialize constraints sampler.
    kwargs_sampling_init: Dict[str, Any] = (
        settings[str(iteration_id)]["sampling"]["init_kargs"]
        if (settings[str(iteration_id)]["sampling"]["init_kargs"] is not None)
        else {}
    )
    sampler: AbstractConstraintsSampling = (
        ClustersBasedConstraintsSampling(**kwargs_sampling_init)
        if (settings[str(iteration_id)]["sampling"]["algorithm"] == "custom")
        else sampling_factory(
            algorithm=settings[str(iteration_id)]["sampling"]["algorithm"],
            random_seed=settings[str(iteration_id)]["sampling"]["random_seed"],
            **kwargs_sampling_init,
        )
    )

    ###
    ### Constraints sampling.
    ###
    with FileLock(str(DATA_DIRECTORY / project_id / "status.json.lock")):
        update_project_status(
            project_id=project_id,
            task_progression=60,
            task_detail="Sample {nb} pairs of texts to annotate.".format(
                nb=str(settings[str(iteration_id)]["sampling"]["nb_to_select"])
            ),
        )

    # Sample pairs of data to annotate.
    sampling_result: List[Tuple[str, str]] = sampler.sample(
        constraints_manager=constraints_manager,
        nb_to_select=settings[str(iteration_id)]["sampling"]["nb_to_select"],
        clustering_result=clustering_results_for_previous_iteration,
        vectors=dict_of_managed_vectors,
    )

    # If needed: complete with some random pairs of data IDs.
    if len(sampling_result) < settings[str(iteration_id)]["sampling"]["nb_to_select"]:
        with FileLock(str(DATA_DIRECTORY / project_id / "status.json.lock")):
            update_project_status(
                project_id=project_id,
                task_progression=75,
                task_detail="Need to complete with {nb} random pairs of texts.".format(
                    nb=str(settings[str(iteration_id)]["sampling"]["nb_to_select"] - len(sampling_result))
                ),
            )
        sampling_result += [
            random_sample
            for random_sample in sampling_factory(
                algorithm="random",
                random_seed=settings[str(iteration_id)]["sampling"]["random_seed"],
            ).sample(
                constraints_manager=constraints_manager,
                nb_to_select=settings[str(iteration_id)]["sampling"]["nb_to_select"] - len(sampling_result),
            )
            if random_sample not in sampling_result
        ]

    ###
    ### Sampling results storage.
    ###
    with FileLock(str(DATA_DIRECTORY / project_id / "status.json.lock")):
        update_project_status(
            project_id=project_id,
            task_progression=90,
            task_detail="Store sampling results and prepapre annotations.",
        )

    # Load sampling results file.
    with open(DATA_DIRECTORY / project_id / "sampling.json", "r") as sampling_fileobject_r:
        sampling_results: Dict[str, List[str]] = json.load(sampling_fileobject_r)

    # Load constraints file.
    with open(DATA_DIRECTORY / project_id / "constraints.json", "r") as constraints_fileobject_r:
        constraints: Dict[str, Dict[str, Any]] = json.load(constraints_fileobject_r)

    # Initialize sampling result for this iteration.
    sampling_results[str(iteration_id)] = []

    # For all sampling to annotate...
    for data_ID1, data_ID2 in sampling_result:
        # Define sampling id.
        constraint_id: str = "({data_ID1_str},{data_ID2_str})".format(
            data_ID1_str=data_ID1,
            data_ID2_str=data_ID2,
        )

        # Add sampling id.
        sampling_results[str(iteration_id)].append(constraint_id)

        # Update constraints if not already known.
        if constraint_id not in constraints.keys():
            constraints[constraint_id] = {
                "data": {
                    "id_1": data_ID1,
                    "id_2": data_ID2,
                },
                "constraint_type": None,
                "constraint_type_previous": [],
                "is_hidden": False,  # if text is deleted.
                "to_annotate": False,
                "to_review": False,
                "to_fix_conflict": False,
                "comment": "",
                "date_of_update": None,
                "iteration_of_sampling": iteration_id,
            }
        constraints[constraint_id]["to_annotate"] = True

    # Store sampling results.
    with open(DATA_DIRECTORY / project_id / "sampling.json", "w") as sampling_fileobject_w:
        json.dump(
            sampling_results,
            sampling_fileobject_w,
            indent=4,
        )

    # Store constraints results.
    with open(DATA_DIRECTORY / project_id / "constraints.json", "w") as constraints_fileobject_w:
        json.dump(
            constraints,
            constraints_fileobject_w,
            indent=4,
        )

    ###
    ### End of task.
    ###

    # Lock status file in order to update project status.
    with FileLock(str(DATA_DIRECTORY / project_id / "status.json.lock")):
        update_project_status(
            project_id=project_id,
            task_progression=None,
            task_detail=None,
            state=ICGUIStates.ANNOTATION_WITH_UPTODATE_MODELIZATION,
        )

run_modelization_update_task(project_id)

Background task route for modelization update. It performs the following actions : texts propressing, texts vectorization, constraints manager update. Emit message to share progress, raise error and announce success.

Parameters:

Name Type Description Default
project_id str

The ID of the project.

required
Source code in src\cognitivefactory\interactive_clustering_gui\backgroundtasks.py
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
def run_modelization_update_task(
    project_id: str,
) -> None:
    """
    Background task route for modelization update.
    It performs the following actions : texts propressing, texts vectorization, constraints manager update.
    Emit message to share progress, raise error and announce success.

    Args:
        project_id (str): The ID of the project.
    """

    ###
    ### Check parameters.
    ###

    # Check project id : Case of unknown.
    if project_id not in get_projects():
        return

    # Lock status file in order to check project status for this step.
    with FileLock(str(DATA_DIRECTORY / project_id / "status.json.lock")):
        ###
        ### Load needed data.
        ###

        # Load status file.
        with open(DATA_DIRECTORY / project_id / "status.json", "r") as status_fileobject_r:
            project_status: Dict[str, Any] = json.load(status_fileobject_r)

        ###
        ### Check parameters.
        ###

        # Check project status.
        working_state: Optional[ICGUIStates] = None
        if project_status["state"] == ICGUIStates.INITIALIZATION_WITH_PENDING_MODELIZATION:
            working_state = ICGUIStates.INITIALIZATION_WITH_WORKING_MODELIZATION
        elif project_status["state"] == ICGUIStates.IMPORT_AT_SAMPLING_STEP_WITH_PENDING_MODELIZATION:
            working_state = ICGUIStates.IMPORT_AT_SAMPLING_STEP_WITH_WORKING_MODELIZATION
        elif project_status["state"] == ICGUIStates.IMPORT_AT_ANNOTATION_STEP_WITH_PENDING_MODELIZATION:
            working_state = ICGUIStates.IMPORT_AT_ANNOTATION_STEP_WITH_WORKING_MODELIZATION
        elif project_status["state"] == ICGUIStates.IMPORT_AT_CLUSTERING_STEP_WITH_PENDING_MODELIZATION:
            working_state = ICGUIStates.IMPORT_AT_CLUSTERING_STEP_WITH_WORKING_MODELIZATION
        elif project_status["state"] == ICGUIStates.IMPORT_AT_ITERATION_END_WITH_PENDING_MODELIZATION:
            working_state = ICGUIStates.IMPORT_AT_ITERATION_END_WITH_WORKING_MODELIZATION
        elif project_status["state"] == ICGUIStates.ANNOTATION_WITH_PENDING_MODELIZATION_WITHOUT_CONFLICTS:
            working_state = ICGUIStates.ANNOTATION_WITH_WORKING_MODELIZATION_WITHOUT_CONFLICTS
        elif project_status["state"] == ICGUIStates.ANNOTATION_WITH_PENDING_MODELIZATION_WITH_CONFLICTS:
            working_state = ICGUIStates.ANNOTATION_WITH_WORKING_MODELIZATION_WITH_CONFLICTS
        else:
            return

        # Update status.
        update_project_status(
            project_id=project_id,
            task_progression=5,
            task_detail="Lock the project for modelization update step.",
            state=working_state,
        )

    # Get current iteration.
    iteration_id: int = project_status["iteration_id"]

    ###
    ### Settings loading.
    ###
    with FileLock(str(DATA_DIRECTORY / project_id / "status.json.lock")):
        update_project_status(
            project_id=project_id,
            task_progression=10,
            task_detail="Load settings.",
        )

    # Load settings file.
    with open(DATA_DIRECTORY / project_id / "settings.json", "r") as settings_fileobject:
        settings: Dict[str, Any] = json.load(settings_fileobject)

    ###
    ### Texts loading.
    ###
    with FileLock(str(DATA_DIRECTORY / project_id / "status.json.lock")):
        update_project_status(
            project_id=project_id,
            task_progression=15,
            task_detail="Load texts.",
        )

    # Load texts
    with open(DATA_DIRECTORY / project_id / "texts.json", "r") as texts_fileobject_r:
        texts: Dict[str, Any] = json.load(texts_fileobject_r)

    ###
    ### Texts preprocessing.
    ###
    with FileLock(str(DATA_DIRECTORY / project_id / "status.json.lock")):
        update_project_status(
            project_id=project_id,
            task_progression=20,
            task_detail="Preprocess texts.",
        )

    # Get all unpreprocessed texts.
    dict_of_unpreprocessed_texts: Dict[str, str] = {
        text_id_before_preprocessing: text_value_before_preprocessing["text"]
        for text_id_before_preprocessing, text_value_before_preprocessing in texts.items()
    }

    # Preprocess all texts (even if text is deleted).
    dict_of_preprocessed_texts: Dict[str, str] = preprocess(
        dict_of_texts=dict_of_unpreprocessed_texts,
        apply_stopwords_deletion=settings[str(iteration_id)]["preprocessing"]["apply_stopwords_deletion"],
        apply_parsing_filter=settings[str(iteration_id)]["preprocessing"]["apply_parsing_filter"],
        apply_lemmatization=settings[str(iteration_id)]["preprocessing"]["apply_lemmatization"],
        spacy_language_model=settings[str(iteration_id)]["preprocessing"]["spacy_language_model"],
    )

    # Update texts with preprocessed values.
    for text_id_with_preprocessing in texts.keys():
        texts[text_id_with_preprocessing]["text_preprocessed"] = dict_of_preprocessed_texts[text_id_with_preprocessing]

    # Store texts.
    with open(DATA_DIRECTORY / project_id / "texts.json", "w") as texts_fileobject_w:
        json.dump(
            texts,
            texts_fileobject_w,
            indent=4,
        )

    ###
    ### Texts vectorization.
    ###
    with FileLock(str(DATA_DIRECTORY / project_id / "status.json.lock")):
        update_project_status(
            project_id=project_id,
            task_progression=35,
            task_detail="Vectorize texts.",
        )

    # Get managed preprocessed texts.
    dict_of_managed_preprocessed_texts: Dict[str, str] = {
        text_id_before_vectorization: text_value_before_vectorization["text_preprocessed"]
        for text_id_before_vectorization, text_value_before_vectorization in texts.items()
        if text_value_before_vectorization["is_deleted"] is False
    }

    # Vectorize texts (only if text is not deleted).
    dict_of_managed_vectors: Dict[str, csr_matrix] = vectorize(
        dict_of_texts=dict_of_managed_preprocessed_texts,
        vectorizer_type=settings[str(iteration_id)]["vectorization"]["vectorizer_type"],
        spacy_language_model=settings[str(iteration_id)]["vectorization"]["spacy_language_model"],
    )

    # Store vectors.
    with open(DATA_DIRECTORY / project_id / "vectors.pkl", "wb") as vectors_fileobject:
        pickle.dump(
            dict_of_managed_vectors,
            vectors_fileobject,
            pickle.HIGHEST_PROTOCOL,
        )

    # Convert vectors into matrix.
    vectors_ND: csr_matrix = vstack(
        dict_of_managed_vectors[text_id_with_ND] for text_id_with_ND in dict_of_managed_vectors.keys()
    )

    ###
    ### Texts vectorization in 2D.
    ###
    with FileLock(str(DATA_DIRECTORY / project_id / "status.json.lock")):
        update_project_status(
            project_id=project_id,
            task_progression=50,
            task_detail="Reduce vectors to 2 dimensions.",
        )

    # Reduce vectors to 2 dimensions with TSNE.
    vectors_2D: ndarray = TSNE(
        n_components=2,
        # learning_rate="auto",  # Error on "scikit-learn==0.24.1" !
        init="random",
        random_state=settings[str(iteration_id)]["vectorization"]["random_seed"],
        perplexity=min(30.0, vectors_ND.shape[0] - 1),  # TSNE requirement.
    ).fit_transform(vectors_ND)

    # Store 2D vectors.
    with open(DATA_DIRECTORY / project_id / "vectors_2D.json", "w") as vectors_2D_fileobject:
        json.dump(
            {
                text_id_with_2D: {
                    "x": float(vectors_2D[i_2D][0]),
                    "y": float(vectors_2D[i_2D][1]),
                }
                for i_2D, text_id_with_2D in enumerate(dict_of_managed_vectors.keys())
            },
            vectors_2D_fileobject,
            indent=4,
        )

    ###
    ### Texts vectorization in 3D.
    ###
    with FileLock(str(DATA_DIRECTORY / project_id / "status.json.lock")):
        update_project_status(
            project_id=project_id,
            task_progression=65,
            task_detail="Reduce vectors to 3 dimensions.",
        )

    # Reduce vectors to 3 dimensions with TSNE.
    vectors_3D: ndarray = TSNE(
        n_components=3,
        # learning_rate="auto",  # Error on "scikit-learn==0.24.1" !
        init="random",
        random_state=settings[str(iteration_id)]["vectorization"]["random_seed"],
        perplexity=min(30.0, vectors_ND.shape[0] - 1),  # TSNE requirement.
    ).fit_transform(vectors_ND)

    # Store 3D vectors.
    with open(DATA_DIRECTORY / project_id / "vectors_3D.json", "w") as vectors_3D_fileobject:
        json.dump(
            {
                text_id_with_3D: {
                    "x": float(vectors_3D[i_3D][0]),
                    "y": float(vectors_3D[i_3D][1]),
                    "z": float(vectors_3D[i_3D][2]),
                }
                for i_3D, text_id_with_3D in enumerate(dict_of_managed_vectors.keys())
            },
            vectors_3D_fileobject,
            indent=4,
        )

    ###
    ### Constraints manager regeneration.
    ###
    with FileLock(str(DATA_DIRECTORY / project_id / "status.json.lock")):
        update_project_status(
            project_id=project_id,
            task_progression=80,
            task_detail="(Re)generate constraints manager.",
        )

    # Initialize constraints manager with managed texts IDs.
    new_constraints_manager: BinaryConstraintsManager = BinaryConstraintsManager(
        list_of_data_IDs=list(dict_of_managed_preprocessed_texts.keys())
    )

    # Load annotated constraints.
    with open(DATA_DIRECTORY / project_id / "constraints.json", "r") as constraints_fileobject_r:
        constraints: Dict[str, Any] = json.load(constraints_fileobject_r)

    # First, reset `to_fix_conflict` status of all constraints.
    for constraint_id in constraints.keys():
        constraints[constraint_id]["to_fix_conflict"] = False

    # Then, update constraints manager with "CANNOT_LINK" constraints.
    for _, constraint_CL in constraints.items():
        if constraint_CL["constraint_type"] == "CANNOT_LINK" and constraint_CL["is_hidden"] is False:
            new_constraints_manager.add_constraint(
                data_ID1=constraint_CL["data"]["id_1"],
                data_ID2=constraint_CL["data"]["id_2"],
                constraint_type="CANNOT_LINK",
            )  # No conflict can append, at this step the constraints manager handle only constraints of same type.

    # Initialize conflicts counter.
    number_of_conflicts: int = 0

    # Finaly, update constraints manager with "MUST_LINK" constraints.
    for constraint_ML_id, constraint_ML in constraints.items():
        if constraint_ML["constraint_type"] == "MUST_LINK" and constraint_ML["is_hidden"] is False:
            try:
                new_constraints_manager.add_constraint(
                    data_ID1=constraint_ML["data"]["id_1"],
                    data_ID2=constraint_ML["data"]["id_2"],
                    constraint_type="MUST_LINK",
                )  # Conflicts can append.
            except ValueError:
                # Update conflicts status.
                constraints[constraint_ML_id]["to_fix_conflict"] = True
                number_of_conflicts += 1

    # Store new constraints manager.
    with open(DATA_DIRECTORY / project_id / "constraints_manager.pkl", "wb") as constraints_manager_fileobject:
        pickle.dump(
            new_constraints_manager,
            constraints_manager_fileobject,
            pickle.HIGHEST_PROTOCOL,
        )

    # Store updated constraints in file.
    with open(DATA_DIRECTORY / project_id / "constraints.json", "w") as constraints_fileobject_w:
        json.dump(constraints, constraints_fileobject_w, indent=4)

    ###
    ### Store modelization inference.
    ###
    with FileLock(str(DATA_DIRECTORY / project_id / "status.json.lock")):
        update_project_status(
            project_id=project_id,
            task_progression=95,
            task_detail="Store modelization inference results.",
        )

    # Load modelization inference file.
    with open(DATA_DIRECTORY / project_id / "modelization.json", "r") as modelization_fileobject_r:
        modelization: Dict[str, Any] = json.load(modelization_fileobject_r)

    # Get constraints transitivity.
    constraints_transitivity: Dict[str, Dict[str, Dict[str, None]]] = (
        new_constraints_manager._constraints_transitivity  # noqa: WPS437
    )

    # Update modelization inference.
    modelization = {}
    for text_id_in_manager in new_constraints_manager.get_list_of_managed_data_IDs():
        modelization[text_id_in_manager] = {
            "MUST_LINK": list(constraints_transitivity[text_id_in_manager]["MUST_LINK"].keys()),
            "CANNOT_LINK": list(constraints_transitivity[text_id_in_manager]["CANNOT_LINK"].keys()),
        }
    for component_id, component in enumerate(new_constraints_manager.get_connected_components()):
        for text_id_in_component in component:
            modelization[text_id_in_component]["COMPONENT"] = component_id

    # Store updated modelization inference in file.
    with open(DATA_DIRECTORY / project_id / "modelization.json", "w") as modelization_fileobject_w:
        json.dump(modelization, modelization_fileobject_w, indent=4)

    ###
    ### End of task.
    ###

    # Define the next state.
    end_state: Optional[ICGUIStates] = None
    if working_state == ICGUIStates.INITIALIZATION_WITH_WORKING_MODELIZATION:
        end_state = (
            ICGUIStates.CLUSTERING_TODO if (number_of_conflicts == 0) else ICGUIStates.INITIALIZATION_WITH_ERRORS
        )
    elif working_state == ICGUIStates.IMPORT_AT_SAMPLING_STEP_WITH_WORKING_MODELIZATION:
        end_state = (
            ICGUIStates.SAMPLING_TODO if (number_of_conflicts == 0) else ICGUIStates.IMPORT_AT_SAMPLING_STEP_WITH_ERRORS
        )
    elif working_state == ICGUIStates.IMPORT_AT_CLUSTERING_STEP_WITH_WORKING_MODELIZATION:
        end_state = (
            ICGUIStates.CLUSTERING_TODO
            if (number_of_conflicts == 0)
            else ICGUIStates.IMPORT_AT_CLUSTERING_STEP_WITH_ERRORS
        )
    elif working_state == ICGUIStates.IMPORT_AT_ITERATION_END_WITH_WORKING_MODELIZATION:
        end_state = (
            ICGUIStates.ITERATION_END if (number_of_conflicts == 0) else ICGUIStates.IMPORT_AT_ITERATION_END_WITH_ERRORS
        )
    #### elif working_state in {
    ####     ICGUIStates.IMPORT_AT_ANNOTATION_STEP_WITH_WORKING_MODELIZATION,
    ####     ICGUIStates.ANNOTATION_WITH_WORKING_MODELIZATION_WITHOUT_CONFLICTS,
    ####     ICGUIStates.ANNOTATION_WITH_WORKING_MODELIZATION_WITH_CONFLICTS,
    #### }:
    else:
        end_state = (
            ICGUIStates.ANNOTATION_WITH_UPTODATE_MODELIZATION
            if (number_of_conflicts == 0)
            else ICGUIStates.ANNOTATION_WITH_OUTDATED_MODELIZATION_WITH_CONFLICTS
        )

    # Lock status file in order to update project status.
    with FileLock(str(DATA_DIRECTORY / project_id / "status.json.lock")):
        update_project_status(
            project_id=project_id,
            task_progression=None,
            task_detail=None,
            state=end_state,
        )

update_project_status(project_id, task_progression, task_detail, state=None)

Update project status during task.

Parameters:

Name Type Description Default
project_id str

The ID of the project.

required
task_progression Optional[int]

The progression of the updated task.

required
task_detail Optional[str]

The detail of the updated task.

required
state Optional[ICGUIStates]

The state of the application. Unchanged if None. Defaults to None.

None
Source code in src\cognitivefactory\interactive_clustering_gui\backgroundtasks.py
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
def update_project_status(
    project_id: str,
    task_progression: Optional[int],
    task_detail: Optional[str],
    state: Optional[ICGUIStates] = None,
) -> None:
    """
    Update project status during task.

    Args:
        project_id (str): The ID of the project.
        task_progression (Optional[int]): The progression of the updated task.
        task_detail (Optional[str]): The detail of the updated task.
        state (Optional[ICGUIStates], optional): The state of the application. Unchanged if `None`. Defaults to `None`.
    """

    # Load status file.
    with open(DATA_DIRECTORY / project_id / "status.json", "r") as status_fileobject_r:
        project_status: Dict[str, Any] = json.load(status_fileobject_r)

    # Update status.
    project_status["task"] = (
        {
            "progression": task_progression,
            "detail": task_detail,
        }
        if (task_progression is not None)
        else None
    )
    project_status["state"] = project_status["state"] if (state is None) else state

    # Store status.
    with open(DATA_DIRECTORY / project_id / "status.json", "w") as status_fileobject_w:
        json.dump(
            project_status,
            status_fileobject_w,
            indent=4,
        )