Skip to content

Commit e8c693e

Browse files
[Andrew Polyakov] Changed behaviour of the NDArrayValueProvider
1 parent 8a497ad commit e8c693e

File tree

8 files changed

+40
-30
lines changed

8 files changed

+40
-30
lines changed

utbot-python/src/main/kotlin/org/utbot/python/evaluation/PythonWorkerManager.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class PythonWorkerManager(
8181
serverSocket.localPort.toString(),
8282
coverageReceiver.address().first,
8383
coverageReceiver.address().second,
84-
"--logfile", logfile.absolutePath,
84+
"--logfile", "C:\\Users\\Andrew\\Desktop\\Huawei\\Tests\\test_cli\\out\\log.log", // TODO: Remove
8585
"--loglevel", "DEBUG", // "DEBUG", "INFO", "WARNING", "ERROR"
8686
"--coverage_type", coverageMeasureMode.toString(), // "lines", "instructions"
8787
sendCoverageContinuously.toSendCoverageContinuouslyString(), // "--send_coverage", "--no-send_coverage"

utbot-python/src/main/kotlin/org/utbot/python/evaluation/serialization/PythonObjectParser.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ class NdarrayMemoryObject(
107107
typeinfo: TypeInfo,
108108
comparable: Boolean,
109109
val items: List<String>,
110+
val dimensions: List<Int>
110111
) : MemoryObject(id, typeinfo, comparable)
111112

112113
fun PythonTree.PythonTreeNode.toMemoryObject(memoryDump: MemoryDump, reload: Boolean = false): String {
@@ -124,10 +125,11 @@ fun PythonTree.PythonTreeNode.toMemoryObject(memoryDump: MemoryDump, reload: Boo
124125
)
125126
}
126127

127-
is PythonTree.NdarrayNode -> { // TODO: Optimize for ndarray
128+
is PythonTree.NDArrayNode -> { // TODO: Optimize for ndarray
128129
val items = this.items.entries
129130
.map { it.value.toMemoryObject(memoryDump) }
130-
NdarrayMemoryObject(id, typeinfo, this.comparable, items) // TODO: Ndarray to memory object
131+
val dimensions = this.dimensions
132+
NdarrayMemoryObject(id, typeinfo, this.comparable, items, dimensions) // TODO: Ndarray to memory object
131133
}
132134

133135
is PythonTree.ListNode -> {
@@ -321,16 +323,15 @@ fun MemoryObject.toPythonTree(
321323

322324
is NdarrayMemoryObject -> {
323325
val draft = when (this.qualname) {
324-
"np.ndarray" -> PythonTree.NdarrayNode(id, mutableMapOf(), listOf())
325-
"numpy.ndarray" -> PythonTree.NdarrayNode(id, mutableMapOf(), listOf())
326+
"numpy.ndarray" -> PythonTree.NDArrayNode(id, mutableMapOf(), this.dimensions)
326327
else -> PythonTree.ListNode(id, mutableMapOf())
327328
}
328329
visited[this.id] = draft
329330

330331
items.mapIndexed { index, valueId ->
331332
val value = memoryDump.getById(valueId).toPythonTree(memoryDump, visited)
332333
when (draft) {
333-
is PythonTree.NdarrayNode -> draft.items[index] = value
334+
is PythonTree.NDArrayNode -> draft.items[index] = value
334335
else -> {}
335336
}
336337
}

utbot-python/src/main/kotlin/org/utbot/python/framework/api/python/PythonTree.kt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,23 +155,28 @@ object PythonTree {
155155
else false
156156
}
157157
}
158-
class NdarrayNode(
158+
class NDArrayNode(
159159
id: Long,
160160
val items: MutableMap<Int, PythonTreeNode>,
161-
val dimention: List<Int>
161+
val dimensions: List<Int>
162162
) : PythonTreeNode(id, pythonNdarrayClassId) {
163-
constructor(items: MutableMap<Int, PythonTreeNode>, dimention: List<Int>) : this(PythonIdGenerator.createId(), items, dimention)
163+
constructor(items: MutableMap<Int, PythonTreeNode>, dimensions: List<Int>) : this(PythonIdGenerator.createId(), items, dimensions)
164164

165165
override val children: List<PythonTreeNode>
166166
get() = items.values.toList()
167167

168168
override fun typeEquals(other: Any?): Boolean {
169-
return if (other is NdarrayNode)
169+
return if (other is NDArrayNode)
170170
children.zip(other.children).all {
171171
it.first.typeEquals(it.second)
172172
}
173173
else false
174174
}
175+
176+
177+
override fun toString(): String {
178+
return "ndarray[${items.values}], shape: ($dimensions)"
179+
}
175180
}
176181

177182
class DictNode(

utbot-python/src/main/kotlin/org/utbot/python/framework/codegen/model/constructor/tree/PythonCgVariableConstructor.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,11 @@ class PythonCgVariableConstructor(cgContext: CgContext) : CgVariableConstructor(
6767
}
6868
}
6969

70-
is PythonTree.NdarrayNode -> {
70+
is PythonTree.NDArrayNode -> {
7171
val items = objectNode.items.values.map { pythonBuildObject(it) }
72-
Pair(CgPythonNdarray(items.map { it.first }), items.flatMap { it.second })
72+
val shape = objectNode.dimensions
73+
val type = objectNode.items.values.firstOrNull()?.type?.typeName
74+
Pair(CgPythonNdarray(items.map { it.first }, shape, type?: "int"), items.flatMap { it.second })
7375
}
7476

7577
is PythonTree.TupleNode -> {

utbot-python/src/main/kotlin/org/utbot/python/framework/codegen/model/constructor/visitor/CgPythonRenderer.kt

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ internal class CgPythonRenderer(
192192
println("\"\"\"")
193193
}
194194

195-
override fun visit(element: CgDocRegularStmt){
195+
override fun visit(element: CgDocRegularStmt) {
196196
if (element.isEmpty()) return
197197

198198
println(element.stmt)
@@ -561,13 +561,16 @@ internal class CgPythonRenderer(
561561
}
562562

563563
override fun visit(element: CgPythonNdarray) {
564-
if (element.elements.isEmpty()) {
565-
print("numpy.array([])")
566-
} else{
567-
print("numpy.array([")
568-
element.elements.renderSeparated()
569-
print("])")
570-
}
564+
565+
print("numpy.ndarray(")
566+
print("dtype=" + element.dtype + ",")
567+
print("shape=(")
568+
569+
print(element.shape.joinToString(",") { it.toString() })
570+
print("), buffer=numpy.array([")
571+
element.elements.renderSeparated()
572+
print("]))")
573+
571574

572575
}
573576

utbot-python/src/main/kotlin/org/utbot/python/framework/codegen/model/tree/CgPythonElement.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ class CgPythonSet(
124124
}
125125

126126
class CgPythonNdarray(
127-
val elements: List<CgValue>
127+
val elements: List<CgValue>,
128+
val shape: List<Int>,
129+
val dtype: String
128130
) : CgValue, CgPythonElement {
129131
override val type: PythonClassId = pythonNdarrayClassId
130132
}

utbot-python/src/main/kotlin/org/utbot/python/fuzzing/provider/NDArrayValueProvider.kt

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import org.utbot.python.fuzzing.FuzzedUtType.Companion.toFuzzed
1010
import org.utbot.python.fuzzing.PythonFuzzedValue
1111
import org.utbot.python.fuzzing.PythonMethodDescription
1212
import org.utbot.python.fuzzing.PythonValueProvider
13-
import org.utbot.python.fuzzing.provider.utils.*
1413
import org.utpython.types.*
1514
import kotlin.math.abs
1615

@@ -41,20 +40,18 @@ class NDArrayValueProvider(
4140
)
4241
) {
4342
PythonFuzzedValue(
44-
45-
PythonTree.NdarrayNode(
43+
PythonTree.NDArrayNode(
4644
emptyMap<Int, PythonTree.PythonTreeNode>().toMutableMap(), // Generate new Python IntNode
4745
((it.first().tree as PythonTree.ListNode).items as Map<Int, PythonTree.PrimitiveNode>).values.map { node ->
48-
abs(node.repr.take(3).toInt()) % 10
46+
abs(node.repr.take(3).toInt()) % 7 // TODO: Fix this
4947
}
5048
), "%var% = ${type.pythonTypeRepresentation()}"
5149
)
5250
},
53-
// TODO: Call to modify
5451
modify = sequence {
5552
yield(Routine.Call((0 until 10000). map{param[1]}.toFuzzed()) { instance, arguments ->
56-
val obj = instance.tree as PythonTree.NdarrayNode
57-
(0 until obj.dimention.fold(1, Int::times)).map {
53+
val obj = instance.tree as PythonTree.NDArrayNode
54+
(0 until obj.dimensions.fold(1, Int::times)).map {
5855
obj.items[it] = arguments.get(it).tree
5956
}
6057

utbot-python/src/main/kotlin/org/utbot/python/newtyping/inference/baseline/BaselineAlgorithm.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class BaselineAlgorithm(
7676

7777
private val checkedSignatures: MutableSet<UtType> = mutableSetOf()
7878

79-
private fun getRandomType(): UtType? {
79+
private fun getRandomType(): UtType? { // TODO: Remove random type when ndarray
8080
val weights = states.map { 1.0 / (it.anyNodes.size * it.anyNodes.size + 1) }
8181
val state = weightedRandom(states, weights, random)
8282
val newState = expandState(state, storage, state.anyNodes.map { mixtureType })
@@ -109,7 +109,7 @@ class BaselineAlgorithm(
109109
val laudedType = getLaudedType()
110110
if (laudedType != null) return laudedType
111111
}
112-
val randomType = getRandomType()
112+
val randomType = getRandomType() // TODO: Not to do when ndarray (via pythonMethod )
113113
if (randomType != null) return randomType
114114
}
115115

0 commit comments

Comments
 (0)