<commit_before>/* Copyright 2019. Uecker Lab, University Medical Center Goettingen.
 * All rights reserved. Use of this source code is governed by
 * a BSD-style license which can be found in the LICENSE file.
 *
 * Authors: <NAME>, <NAME>
 */


#include <stdlib.h>
#include <complex.h>
#include <math.h>
#include <stdbool.h>
#include <assert.h>

#include "misc/misc.h"
#include "misc/mri.h"
#include "misc/mmio.h"
#include "misc/debug.h"

#include "linops/someops.h"

#include "nlops/nlop.h"
#include "nlops/chain.h"
#include "nlops/cast.h"
#include "nlops/stack.h"

#include "num/multind.h"
#include "num/flpmath.h"
#include "num/iovec.h"

#include "noir/model.h"

#include "moba/T1fun.h"

#include "model_T1.h"




static struct mobamod T1_create_internal(const long dims[DIMS], const complex float* mask, const complex float* TI, const complex float* psf,
				float scaling_M0, float scaling_R1s, const struct noir_model_conf_s* conf, float fov)
{
	long data_dims[DIMS];
	md_select_dims(DIMS, ~COEFF_FLAG, data_dims, dims);

	struct noir_s nlinv = noir_create3(data_dims, mask, psf, conf);
	struct mobamod ret;

	long map_dims[DIMS];
	long out_dims[DIMS];
	long in_dims[DIMS];
	long TI_dims[DIMS];

	md_select_dims(DIMS, conf->fft_flags|TIME_FLAG|TIME2_FLAG, map_dims, dims);
	md_select_dims(DIMS, conf->fft_flags|TE_FLAG|TIME_FLAG|TIME2_FLAG, out_dims, dims);
	md_select_dims(DIMS, conf->fft_flags|COEFF_FLAG|TIME_FLAG|TIME2_FLAG, in_dims, dims);
	md_select_dims(DIMS, TE_FLAG|TIME_FLAG|TIME2_FLAG, TI_dims, dims);


#if 1
	// chain T1 model

	struct nlop_s* T1 = NULL;
	if(conf->noncart) { // overgridding with factor two

		long map_dims2[DIMS];
		long out_dims2[DIMS];
		long in_dims2[DIMS];

		md_copy_dims(DIMS, map_dims2, map_dims);
		md_copy_dims(DIMS, out_dims2, out_dims);
		md_copy_dims(DIMS, in_dims2, in_dims);

		long red_fov[3];

		for (int i = 0; i < 3; i++)
			red_fov[i] = (1 == map_dims[i]) ? 1 : (map_dims[i] * fov);

		if (1. != fov) {

			md_copy_dims(3, map_dims2, red_fov);
			md_copy_dims(3, out_dims2, red_fov);
			md_copy_dims(3, in_dims2, red_fov);
		}

		T1 = nlop_T1_create(DIMS, map_dims2, out_dims2, in_dims2, TI_dims, TI, scaling_M0, scaling_R1s);

		T1 = nlop_chain_FF(T1, nlop_from_linop_F(linop_resize_center_create(DIMS, out_dims, out_dims2)));
		T1 = nlop_chain_FF(nlop_from_linop_F(linop_resize_center_create(DIMS, in_dims2, in_dims)), T1);

	} else
		T1 = nlop_T1_create(DIMS, map_dims, out_dims, in_dims, TI_dims, TI, scaling_M0, scaling_R1s);

	debug_printf(DP_INFO, "T1 Model created:\n Model ");
	nlop_debug(DP_INFO, T1);

	debug_printf(DP_INFO, "NLINV ");
	nlop_debug(DP_INFO, nlinv.nlop);

	const struct nlop_s* b = nlinv.nlop;
	const struct nlop_s* c = nlop_chain2(T1, 0, b, 0);
	nlop_free(b);

	nlinv.nlop = nlop_permute_inputs(c, 2, (const int[2]){ 1, 0 });
	nlop_free(c);
#endif
	ret.nlop = nlinv.nlop;
	ret.linop = nlinv.linop;

	return ret;
}


struct mobamod T1_create(const long dims[DIMS], const complex float* mask, const complex float* TI, const complex float* psf, 
			float scaling_M0, float scaling_R1s, const struct noir_model_conf_s* conf, float fov)<commit_msg>fix memleak<commit_after>/* Copyright 2019. Uecker Lab, University Medical Center Goettingen.
 * All rights reserved. Use of this source code is governed by
 * a BSD-style license which can be found in the LICENSE file.
 *
 * Authors: <NAME>, <NAME>
 */


