<commit_before># -*- coding: utf-8 -*-
import pickle

import rpy2.robjects as robjects
from rpy2.robjects import numpy2ri
numpy2ri.activate()

from django.core.management.base import BaseCommand, CommandError

from annotations.models import Corpus

class Command(BaseCommand):
    help = 'Exports the matrix in R format'

    def add_arguments(self, parser):
        parser.add_argument('corpus', type=str)

    def handle(self, *args, **options):
        try:
            corpus = Corpus.objects.get(title=options['corpus'])
        except Corpus.DoesNotExist:
            raise CommandError('Corpus with title {} does not exist'.format[options['corpus']])        

        pre = 'plots/{}_'.format(corpus.pk)
        matrix = pickle.load(open(pre + 'matrix.p', 'rb'))
        fragment_ids = pickle.load(open(pre + 'fragments.p', 'rb'))

        r_matrix = robjects.r.matrix(matrix)
        r_matrix.colnames = robjects.StrVector(fragment_ids)
        
        robjects.assign('data', r_matrix)
        robjects.save('data', file='test.R')<commit_msg>Fixing the export to R<commit_after># -*- coding: utf-8 -*-
import pickle

import rpy2.robjects as robjects
from rpy2.robjects.packages import importr
from rpy2.robjects import numpy2ri

from django.core.management.base import BaseCommand, CommandError

from annotations.models import Corpus, Fragment

numpy2ri.activate()


class Command(BaseCommand):
    help = 'Exports the distance matrix in R format'

    def add_arguments(self, parser):
        parser.add_argument('corpus', type=str)

    def handle(self, *args, **options):
        # Retrieve the Corpus from the database
        try:
            corpus = Corpus.objects.get(title=options['corpus'])
        except Corpus.DoesNotExist:
            raise CommandError('Corpus with title {} does not exist'.format[options['corpus']])        

        # Retrieve the pickled data
        pre = 'plots/{}_'.format(corpus.pk)
        matrix = pickle.load(open(pre + 'matrix.p', 'rb'))
        fragment_ids = pickle.load(open(pre + 'fragments.p', 'rb'))
        tenses = pickle.load(open(pre + 'tenses.p', 'rb'))

        # Assign the pickled data to R variables
        robjects.r.assign('matrix', matrix)
        robjects.r.assign('fragment_ids', robjects.StrVector(fragment_ids))

        for (language, _) in Fragment.LANGUAGES:
            robjects.r.assign('tenses_{}'.format(language), robjects.StrVector(tenses[language]))

        # Save the workspace
        filename = '{}.RData'.format(corpus.title)
        base = importr('base')
        base.save_image(file=filename)
{'fraction_non_alphanumeric': 0.12451662799690642, 'fraction_numerical': 0.004253673627223511, 'mean_word_length': 3.0998415213946116, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4255286', 'n_tokens_mistral': 811, 'n_tokens_neox': 792, 'n_words': 182}