#include <stdlib.h>
#include <complex.h>
#include <math.h>
#include <stdbool.h>
#include <assert.h>

#include "misc/misc.h"
#include "misc/mri.h"
#include "misc/mmio.h"
#include "misc/debug.h"

#include "linops/someops.h"

#include "nlops/nlop.h"
#include "nlops/chain.h"
#include "nlops/cast.h"
#include "nlops/stack.h"

#include "num/multind.h"
#include "num/flpmath.h"
#include "num/iovec.h"

#include "noir/model.h"

#include "moba/T1fun.h"

#include "model_T1.h"




static struct mobamod T1_create_internal(const long dims[DIMS], const complex float* mask, const complex float* TI, const complex float* psf,
				float scaling_M0, float scaling_R1s, const struct noir_model_conf_s* conf, float fov)
{
	long data_dims[DIMS];
	md_select_dims(DIMS, ~COEFF_FLAG, data_dims, dims);

	struct noir_s nlinv = noir_create3(data_dims, mask, psf, conf);
	struct mobamod ret;

	long map_dims[DIMS];
	long out_dims[DIMS];
	long in_dims[DIMS];
	long TI_dims[DIMS];

	md_select_dims(DIMS, conf->fft_flags|TIME_FLAG|TIME2_FLAG, map_dims, dims);
	md_select_dims(DIMS, conf->fft_flags|TE_FLAG|TIME_FLAG|TIME2_FLAG, out_dims, dims);
	md_select_dims(DIMS, conf->fft_flags|COEFF_FLAG|TIME_FLAG|TIME2_FLAG, in_dims, dims);
	md_select_dims(DIMS, TE_FLAG|TIME_FLAG|TIME2_FLAG, TI_dims, dims);


#if 1
	// chain T1 model

	struct nlop_s* T1 = NULL;
	if(conf->noncart) { // overgridding with factor two

		long map_dims2[DIMS];
		long out_dims2[DIMS];
		long in_dims2[DIMS];

		md_copy_dims(DIMS, map_dims2, map_dims);
		md_copy_dims(DIMS, out_dims2, out_dims);
		md_copy_dims(DIMS, in_dims2, in_dims);

		long red_fov[3];

		for (int i = 0; i < 3; i++)
			red_fov[i] = (1 == map_dims[i]) ? 1 : (map_dims[i] * fov);

		if (1. != fov) {

			md_copy_dims(3, map_dims2, red_fov);
			md_copy_dims(3, out_dims2, red_fov);
			md_copy_dims(3, in_dims2, red_fov);
		}

		T1 = nlop_T1_create(DIMS, map_dims2, out_dims2, in_dims2, TI_dims, TI, scaling_M0, scaling_R1s);

		T1 = nlop_chain_FF(T1, nlop_from_linop_F(linop_resize_center_create(DIMS, out_dims, out_dims2)));
		T1 = nlop_chain_FF(nlop_from_linop_F(linop_resize_center_create(DIMS, in_dims2, in_dims)), T1);

	} else
		T1 = nlop_T1_create(DIMS, map_dims, out_dims, in_dims, TI_dims, TI, scaling_M0, scaling_R1s);

	debug_printf(DP_INFO, "T1 Model created:\n Model ");
	nlop_debug(DP_INFO, T1);

	debug_printf(DP_INFO, "NLINV ");
	nlop_debug(DP_INFO, nlinv.nlop);

	const struct nlop_s* b = nlinv.nlop;
	const struct nlop_s* c = nlop_chain2_FF(T1, 0, b, 0);

	nlinv.nlop = nlop_permute_inputs_F(c, 2, (const int[2]){ 1, 0 });
#endif
	ret.nlop = nlinv.nlop;
	ret.linop = nlinv.linop;

	return ret;
}


struct mobamod T1_create(const long dims[DIMS], const complex float* mask, const complex float* TI, const complex float* psf, 
			float scaling_M0, float scaling_R1s, const struct noir_model_conf_s* conf, float fov)
{'fraction_non_alphanumeric': 0.13260555466366994, 'fraction_numerical': 0.02087012361534757, 'mean_word_length': 5.217564870259481, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 25, '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': '191734', 'n_tokens_mistral': 2824, 'n_tokens_neox': 2810, 'n_words': 586}